diet-guard/app/test/screens/food_bank/fb_sheet_test.dart
Krzysztof kuhy Rudnicki 49144a2c49 Move the app to material_ui for Flutter 3.50
Flutter 3.50 deprecates package:flutter/material.dart; 73 deprecation
infos failed flutter analyze --fatal-infos, and four Dart pins were
behind (crdt_sync_flutter 0.8.2, flutter_local_notifications 23.0.2,
github_device_auth 0.5.0, sync_settings_ui 0.8.2). Those UI packages
now sit on material_ui, so the app has to as well or their widgets lose
MaterialLocalizations.

Imports swap one-for-one and dart fix re-sorted the directives; no
behaviour change. analyze is clean and all six test shards pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWWfb63jncj9Lca2jeX
2026-11-16 09:25:34 +01:00

182 lines
5.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:diet_guard_app/models/food_bank_record.dart';
import 'package:diet_guard_app/models/food_entry.dart';
import 'package:diet_guard_app/screens/food_bank_screen.dart';
import 'package:diet_guard_app/services/foodbank_service.dart';
import 'package:diet_guard_app/widgets/food_bank/fb_filter_sheet.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../label_finders.dart';
import 'fb_test_support.dart';
void main() {
useTempFoodBankStores();
testWidgets('record tile shows usage count for log-derived entries', (
tester,
) async {
await tester.runAsync(() async {
await FoodBankService.instance.rebuildAndPersist({
'2026-06-22': [
const FoodEntry(
id: '1',
time: '2026-06-22T08:00:00+02:00',
desc: 'rice',
grams: 100,
kcal: 130,
proteinG: 3,
carbsG: 28,
fatG: 0.3,
source: 'manual',
),
const FoodEntry(
id: '2',
time: '2026-06-22T12:00:00+02:00',
desc: 'rice',
grams: 100,
kcal: 130,
proteinG: 3,
carbsG: 28,
fatG: 0.3,
source: 'manual',
),
],
});
await tester.pumpWidget(const MaterialApp(home: FoodBankScreen()));
await settle(tester);
// The rice entry was logged twice — the tile trailing shows ×2.
expect(find.textContaining('×2'), findsOneWidget);
});
});
testWidgets('filter sheet sort dropdown changes sort field', (tester) async {
await tester.runAsync(() async {
// Zero macros: no RangeSliders appear, sort section is immediately visible.
await FoodBankService.instance.addManualEntry(
const FoodBankRecord(
desc: 'ZeroItem',
kcal: 0,
proteinG: 0,
carbsG: 0,
fatG: 0,
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.byType(FbFilterSheet), findsOneWidget);
// Usage count is the default sort chip; pick the name chip instead.
await tester.tap(chipByLabel('Name'));
await settle(tester);
await tester.tap(byIconLabel('Apply'));
await settle(tester);
expect(find.byType(FbFilterSheet), findsNothing);
expect(find.text('ZeroItem'), findsOneWidget);
});
});
testWidgets('filter sheet RangeSlider onChanged callbacks fire', (
tester,
) async {
await tester.runAsync(() async {
// Non-zero macros: all four RangeSliders appear in the filter sheet.
await FoodBankService.instance.addManualEntry(
const FoodBankRecord(
desc: 'SliderFood',
kcal: 200,
proteinG: 10,
carbsG: 25,
fatG: 8,
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);
// Use getRect() + dragFrom() to bypass the _maybeViewOf ancestor-search
// failure that tester.drag(finder, …) triggers inside modal overlays.
// Kcal slider covers lines 468-471.
await tester.dragFrom(
tester.getRect(find.byType(RangeSlider).at(0)).center,
const Offset(-30, 0),
);
await settle(tester);
// Protein slider covers lines 491-495.
await tester.dragFrom(
tester.getRect(find.byType(RangeSlider).at(1)).center,
const Offset(-30, 0),
);
await settle(tester);
// Carbs slider covers lines 515-518.
await tester.dragFrom(
tester.getRect(find.byType(RangeSlider).at(2)).center,
const Offset(-30, 0),
);
await settle(tester);
// Fat slider covers lines 538-541.
await tester.dragFrom(
tester.getRect(find.byType(RangeSlider).at(3)).center,
const Offset(-30, 0),
);
await settle(tester);
await tester.tap(byIconLabel('Apply'));
await settle(tester);
expect(find.byType(FbFilterSheet), findsNothing);
});
});
testWidgets('filter sheet sort direction toggle fires onSortChanged', (
tester,
) async {
await tester.runAsync(() async {
// Zero macros: no RangeSliders appear, sort section is immediately visible.
await FoodBankService.instance.addManualEntry(
const FoodBankRecord(
desc: 'ZeroItem2',
kcal: 0,
proteinG: 0,
carbsG: 0,
fatG: 0,
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.byType(FbFilterSheet), findsOneWidget);
// Default sort is count-descending; the direction icon is arrow_downward.
await tester.tap(find.byIcon(Icons.arrow_downward));
await settle(tester);
await tester.tap(byIconLabel('Apply'));
await settle(tester);
expect(find.text('ZeroItem2'), findsOneWidget);
});
});
}