m3_lightmeter/test/screens/timer/bloc_timer_test.dart

78 lines
2.4 KiB
Dart
Raw Normal View History

2024-05-03 10:49:11 +00:00
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() {
2024-05-04 17:58:19 +00:00
late _MockTimerInteractor timerInteractor;
2024-05-03 10:49:11 +00:00
2024-05-04 17:58:19 +00:00
setUpAll(() {
timerInteractor = _MockTimerInteractor();
when(() => timerInteractor.isAutostartTimerEnabled).thenReturn(true);
2024-05-06 12:14:50 +00:00
when(timerInteractor.startVibration).thenAnswer((_) async {});
when(timerInteractor.endVibration).thenAnswer((_) async {});
2024-05-03 10:49:11 +00:00
});
2024-05-04 17:58:19 +00:00
blocTest<TimerBloc, TimerState>(
'Autostart',
build: () => TimerBloc(timerInteractor, const Duration(seconds: 1)),
verify: (_) {
2024-05-06 12:14:50 +00:00
verify(() => timerInteractor.startVibration()).called(1);
2024-05-04 17:58:19 +00:00
},
expect: () => [
isA<TimerResumedState>(),
],
);
2024-05-03 10:49:11 +00:00
blocTest<TimerBloc, TimerState>(
'Start -> wait till the end -> reset',
2024-05-04 17:58:19 +00:00
build: () => TimerBloc(timerInteractor, const Duration(seconds: 1)),
setUp: () {
when(() => timerInteractor.isAutostartTimerEnabled).thenReturn(false);
},
act: (bloc) {
2024-05-03 10:49:11 +00:00
bloc.add(const StartTimerEvent());
bloc.add(const TimerEndedEvent());
bloc.add(const ResetTimerEvent());
},
verify: (_) {
2024-05-06 12:14:50 +00:00
verify(() => timerInteractor.startVibration()).called(1);
verify(() => timerInteractor.endVibration()).called(1);
2024-05-03 10:49:11 +00:00
},
expect: () => [
isA<TimerResumedState>(),
isA<TimerStoppedState>(),
isA<TimerResetState>(),
],
);
blocTest<TimerBloc, TimerState>(
'Start -> stop -> start -> wait till the end',
2024-05-04 17:58:19 +00:00
build: () => TimerBloc(timerInteractor, const Duration(seconds: 1)),
setUp: () {
when(() => timerInteractor.isAutostartTimerEnabled).thenReturn(false);
},
2024-05-03 10:49:11 +00:00
act: (bloc) async {
bloc.add(const StartTimerEvent());
bloc.add(const StopTimerEvent());
bloc.add(const StartTimerEvent());
bloc.add(const TimerEndedEvent());
},
verify: (_) {
2024-05-06 12:14:50 +00:00
verify(() => timerInteractor.startVibration()).called(3);
verify(() => timerInteractor.endVibration()).called(1);
2024-05-03 10:49:11 +00:00
},
expect: () => [
isA<TimerResumedState>(),
isA<TimerStoppedState>(),
isA<TimerResumedState>(),
isA<TimerStoppedState>(),
],
);
}