tane/apps/app_seeds/test/ui/variety_detail_screen_test.dart
vjrj 5d4053157f feat(inventory): month/year harvest date picker (schema v3)
Replace the free-text harvest year with a themed month/year picker:
a tappable field opens a green-styled dialog with month and year grids
(navigable, paginated years), month optional. Lots now read e.g.
"September 2021" or just "Year 2021".

- schema v3: add nullable Lot.harvest_month (1..12) + migration + tests
- repo/cubit propagate harvestMonth; localized month names (en/es) via slang
2026-07-08 12:09:38 +02:00

332 lines
10 KiB
Dart

import 'package:commons_core/commons_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/species_repository.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,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
expect(find.text('Maize'), findsOneWidget); // app bar title
expect(find.text('Poaceae'), findsOneWidget); // category chip
expect(find.text('Year 2024 · 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,
species: newTestSpeciesRepository(db),
),
);
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,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
expect(find.text('No lots yet.'), findsOneWidget);
await tester.tap(find.byKey(const Key('detail.addLot')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('addLot.year'))); // open the picker
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('harvestPicker.yearToggle')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('harvestPicker.year.2023')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('harvestPicker.ok')));
await tester.pumpAndSettle();
await tester.tap(find.text('spoon')); // pick the unit from the scale
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('quantity.count')), '2');
await tester.tap(find.byKey(const Key('addLot.save')));
await tester.pumpAndSettle();
expect(find.text('Year 2023 · 2 spoons'), 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,
species: newTestSpeciesRepository(db),
),
);
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);
});
testWidgets(
'linking a species from the edit sheet shows its scientific name',
(tester) async {
final repo = newTestRepository(db);
final species = newTestSpeciesRepository(db);
await species.seedBundled(const [
SpeciesSeed(
scientificName: 'Solanum lycopersicum',
family: 'Solanaceae',
commonNames: {
'en': ['Tomato'],
},
),
]);
final id = await repo.addQuickVariety(label: "Grandma's tomato");
await tester.pumpWidget(
wrapDetail(repository: repo, varietyId: id, species: species),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('detail.edit')));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('editVariety.species')),
'toma',
);
await tester.pumpAndSettle();
await tester.tap(find.text('Tomato (Solanum lycopersicum)'));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('editVariety.save')));
await tester.pumpAndSettle();
// Detail now shows the scientific name and the family-prefilled category.
expect(find.text('Solanum lycopersicum'), findsOneWidget);
expect(find.text('Solanaceae'), findsOneWidget);
await disposeTree(tester);
},
);
testWidgets('recording a germination test shows the rate badge on the lot', (
tester,
) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Maize');
await repo.addLot(
varietyId: id,
harvestYear: 2024,
quantity: const Quantity(kind: QuantityKind.cob),
);
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byIcon(Icons.grass_outlined).first);
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('germination.germinated')),
'18',
);
await tester.enterText(
find.byKey(const Key('germination.sampleSize')),
'20',
);
await tester.tap(find.byKey(const Key('germination.save')));
await tester.pumpAndSettle();
expect(find.text('90%'), findsOneWidget); // germination badge on the lot
await disposeTree(tester);
});
testWidgets('editing a lot updates its line', (tester) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Maize');
await repo.addLot(
varietyId: id,
harvestYear: 2020,
quantity: const Quantity(kind: QuantityKind.packet, count: 1),
);
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Year 2020 · 1 packet')); // open the lot
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('addLot.year'))); // open the picker
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('harvestPicker.yearToggle')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('harvestPicker.year.2024')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('harvestPicker.ok')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('addLot.save')));
await tester.pumpAndSettle();
expect(find.text('Year 2024 · 1 packet'), findsOneWidget);
await disposeTree(tester);
});
testWidgets('picking a harvest month shows month + year on the lot', (
tester,
) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Maize');
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('detail.addLot')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('addLot.year'))); // open the picker
await tester.pumpAndSettle();
// Pick year 2022…
await tester.tap(find.byKey(const Key('harvestPicker.yearToggle')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('harvestPicker.year.2022')));
await tester.pumpAndSettle();
// …then September (month 9).
await tester.tap(find.byKey(const Key('harvestPicker.month.9')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('harvestPicker.ok')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('addLot.save')));
await tester.pumpAndSettle();
expect(find.text('September 2022'), findsOneWidget);
await disposeTree(tester);
});
testWidgets('deleting a lot removes it', (tester) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Maize');
await repo.addLot(varietyId: id, harvestYear: 2020);
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
await tester.tap(find.textContaining('Year 2020')); // open the lot
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('lot.delete')));
await tester.pumpAndSettle();
expect(find.text('No lots yet.'), findsOneWidget);
await disposeTree(tester);
});
testWidgets('adding and removing a vernacular name', (tester) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Maize');
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('name.add')));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('name.field')), 'Maíz');
await tester.tap(find.byKey(const Key('name.save')));
await tester.pumpAndSettle();
expect(find.text('Maíz'), findsOneWidget);
await tester.tap(find.byIcon(Icons.close)); // the chip's delete icon
await tester.pumpAndSettle();
expect(find.text('Maíz'), findsNothing);
await disposeTree(tester);
});
}