-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstyle_screen_test.dart
More file actions
360 lines (299 loc) · 12.2 KB
/
Copy pathstyle_screen_test.dart
File metadata and controls
360 lines (299 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:cambridge_beer_festival/screens/screens.dart';
import 'package:cambridge_beer_festival/models/models.dart';
import 'package:cambridge_beer_festival/providers/providers.dart';
import 'package:cambridge_beer_festival/services/services.dart';
import 'package:cambridge_beer_festival/widgets/widgets.dart';
import 'package:provider/provider.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'provider_test.mocks.dart';
void main() {
group('StyleScreen', () {
late MockDrinkRepository mockDrinkRepository;
late MockFestivalRepository mockFestivalRepository;
late MockAnalyticsService mockAnalyticsService;
late BeerProvider provider;
const producer1 = Producer(
id: 'brewery1',
name: 'Test Brewery 1',
location: 'Cambridge, UK',
yearFounded: 1990,
products: [],
);
const producer2 = Producer(
id: 'brewery2',
name: 'Test Brewery 2',
location: 'London, UK',
yearFounded: 2000,
products: [],
);
const product1 = Product(
id: 'drink1',
name: 'Test IPA 1',
abv: 5.0,
category: 'beer',
style: 'IPA',
dispense: 'cask',
);
const product2 = Product(
id: 'drink2',
name: 'Test IPA 2',
abv: 6.5,
category: 'beer',
style: 'IPA',
dispense: 'keg',
);
const product3 = Product(
id: 'drink3',
name: 'Test Stout',
abv: 4.5,
category: 'beer',
style: 'Stout',
dispense: 'cask',
);
final drink1 = Drink(product: product1, producer: producer1, festivalId: 'cbf2025');
final drink2 = Drink(product: product2, producer: producer2, festivalId: 'cbf2025');
final drink3 = Drink(product: product3, producer: producer1, festivalId: 'cbf2025');
setUp(() async {
SharedPreferences.setMockInitialValues({});
mockDrinkRepository = MockDrinkRepository();
mockFestivalRepository = MockFestivalRepository();
mockAnalyticsService = MockAnalyticsService();
when(mockFestivalRepository.getFestivals()).thenAnswer(
(_) async => FestivalsResponse(
festivals: [DefaultFestivals.cambridge2025],
defaultFestivalId: DefaultFestivals.cambridge2025.id,
version: '1.0',
baseUrl: 'https://data.cambeerfestival.app',
),
);
when(mockFestivalRepository.getSelectedFestivalId()).thenAnswer((_) async => null);
when(mockDrinkRepository.getDrinks(any)).thenAnswer((_) async => []);
provider = BeerProvider(
drinkRepository: mockDrinkRepository,
festivalRepository: mockFestivalRepository,
analyticsService: mockAnalyticsService,
);
await provider.initialize();
});
tearDown(() {
provider.dispose();
});
Widget createTestWidget(String style) {
return ChangeNotifierProvider<BeerProvider>.value(
value: provider,
child: MaterialApp(
home: StyleScreen(festivalId: 'cbf2025', style: style),
),
);
}
testWidgets('displays style not found when no drinks with that style exist',
(WidgetTester tester) async {
await tester.pumpWidget(createTestWidget('NonExistent Style'));
await tester.pumpAndSettle();
expect(find.text('Style Not Found'), findsOneWidget);
expect(find.text('No drinks found with this style.'), findsOneWidget);
});
testWidgets('displays style information when drinks with that style exist',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1, drink2]);
await provider.loadDrinks();
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
expect(find.text('IPA'), findsWidgets);
// The new UI uses HeroInfoCard showing drink count
expect(find.textContaining('2 drinks at this festival'), findsOneWidget);
// Note: 'Drinks' section header with count
expect(find.text('Drinks (2)'), findsOneWidget);
// Festival name in breadcrumb
expect(find.textContaining('Festival'), findsWidgets);
});
testWidgets('displays original mixed-case style name for a lowercase URL param',
(WidgetTester tester) async {
// Style URLs use a lowercase canonical form (see buildStylePath), so the
// router passes a lowercased style. The screen must still display the
// original mixed-case name from the matched drinks.
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1, drink2]);
await provider.loadDrinks();
await tester.pumpWidget(createTestWidget('ipa'));
await tester.pumpAndSettle();
expect(find.text('IPA'), findsWidgets);
expect(find.text('ipa'), findsNothing);
});
testWidgets('displays drinks with the specified style',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1, drink2]);
await provider.loadDrinks();
await tester.binding.setSurfaceSize(const Size(400, 1200));
addTearDown(() => tester.binding.setSurfaceSize(null));
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
expect(find.text('Drinks (2)'), findsOneWidget);
expect(find.text('Test IPA 1'), findsOneWidget);
expect(find.text('Test IPA 2'), findsOneWidget);
});
testWidgets('navigates to drink detail when drink card is tapped',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1]);
await provider.loadDrinks();
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
// Find the drink card - this verifies the card is rendered and tappable
expect(find.text('Test IPA 1'), findsOneWidget);
// NOTE: Navigation to DrinkDetailScreen uses go_router's context.push()
// which requires GoRouter in the widget tree. This is tested in E2E tests
// (test-e2e/routing.spec.ts) instead of unit tests.
});
testWidgets('toggles favorite when favorite button is tapped',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1]);
await provider.loadDrinks();
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
expect(drink1.isFavorite, false);
// Mock toggleFavorite to properly toggle state
final favorites = <String>{};
when(mockDrinkRepository.toggleFavorite(any, any)).thenAnswer((invocation) async {
final drinkId = invocation.positionalArguments[1] as String;
if (favorites.contains(drinkId)) {
favorites.remove(drinkId);
return false;
} else {
favorites.add(drinkId);
return true;
}
});
// Find and tap the favorite button
final favoriteButton = find.descendant(
of: find.byType(DrinkCard),
matching: find.byIcon(Icons.favorite_border),
);
await tester.tap(favoriteButton);
await tester.pumpAndSettle();
expect(drink1.isFavorite, true);
});
testWidgets('displays correct count of drinks',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1, drink2]);
await provider.loadDrinks();
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
expect(find.text('Drinks (2)'), findsOneWidget);
});
testWidgets('filters drinks to show only the specified style',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1, drink2, drink3]);
await provider.loadDrinks();
await tester.binding.setSurfaceSize(const Size(400, 1200));
addTearDown(() => tester.binding.setSurfaceSize(null));
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
// Should only show IPA drinks
expect(find.text('Test IPA 1'), findsOneWidget);
expect(find.text('Test IPA 2'), findsOneWidget);
expect(find.text('Test Stout'), findsNothing);
expect(find.text('Drinks (2)'), findsOneWidget);
});
testWidgets('shows drinks from different breweries with same style',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1, drink2]);
await provider.loadDrinks();
await tester.binding.setSurfaceSize(const Size(400, 1200));
addTearDown(() => tester.binding.setSurfaceSize(null));
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
// Should show drinks from both breweries
expect(find.text('Test IPA 1'), findsOneWidget);
expect(find.text('Test IPA 2'), findsOneWidget);
expect(find.textContaining('Test Brewery 1'), findsOneWidget);
expect(find.textContaining('Test Brewery 2'), findsOneWidget);
});
testWidgets('displays style description when available',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1, drink2]);
await provider.loadDrinks();
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
// Wait for FutureBuilder to complete
await tester.pumpAndSettle();
// Should display the IPA description
expect(
find.textContaining('heavily hopped'),
findsOneWidget,
);
// Should still show the stats
// Note: 'Drinks' section header with count
expect(find.textContaining('Drinks ('), findsOneWidget);
// Festival name in breadcrumb
expect(find.textContaining('Festival'), findsWidgets);
// Average ABV in HeroInfoCard
expect(find.textContaining('Average ABV:'), findsOneWidget);
});
testWidgets('header without description when style has none',
(WidgetTester tester) async {
// Create a drink with a style that has no description
const productUnknown = Product(
id: 'drink4',
name: 'Unknown Style Beer',
abv: 5.0,
category: 'beer',
style: 'Unknown Style',
dispense: 'cask',
);
final drinkUnknown = Drink(
product: productUnknown,
producer: producer1,
festivalId: 'cbf2025',
);
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drinkUnknown]);
await provider.loadDrinks();
await tester.pumpWidget(createTestWidget('Unknown Style'));
await tester.pumpAndSettle();
// Wait for FutureBuilder to complete
await tester.pumpAndSettle();
// Should not crash and should still show stats
// Note: 'Drinks' section header with count
expect(find.textContaining('Drinks ('), findsOneWidget);
// Festival name in breadcrumb
expect(find.textContaining('Festival'), findsWidgets);
// Average ABV in HeroInfoCard
expect(find.textContaining('Average ABV:'), findsOneWidget);
});
testWidgets('can scroll when header is expanded',
(WidgetTester tester) async {
when(mockDrinkRepository.getDrinks(any))
.thenAnswer((_) async => [drink1, drink2, drink3]);
await provider.loadDrinks();
await tester.pumpWidget(createTestWidget('IPA'));
await tester.pumpAndSettle();
// Wait for FutureBuilder to complete
await tester.pumpAndSettle();
// Find the CustomScrollView
final scrollView = find.byType(CustomScrollView);
expect(scrollView, findsOneWidget);
// Verify description is visible when expanded
expect(
find.textContaining('heavily hopped'),
findsOneWidget,
);
// Scroll down to collapse the header
await tester.drag(scrollView, const Offset(0, -200));
await tester.pumpAndSettle();
// After scrolling, the list of drinks should be visible
expect(find.text('Test IPA 1'), findsOneWidget);
expect(find.text('Test IPA 2'), findsOneWidget);
});
});
}