diet-guard/app/test/screens/food_bank/fb_screen_test.dart
Krzysztof kuhy Rudnicki 7cb09a5b85 Split food_bank_screen; share the filter widgets with history
food_bank_screen.dart (712) carried a near-clone of the filter sheet batch 2
had just factored out of history: the same name-search field, the same slider
captions, and four more copies of the same min/max macro range block. Splitting
it in isolation would have baked that duplication in permanently, so the shared
widgets moved to a neutral `lib/widgets/filters/` first and both screens now
import them.

  food_bank_screen.dart      712 -> 238
  lib/widgets/food_bank/
    fb_filter.dart            156  FbFilter, FbSortField, applyFbFilter
    fb_filter_sheet.dart      244  the sheet itself
    fb_add_entry_dialog.dart  103  the manual-entry dialog
  lib/widgets/history/history_filter_fields.dart
                        -> lib/widgets/filters/filter_fields.dart

FilterRangeRow gained `showEndpointLabels`, defaulting to true. History showed
endpoint captions and a selected-range readout; the food-bank sheet never did,
and switching them on there would have been a UI change smuggled in under a
refactor. The food-bank call sites pass false, so both screens render exactly
what they rendered before.

FbSortField and HistorySortField stay separate on purpose -- they are not the
same enum (`name` vs `date`/`description`), so the sort dropdown is not shared.

The test file mirrors the source split, three files under test/screens/
food_bank/ plus fb_test_support.dart for the setUp/tearDown/settle prologue,
same shape as batch 2's history_test_support.dart.

Verified: 555 flutter tests (unchanged count -- no test lost, and food bank's
own RangeSlider/sort-toggle tests still pass against the shared widget),
flutter analyze clean over lib/ and test/, live ~/.local/share/diet_guard
byte-identical. Over-cap files: 40 -> 38.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
2026-08-15 19:43:08 +02:00

207 lines
5.6 KiB
Dart

import 'package:diet_guard_app/models/food_bank_record.dart';
import 'package:diet_guard_app/screens/food_bank_screen.dart';
import 'package:diet_guard_app/services/foodbank_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'fb_test_support.dart';
void main() {
useTempFoodBankStores();
testWidgets('shows empty-bank message when no entries exist', (
tester,
) async {
await tester.runAsync(() async {
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
expect(find.textContaining('Food bank is empty'), findsOneWidget);
});
});
testWidgets('lists entries from the merged bank', (tester) async {
await tester.runAsync(() async {
await FoodBankService.instance.addManualEntry(
const FoodBankRecord(
desc: 'Manual oat',
kcal: 370,
proteinG: 13,
carbsG: 66,
fatG: 7,
grams: 100,
count: 0,
),
);
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
expect(find.text('Manual oat'), findsOneWidget);
});
});
testWidgets('FAB opens add-entry dialog and saving adds to bank', (
tester,
) async {
await tester.runAsync(() async {
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
await tester.tap(find.byType(FloatingActionButton));
await settle(tester);
expect(find.text('Add to food bank'), findsOneWidget);
await tester.enterText(
find.widgetWithText(TextField, 'Name'),
'Test food',
);
await tester.enterText(
find.widgetWithText(TextField, 'Kcal'),
'200',
);
await tester.tap(find.text('Save to bank'));
await settle(tester);
// After saving, the screen reloads and shows the new entry.
expect(find.text('Test food'), findsOneWidget);
});
});
testWidgets('dialog cancel does not save anything', (tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
await tester.tap(find.byType(FloatingActionButton));
await settle(tester);
await tester.tap(find.text('Cancel'));
await settle(tester);
expect(find.textContaining('Food bank is empty'), findsOneWidget);
});
});
testWidgets('dialog save with empty name does nothing', (tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
await tester.tap(find.byType(FloatingActionButton));
await settle(tester);
// Tap save without entering a name.
await tester.tap(find.text('Save to bank'));
await settle(tester);
// Dialog stays open; no entry saved.
expect(find.text('Add to food bank'), findsOneWidget);
});
});
testWidgets('filter icon appears when entries exist', (tester) async {
await tester.runAsync(() async {
await FoodBankService.instance.addManualEntry(
const FoodBankRecord(
desc: 'Oat',
kcal: 370,
proteinG: 13,
carbsG: 66,
fatG: 7,
grams: 100,
count: 0,
),
);
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
expect(
find.widgetWithIcon(IconButton, Icons.filter_list),
findsOneWidget,
);
});
});
testWidgets('filter sheet opens and Apply filters results', (tester) async {
await tester.runAsync(() async {
await FoodBankService.instance.addManualEntry(
const FoodBankRecord(
desc: 'Oat',
kcal: 370,
proteinG: 13,
carbsG: 66,
fatG: 7,
grams: 100,
count: 0,
),
);
await FoodBankService.instance.addManualEntry(
const FoodBankRecord(
desc: 'Egg',
kcal: 155,
proteinG: 13,
carbsG: 1,
fatG: 11,
grams: 100,
count: 0,
),
);
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
await tester.tap(find.byIcon(Icons.filter_list));
await settle(tester);
expect(find.text('Filter & Sort'), findsOneWidget);
// Type in the only TextField in the sheet (the name search field).
await tester.enterText(find.byType(TextField).first, 'Oat');
await settle(tester);
await tester.tap(find.text('Apply'));
await settle(tester);
// Sheet is closed; only the matching entry is visible.
expect(find.text('Filter & Sort'), findsNothing);
expect(find.text('Oat'), findsOneWidget);
expect(find.text('Egg'), findsNothing);
});
});
testWidgets('filter sheet Clear all resets draft then Apply shows all', (
tester,
) async {
await tester.runAsync(() async {
await FoodBankService.instance.addManualEntry(
const FoodBankRecord(
desc: 'Walnut',
kcal: 654,
proteinG: 15,
carbsG: 14,
fatG: 65,
grams: 100,
count: 0,
),
);
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
await tester.tap(find.byIcon(Icons.filter_list));
await settle(tester);
await tester.tap(find.text('Clear all'));
await settle(tester);
await tester.tap(find.text('Apply'));
await settle(tester);
expect(find.text('Walnut'), findsOneWidget);
});
});
}