1+ import 'package:ht_categories_client/ht_categories_client.dart' ;
2+ import 'package:ht_countries_client/ht_countries_client.dart' ;
3+ import 'package:ht_headlines_client/ht_headlines_client.dart' ;
14import 'package:ht_headlines_repository/ht_headlines_repository.dart' ;
25import 'package:ht_shared/ht_shared.dart' ;
6+ import 'package:ht_sources_client/ht_sources_client.dart' ;
37import 'package:mocktail/mocktail.dart' ;
48import 'package:test/test.dart' ;
59
10+ // Mocks and Fakes
611class MockHtHeadlinesClient extends Mock implements HtHeadlinesClient {}
712
813class FakeHeadline extends Fake implements Headline {}
914
15+ class FakeCategory extends Fake implements Category {}
16+
17+ class FakeSource extends Fake implements Source {}
18+
19+ class FakeCountry extends Fake implements Country {}
20+
1021void main () {
1122 group ('HtHeadlinesRepository' , () {
1223 late HtHeadlinesClient client;
1324 late HtHeadlinesRepository repository;
1425
26+ // Sample data for testing
27+ final category1 = Category (id: 'cat1' , name: 'General' );
28+ final source1 = Source (id: 'src1' , name: 'BBC' );
29+ final country1 = Country (
30+ id: 'cty1' ,
31+ isoCode: 'US' ,
32+ name: 'USA' ,
33+ flagUrl: '' ,
34+ );
35+ final categoriesList = [category1];
36+ final sourcesList = [source1];
37+ final eventCountriesList = [country1];
38+
1539 setUpAll (() {
1640 registerFallbackValue (FakeHeadline ());
41+ registerFallbackValue (FakeCategory ());
42+ registerFallbackValue (FakeSource ());
43+ registerFallbackValue (FakeCountry ());
1744 });
1845
1946 setUp (() {
@@ -24,32 +51,29 @@ void main() {
2451 group ('getHeadlines' , () {
2552 const limit = 10 ;
2653 const startAfterId = 'abc' ;
27- const category = 'general' ;
28- const source = 'bbc' ;
29- const eventCountry = 'US' ;
3054
3155 final headlines = List .generate (
3256 limit,
3357 (index) => Headline (id: '$index ' , title: 'Headline $index ' ),
3458 );
3559
36- test ('successfully fetches headlines' , () async {
60+ test ('successfully fetches headlines with filters ' , () async {
3761 when (
3862 () => client.getHeadlines (
3963 limit: limit,
4064 startAfterId: startAfterId,
41- category : category ,
42- source : source ,
43- eventCountry : eventCountry ,
65+ categories : categoriesList ,
66+ sources : sourcesList ,
67+ eventCountries : eventCountriesList ,
4468 ),
4569 ).thenAnswer ((_) async => headlines);
4670
4771 final result = await repository.getHeadlines (
4872 limit: limit,
4973 startAfterId: startAfterId,
50- category : category ,
51- source : source ,
52- eventCountry : eventCountry ,
74+ categories : categoriesList ,
75+ sources : sourcesList ,
76+ eventCountries : eventCountriesList ,
5377 );
5478
5579 expect (
@@ -67,21 +91,21 @@ void main() {
6791 test ('throws HeadlinesFetchException on client failure' , () async {
6892 when (
6993 () => client.getHeadlines (
70- limit: limit,
71- startAfterId: startAfterId,
72- category : category ,
73- source : source ,
74- eventCountry : eventCountry ,
94+ limit: any (named : ' limit' ) ,
95+ startAfterId: any (named : ' startAfterId' ) ,
96+ categories : any (named : 'categories' ) ,
97+ sources : any (named : 'sources' ) ,
98+ eventCountries : any (named : 'eventCountries' ) ,
7599 ),
76100 ).thenThrow (const HeadlinesFetchException ('Failed to fetch headlines' ));
77101
78102 expect (
79103 () => repository.getHeadlines (
80104 limit: limit,
81105 startAfterId: startAfterId,
82- category : category ,
83- source : source ,
84- eventCountry : eventCountry ,
106+ categories : categoriesList ,
107+ sources : sourcesList ,
108+ eventCountries : eventCountriesList ,
85109 ),
86110 throwsA (isA <HeadlinesFetchException >()),
87111 );
@@ -91,31 +115,28 @@ void main() {
91115 group ('getHeadlinesStream' , () {
92116 const limit = 10 ;
93117 const startAfterId = 'abc' ;
94- const category = 'general' ;
95- const source = 'bbc' ;
96- const eventCountry = 'US' ;
97118
98119 final headlines = List .generate (
99120 limit,
100121 (index) => Headline (id: '$index ' , title: 'Headline $index ' ),
101122 );
102- test ('successfully fetches headlines stream' , () async {
123+ test ('successfully fetches headlines stream with filters ' , () async {
103124 when (
104125 () => client.getHeadlines (
105126 limit: limit,
106127 startAfterId: startAfterId,
107- category : category ,
108- source : source ,
109- eventCountry : eventCountry ,
128+ categories : categoriesList ,
129+ sources : sourcesList ,
130+ eventCountries : eventCountriesList ,
110131 ),
111132 ).thenAnswer ((_) async => headlines);
112133
113134 final result = repository.getHeadlinesStream (
114135 limit: limit,
115136 startAfterId: startAfterId,
116- category : category ,
117- source : source ,
118- eventCountry : eventCountry ,
137+ categories : categoriesList ,
138+ sources : sourcesList ,
139+ eventCountries : eventCountriesList ,
119140 interval: const Duration (milliseconds: 100 ),
120141 );
121142
@@ -134,7 +155,7 @@ void main() {
134155
135156 group ('getHeadline' , () {
136157 const id = '123' ;
137- const headline = Headline (id: id, title: 'Headline 1' );
158+ final headline = Headline (id: id, title: 'Headline 1' );
138159
139160 test ('successfully fetches a headline' , () async {
140161 when (
@@ -173,7 +194,7 @@ void main() {
173194 });
174195
175196 group ('createHeadline' , () {
176- const headline = Headline (id: '123' , title: 'Headline 1' );
197+ final headline = Headline (id: '123' , title: 'Headline 1' );
177198
178199 test ('successfully creates a headline' , () async {
179200 when (
@@ -199,7 +220,7 @@ void main() {
199220 });
200221
201222 group ('updateHeadline' , () {
202- const headline = Headline (id: '123' , title: 'Headline 1' );
223+ final headline = Headline (id: '123' , title: 'Headline 1' );
203224
204225 test ('successfully updates a headline' , () async {
205226 when (
0 commit comments