import 'package:commons_core/commons_core.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:tane/db/database.dart'; import '../support/test_support.dart'; void main() { late AppDatabase db; setUp(() => db = newTestDatabase()); tearDown(() => db.close()); testWidgets('renders the variety label, category and its lots', ( tester, ) async { final repo = newTestRepository(db); final id = await repo.addQuickVariety(label: 'Maize', category: 'Poaceae'); await repo.addLot( varietyId: id, harvestYear: 2024, quantity: const Quantity(kind: QuantityKind.cob), ); await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id)); await tester.pumpAndSettle(); expect(find.text('Maize'), findsOneWidget); // app bar title expect(find.text('Poaceae'), findsOneWidget); // category chip expect(find.text('Year 2024 · a cob'), findsOneWidget); // lot line await disposeTree(tester); }); testWidgets('editing the name updates the title', (tester) async { final repo = newTestRepository(db); final id = await repo.addQuickVariety(label: 'Old name'); await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id)); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('detail.edit'))); await tester.pumpAndSettle(); await tester.enterText( find.byKey(const Key('editVariety.name')), 'New name', ); await tester.tap(find.byKey(const Key('editVariety.save'))); await tester.pumpAndSettle(); expect(find.text('New name'), findsOneWidget); await disposeTree(tester); }); testWidgets('adding a lot shows it in the list', (tester) async { final repo = newTestRepository(db); final id = await repo.addQuickVariety(label: 'Bean'); await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id)); await tester.pumpAndSettle(); expect(find.text('No lots yet.'), findsOneWidget); await tester.tap(find.byKey(const Key('detail.addLot'))); await tester.pumpAndSettle(); await tester.enterText(find.byKey(const Key('addLot.year')), '2023'); await tester.tap(find.text('a handful')); await tester.tap(find.byKey(const Key('addLot.save'))); await tester.pumpAndSettle(); expect(find.text('Year 2023 · a handful'), findsOneWidget); await disposeTree(tester); }); testWidgets('deleting the variety shows the not-found state', (tester) async { final repo = newTestRepository(db); final id = await repo.addQuickVariety(label: 'Doomed'); await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id)); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('detail.delete'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('detail.deleteConfirm'))); await tester.pumpAndSettle(); // The variety is soft-deleted: the reactive view re-emits null. expect(find.text('This seed is no longer here.'), findsOneWidget); await disposeTree(tester); }); }