split EquipmentProfileListener tests

This commit is contained in:
Vadim 2023-11-01 21:15:55 +01:00
parent 6507df2ad6
commit a41add2f97

View file

@ -12,21 +12,16 @@ class _MockIAPStorageService extends Mock implements IAPStorageService {}
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
late _MockIAPStorageService storageService; final storageService = _MockIAPStorageService();
final equipmentProfileProviderKey = GlobalKey<EquipmentProfileProviderState>(); final equipmentProfileProviderKey = GlobalKey<EquipmentProfileProviderState>();
final onDidChangeDependencies = MockValueChanged<EquipmentProfile>();
setUpAll(() {
storageService = _MockIAPStorageService();
});
tearDown(() { tearDown(() {
reset(onDidChangeDependencies);
reset(storageService); reset(storageService);
}); });
Future<void> pumpTestWidget( Future<void> pumpTestWidget(WidgetTester tester) async {
WidgetTester tester,
ValueChanged<EquipmentProfile> onDidChangeDependencies,
) async {
await tester.pumpWidget( await tester.pumpWidget(
IAPProducts( IAPProducts(
products: [ products: [
@ -40,7 +35,7 @@ void main() {
storageService: storageService, storageService: storageService,
child: MaterialApp( child: MaterialApp(
home: EquipmentProfileListener( home: EquipmentProfileListener(
onDidChangeDependencies: onDidChangeDependencies, onDidChangeDependencies: onDidChangeDependencies.onChanged,
child: Builder(builder: (context) => Text(EquipmentProfiles.selectedOf(context).name)), child: Builder(builder: (context) => Text(EquipmentProfiles.selectedOf(context).name)),
), ),
), ),
@ -50,20 +45,25 @@ void main() {
} }
testWidgets( testWidgets(
'Listen to equipment profile selection and edit', 'Trigger `onDidChangeDependencies` by selecting a new profile',
(tester) async { (tester) async {
when(() => storageService.equipmentProfiles).thenReturn(List.from(_customProfiles)); when(() => storageService.equipmentProfiles).thenReturn(List.from(_customProfiles));
when(() => storageService.selectedEquipmentProfileId).thenReturn(''); when(() => storageService.selectedEquipmentProfileId).thenReturn('');
final onDidChangeDependencies = MockValueChanged<EquipmentProfile>(); await pumpTestWidget(tester);
await pumpTestWidget(tester, onDidChangeDependencies.onChanged);
/// Verify that selecting a new profile triggers the callback
equipmentProfileProviderKey.currentState!.setProfile(_customProfiles[0]); equipmentProfileProviderKey.currentState!.setProfile(_customProfiles[0]);
await tester.pump(); await tester.pump();
verify(() => onDidChangeDependencies.onChanged(_customProfiles[0])).called(1); verify(() => onDidChangeDependencies.onChanged(_customProfiles[0])).called(1);
},
);
testWidgets(
'Trigger `onDidChangeDependencies` by updating the selected profile',
(tester) async {
when(() => storageService.equipmentProfiles).thenReturn(List.from(_customProfiles));
when(() => storageService.selectedEquipmentProfileId).thenReturn(_customProfiles[0].id);
await pumpTestWidget(tester);
/// Verify that updating the current profile triggers the callback
final updatedProfile1 = _customProfiles[0].copyWith(name: 'Test 1 updated'); final updatedProfile1 = _customProfiles[0].copyWith(name: 'Test 1 updated');
equipmentProfileProviderKey.currentState!.updateProfile(updatedProfile1); equipmentProfileProviderKey.currentState!.updateProfile(updatedProfile1);
await tester.pump(); await tester.pump();
@ -76,6 +76,20 @@ void main() {
verifyNever(() => onDidChangeDependencies.onChanged(updatedProfile2)); verifyNever(() => onDidChangeDependencies.onChanged(updatedProfile2));
}, },
); );
testWidgets(
"Don't trigger `onDidChangeDependencies` by updating the unselected profile",
(tester) async {
when(() => storageService.equipmentProfiles).thenReturn(List.from(_customProfiles));
when(() => storageService.selectedEquipmentProfileId).thenReturn(_customProfiles[0].id);
await pumpTestWidget(tester);
final updatedProfile2 = _customProfiles[1].copyWith(name: 'Test 2 updated');
equipmentProfileProviderKey.currentState!.updateProfile(updatedProfile2);
await tester.pump();
verifyNever(() => onDidChangeDependencies.onChanged(updatedProfile2));
},
);
} }
final List<EquipmentProfile> _customProfiles = [ final List<EquipmentProfile> _customProfiles = [