From 90a4b9a056c5844aa9b24e985ea02ac1484cce76 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Fri, 3 May 2024 12:49:11 +0200 Subject: [PATCH] added `TimerBloc` test --- test/screens/timer/bloc_timer_test.dart | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/screens/timer/bloc_timer_test.dart diff --git a/test/screens/timer/bloc_timer_test.dart b/test/screens/timer/bloc_timer_test.dart new file mode 100644 index 0000000..80c7349 --- /dev/null +++ b/test/screens/timer/bloc_timer_test.dart @@ -0,0 +1,66 @@ +import 'package:bloc_test/bloc_test.dart'; +import 'package:lightmeter/interactors/timer_interactor.dart'; +import 'package:lightmeter/screens/timer/bloc_timer.dart'; +import 'package:lightmeter/screens/timer/event_timer.dart'; +import 'package:lightmeter/screens/timer/state_timer.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:test/test.dart'; + +class _MockTimerInteractor extends Mock implements TimerInteractor {} + +void main() { + late _MockTimerInteractor meteringInteractor; + late TimerBloc bloc; + + setUp(() { + meteringInteractor = _MockTimerInteractor(); + when(meteringInteractor.quickVibration).thenAnswer((_) async {}); + when(meteringInteractor.responseVibration).thenAnswer((_) async {}); + + bloc = TimerBloc(meteringInteractor, const Duration(seconds: 1)); + }); + + tearDown(() { + bloc.close(); + }); + + blocTest( + 'Start -> wait till the end -> reset', + build: () => bloc, + act: (bloc) async { + bloc.add(const StartTimerEvent()); + bloc.add(const TimerEndedEvent()); + bloc.add(const ResetTimerEvent()); + }, + verify: (_) { + verify(() => meteringInteractor.quickVibration()).called(1); + verify(() => meteringInteractor.responseVibration()).called(1); + }, + expect: () => [ + isA(), + isA(), + isA(), + ], + ); + + blocTest( + 'Start -> stop -> start -> wait till the end', + build: () => bloc, + act: (bloc) async { + bloc.add(const StartTimerEvent()); + bloc.add(const StopTimerEvent()); + bloc.add(const StartTimerEvent()); + bloc.add(const TimerEndedEvent()); + }, + verify: (_) { + verify(() => meteringInteractor.quickVibration()).called(3); + verify(() => meteringInteractor.responseVibration()).called(1); + }, + expect: () => [ + isA(), + isA(), + isA(), + isA(), + ], + ); +}