tane/apps/app_seeds/test/state/quick_add_cubit_test.dart

71 lines
2 KiB
Dart

import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/db/database.dart';
import 'package:tane/db/enums.dart';
import 'package:tane/state/quick_add_cubit.dart';
import '../support/test_support.dart';
void main() {
late AppDatabase db;
late QuickAddCubit cubit;
setUp(() {
db = newTestDatabase();
cubit = QuickAddCubit(newTestRepository(db));
});
tearDown(() async {
await cubit.close();
await db.close();
});
test(
'submitAndAddAnother with an empty label errors and saves nothing',
() async {
await cubit.submitAndAddAnother();
expect(cubit.state.showLabelError, isTrue);
expect(cubit.state.addedCount, 0);
expect(await db.select(db.varieties).get(), isEmpty);
},
);
test(
'submitAndAddAnother saves, resets the form and keeps the lot type',
() async {
cubit
..setLotType(LotType.plant)
..labelChanged('Basil')
..setQuantity(const Quantity(kind: QuantityKind.plant, count: 3));
await cubit.submitAndAddAnother();
// Saved to the DB.
final varieties = await db.select(db.varieties).get();
expect(varieties.single.label, 'Basil');
// Form reset for the next one, but the chosen lot type is kept and the
// sheet is NOT told to close (submitted stays false).
expect(cubit.state.label, '');
expect(cubit.state.quantity, isNull);
expect(cubit.state.lotType, LotType.plant);
expect(cubit.state.submitted, isFalse);
expect(cubit.state.addedCount, 1);
},
);
test('addedCount accumulates across several saves', () async {
cubit.labelChanged('One');
await cubit.submitAndAddAnother();
cubit.labelChanged('Two');
await cubit.submitAndAddAnother();
expect(cubit.state.addedCount, 2);
expect(await db.select(db.varieties).get(), hasLength(2));
});
test('submit still closes the sheet (submitted = true)', () async {
cubit.labelChanged('Once');
await cubit.submit();
expect(cubit.state.submitted, isTrue);
});
}