Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3ec8384

Browse files
Added function searchTopHeadlinesNews in news repository
1 parent 21f050c commit 3ec8384

File tree

3 files changed

+88
-16
lines changed

3 files changed

+88
-16
lines changed

‎lib/feature/data/repository/news/news_repository_impl.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,19 @@ class NewsRepositoryImpl implements NewsRepository {
3030
return Left(ConnectionFailure());
3131
}
3232
}
33+
34+
@override
35+
Future<Either<Failure, TopHeadlinesNewsResponseModel>> searchTopHeadlinesNews(String keyword) async {
36+
var isConnected = await networkInfo.isConnected;
37+
if (isConnected) {
38+
try {
39+
var response = await newsRemoteDataSource.searchTopHeadlinesNews(keyword);
40+
return Right(response);
41+
} on DioError catch (error) {
42+
return Left(ServerFailure(error.message));
43+
}
44+
} else {
45+
return Left(ConnectionFailure());
46+
}
47+
}
3348
}

‎lib/feature/domain/repository/news/news_repository.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ abstract class NewsRepository {
66

77
Future<Either<Failure, TopHeadlinesNewsResponseModel>> getTopHeadlinesNews(String category);
88

9+
Future<Either<Failure, TopHeadlinesNewsResponseModel>> searchTopHeadlinesNews(String keyword);
910
}

‎test/feature/data/repository/news/news_repository_impl_test.dart

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,57 @@ void main() {
3838
when(mockNetworkInfo.isConnected).thenAnswer((_) async => false);
3939
}
4040

41-
group('getTopHeadlinesNews', () {
42-
final tCategory = 'technology';
43-
final tTopHeadlinesNewsResponseModel = TopHeadlinesNewsResponseModel.fromJson(
44-
json.decode(
45-
fixture('top_headlines_news_response_model.json'),
46-
),
47-
);
48-
41+
void testNetworkConnected(Function endpointCall) {
4942
test(
5043
'make sure that the device is connected to the internet when making a request to the endpoint',
5144
() async {
5245
// arrange
5346
setUpMockNetworkConnected();
5447

5548
// act
56-
await newsRepositoryImpl.getTopHeadlinesNews(tCategory);
49+
await endpointCall.call();
50+
51+
// assert
52+
verify(mockNetworkInfo.isConnected);
53+
},
54+
);
55+
}
56+
57+
void testNetworkDisconnected(Function endpointCall) {
58+
test(
59+
'make sure to return the ConnectionFailure object when the device connection is not connected to the internet',
60+
() async {
61+
// arrange
62+
setUpMockNetworkDisconnected();
63+
64+
// act
65+
final result = await endpointCall.call();
5766

5867
// assert
5968
verify(mockNetworkInfo.isConnected);
69+
expect(result, Left(ConnectionFailure()));
6070
},
6171
);
72+
}
73+
74+
group('getTopHeadlinesNews', () {
75+
final tCategory = 'technology';
76+
final tTopHeadlinesNewsResponseModel = TopHeadlinesNewsResponseModel.fromJson(
77+
json.decode(
78+
fixture('top_headlines_news_response_model.json'),
79+
),
80+
);
81+
82+
testNetworkConnected(() => newsRepositoryImpl.getTopHeadlinesNews(tCategory));
6283

6384
test(
6485
'make sure to return the value of the TopHeadlinesNewsResponseModel object when '
6586
'NewsRemoteDataSource successfully receives a successful data response from the endpoint',
6687
() async {
6788
// arrange
6889
setUpMockNetworkConnected();
69-
when(mockNewsRemoteDataSource.getTopHeadlinesNews(tCategory)).thenAnswer((_) async => tTopHeadlinesNewsResponseModel);
90+
when(mockNewsRemoteDataSource.getTopHeadlinesNews(tCategory))
91+
.thenAnswer((_) async => tTopHeadlinesNewsResponseModel);
7092

7193
// act
7294
final result = await newsRepositoryImpl.getTopHeadlinesNews(tCategory);
@@ -94,20 +116,54 @@ void main() {
94116
},
95117
);
96118

119+
testNetworkDisconnected(() => newsRepositoryImpl.getTopHeadlinesNews(tCategory));
120+
});
121+
122+
group('searchTopHeadlinesNews', () {
123+
final tKeyword = 'testKeyword';
124+
final tTopHeadlinesNewsResponseModel = TopHeadlinesNewsResponseModel.fromJson(
125+
json.decode(
126+
fixture('top_headlines_news_response_model.json'),
127+
),
128+
);
129+
130+
testNetworkConnected(() => newsRepositoryImpl.searchTopHeadlinesNews(tKeyword));
131+
97132
test(
98-
'make sure to return the ConnectionFailure object when the device connection is not connected '
99-
'to the internet',
133+
'make sure to return the value of the TopHeadlinesNewsResponseModel object when '
134+
'NewsRemoteDataSource successfully receives a successful data response from the endpoint',
100135
() async {
101136
// arrange
102-
setUpMockNetworkDisconnected();
137+
setUpMockNetworkConnected();
138+
when(mockNewsRemoteDataSource.searchTopHeadlinesNews(tKeyword))
139+
.thenAnswer((_) async => tTopHeadlinesNewsResponseModel);
103140

104141
// act
105-
final result = await newsRepositoryImpl.getTopHeadlinesNews(tCategory);
142+
final result = await newsRepositoryImpl.searchTopHeadlinesNews(tKeyword);
106143

107144
// assert
108-
verify(mockNetworkInfo.isConnected);
109-
expect(result, Left(ConnectionFailure()));
145+
verify(mockNewsRemoteDataSource.searchTopHeadlinesNews(tKeyword));
146+
expect(result, Right(tTopHeadlinesNewsResponseModel));
147+
},
148+
);
149+
150+
test(
151+
'make sure to return the ServerFailure object when NewsRemoteDataSource receives a failure '
152+
'data response from the endpoint',
153+
() async {
154+
// arrange
155+
setUpMockNetworkConnected();
156+
when(mockNewsRemoteDataSource.searchTopHeadlinesNews(tKeyword)).thenThrow(DioError(error: 'testError'));
157+
158+
// act
159+
final result = await newsRepositoryImpl.searchTopHeadlinesNews(tKeyword);
160+
161+
// assert
162+
verify(mockNewsRemoteDataSource.searchTopHeadlinesNews(tKeyword));
163+
expect(result, Left(ServerFailure('testError')));
110164
},
111165
);
166+
167+
testNetworkDisconnected(() => newsRepositoryImpl.searchTopHeadlinesNews(tKeyword));
112168
});
113169
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /