Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update unit test
  • Loading branch information
CoderJava committed May 27, 2020
commit a82058f5d4c89511a7250a30dbf566397d7b84c2
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void main() {
});

group('getTopHeadlinesNews', () {
final tCategory = 'technology';
final tTopHeadlinesNewsResponseModel = TopHeadlinesNewsResponseModel.fromJson(
json.decode(
fixture('top_headlines_news_response_model.json'),
Expand Down Expand Up @@ -66,7 +67,7 @@ void main() {
setUpMockDioSuccess();

// act
await newsRemoteDataSource.getTopHeadlinesNews();
await newsRemoteDataSource.getTopHeadlinesNews('all');

// assert
verify(
Expand All @@ -81,6 +82,29 @@ void main() {
},
);

test(
'make sure there is a GET request to endpoint /v2/top-headlines?country=:country&apiKey=:apiKey&category=:category',
() async {
// arrange
setUpMockDioSuccess();

// act
await newsRemoteDataSource.getTopHeadlinesNews(tCategory);

// assert
verify(
mockDio.get(
'/v2/top-headlines',
queryParameters: {
'category': tCategory,
'country': 'id',
'apiKey': constantConfig.apiKeyNewsApi,
},
),
);
},
);

test(
'make sure to return the TopHeadlinesNewsResponseModel object when the '
'response code is successful from the endpoint',
Expand All @@ -89,7 +113,7 @@ void main() {
setUpMockDioSuccess();

// act
final result = await newsRemoteDataSource.getTopHeadlinesNews();
final result = await newsRemoteDataSource.getTopHeadlinesNews(tCategory);

// assert
expect(result, tTopHeadlinesNewsResponseModel);
Expand All @@ -107,7 +131,7 @@ void main() {
when(mockDio.get(any, queryParameters: anyNamed('queryParameters'))).thenAnswer((_) async => response);

// act
final call = newsRemoteDataSource.getTopHeadlinesNews();
final call = newsRemoteDataSource.getTopHeadlinesNews(tCategory);

// assert
expect(() => call, throwsA(TypeMatcher<DioError>()));
Expand Down
17 changes: 9 additions & 8 deletions test/feature/data/repository/news/news_repository_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void main() {
}

group('getTopHeadlinesNews', () {
final tCategory = 'technology';
final tTopHeadlinesNewsResponseModel = TopHeadlinesNewsResponseModel.fromJson(
json.decode(
fixture('top_headlines_news_response_model.json'),
Expand All @@ -52,7 +53,7 @@ void main() {
setUpMockNetworkConnected();

// act
await newsRepositoryImpl.getTopHeadlinesNews();
await newsRepositoryImpl.getTopHeadlinesNews(tCategory);

// assert
verify(mockNetworkInfo.isConnected);
Expand All @@ -65,13 +66,13 @@ void main() {
() async {
// arrange
setUpMockNetworkConnected();
when(mockNewsRemoteDataSource.getTopHeadlinesNews()).thenAnswer((_) async => tTopHeadlinesNewsResponseModel);
when(mockNewsRemoteDataSource.getTopHeadlinesNews(tCategory)).thenAnswer((_) async => tTopHeadlinesNewsResponseModel);

// act
final result = await newsRepositoryImpl.getTopHeadlinesNews();
final result = await newsRepositoryImpl.getTopHeadlinesNews(tCategory);

// assert
verify(mockNewsRemoteDataSource.getTopHeadlinesNews());
verify(mockNewsRemoteDataSource.getTopHeadlinesNews(tCategory));
expect(result, Right(tTopHeadlinesNewsResponseModel));
},
);
Expand All @@ -82,13 +83,13 @@ void main() {
() async {
// arrange
setUpMockNetworkConnected();
when(mockNewsRemoteDataSource.getTopHeadlinesNews()).thenThrow(DioError(error: 'testError'));
when(mockNewsRemoteDataSource.getTopHeadlinesNews(tCategory)).thenThrow(DioError(error: 'testError'));

// act
final result = await newsRepositoryImpl.getTopHeadlinesNews();
final result = await newsRepositoryImpl.getTopHeadlinesNews(tCategory);

// assert
verify(mockNewsRemoteDataSource.getTopHeadlinesNews());
verify(mockNewsRemoteDataSource.getTopHeadlinesNews(tCategory));
expect(result, Left(ServerFailure('testError')));
},
);
Expand All @@ -101,7 +102,7 @@ void main() {
setUpMockNetworkDisconnected();

// act
final result = await newsRepositoryImpl.getTopHeadlinesNews();
final result = await newsRepositoryImpl.getTopHeadlinesNews(tCategory);

// assert
verify(mockNetworkInfo.isConnected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:convert';

import 'package:dartz/dartz.dart';
import 'package:flutter_news_app/core/usecase/usecase.dart';
import 'package:flutter_news_app/feature/data/model/topheadlinesnews/top_headlines_news_response_model.dart';
import 'package:flutter_news_app/feature/domain/repository/news/news_repository.dart';
import 'package:flutter_news_app/feature/domain/usecase/gettopheadlinesnews/get_top_headlines_news.dart';
Expand All @@ -15,6 +14,7 @@ class MockNewsRepository extends Mock implements NewsRepository {}
void main() {
GetTopHeadlinesNews getTopHeadlinesNews;
MockNewsRepository mockNewsRepository;
final tCategory = 'technology';

setUp(() {
mockNewsRepository = MockNewsRepository();
Expand All @@ -31,15 +31,30 @@ void main() {
fixture('top_headlines_news_response_model.json'),
),
);
when(mockNewsRepository.getTopHeadlinesNews()).thenAnswer((_) async => Right(tTopHeadlinesNewsResponseModel));
when(mockNewsRepository.getTopHeadlinesNews(tCategory))
.thenAnswer((_) async => Right(tTopHeadlinesNewsResponseModel));

// act
final result = await getTopHeadlinesNews(NoParams());
final result = await getTopHeadlinesNews(ParamsGetTopHeadlinesNews(category: tCategory));

// assert
expect(result, Right(tTopHeadlinesNewsResponseModel));
verify(mockNewsRepository.getTopHeadlinesNews());
verify(mockNewsRepository.getTopHeadlinesNews(tCategory));
verifyNoMoreInteractions(mockNewsRepository);
},
);

test(
'make sure the output of the toString function',
() async {
// arrange
final tParamsGetTopHeadlinesNews = ParamsGetTopHeadlinesNews(category: tCategory);

// assert
expect(
tParamsGetTopHeadlinesNews.toString(),
'ParamsGetTopHeadlinesNews{category: $tCategory}',
);
},
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:convert';
import 'package:bloc_test/bloc_test.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter_news_app/core/error/failure.dart';
import 'package:flutter_news_app/core/usecase/usecase.dart';
import 'package:flutter_news_app/feature/data/model/topheadlinesnews/top_headlines_news_response_model.dart';
import 'package:flutter_news_app/feature/domain/usecase/gettopheadlinesnews/get_top_headlines_news.dart';
import 'package:flutter_news_app/feature/presentation/bloc/topheadlinesnews/bloc.dart';
Expand Down Expand Up @@ -55,6 +54,7 @@ void main() {
);

group('LoadTopHeadlinesNews', () {
final tCategory = 'technology';
final tTopHeadlinesNewsResponseModel = TopHeadlinesNewsResponseModel.fromJson(
json.decode(
fixture('top_headlines_news_response_model.json'),
Expand All @@ -68,11 +68,11 @@ void main() {
when(mockGetTopHeadlinesNews(any)).thenAnswer((_) async => Right(tTopHeadlinesNewsResponseModel));

// act
topHeadlinesNewsBloc.add(LoadTopHeadlinesNewsEvent());
topHeadlinesNewsBloc.add(LoadTopHeadlinesNewsEvent(category: tCategory));
await untilCalled(mockGetTopHeadlinesNews(any));

// assert
verify(mockGetTopHeadlinesNews(NoParams()));
verify(mockGetTopHeadlinesNews(ParamsGetTopHeadlinesNews(category: tCategory)));
},
);

Expand All @@ -86,14 +86,14 @@ void main() {
},
act: (bloc) {
// act
return bloc.add(LoadTopHeadlinesNewsEvent());
return bloc.add(LoadTopHeadlinesNewsEvent(category: tCategory));
},
expect: [
LoadingTopHeadlinesNewsState(),
LoadedTopHeadlinesNewsState(listArticles: tTopHeadlinesNewsResponseModel.articles),
],
verify: (_) async {
verify(mockGetTopHeadlinesNews(NoParams())).called(1);
verify(mockGetTopHeadlinesNews(ParamsGetTopHeadlinesNews(category: tCategory))).called(1);
},
);

Expand All @@ -111,14 +111,14 @@ void main() {
},
act: (bloc) {
// act
return bloc.add(LoadTopHeadlinesNewsEvent());
return bloc.add(LoadTopHeadlinesNewsEvent(category: tCategory));
},
expect: [
LoadingTopHeadlinesNewsState(),
FailureTopHeadlinesNewsState(errorMessage: 'testErrorMessage'),
],
verify: (_) async {
verify(mockGetTopHeadlinesNews(NoParams())).called(1);
verify(mockGetTopHeadlinesNews(ParamsGetTopHeadlinesNews(category: tCategory))).called(1);
},
);

Expand All @@ -130,14 +130,14 @@ void main() {
return topHeadlinesNewsBloc;
},
act: (bloc) {
return bloc.add(LoadTopHeadlinesNewsEvent());
return bloc.add(LoadTopHeadlinesNewsEvent(category: tCategory));
},
expect: [
LoadingTopHeadlinesNewsState(),
FailureTopHeadlinesNewsState(errorMessage: messageConnectionFailure),
],
verify: (_) async {
verify(mockGetTopHeadlinesNews(NoParams())).called(1);
verify(mockGetTopHeadlinesNews(ParamsGetTopHeadlinesNews(category: tCategory))).called(1);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ import 'package:flutter_test/flutter_test.dart';

void main() {
group('LoadTopHeadlinesNews', () {
final tCategory = 'technology';

test(
'make sure the props value is []',
() async {
// assert
expect(LoadTopHeadlinesNewsEvent().props, []);
expect(LoadTopHeadlinesNewsEvent(category: tCategory).props, [tCategory]);
},
);

test(
'make sure the output of the toString function',
() async {
// assert
expect(LoadTopHeadlinesNewsEvent().toString(), 'LoadTopHeadlinesNewsEvent');
expect(
LoadTopHeadlinesNewsEvent(category: tCategory).toString(),
'LoadTopHeadlinesNewsEvent{category: $tCategory}',
);
},
);
});
Expand Down