Skip to content

Commit d038a35

Browse files
authored
fix(bloc_test)!: propagate lifecycle events to registered BlocObserver (#4688)
1 parent ab1f07c commit d038a35

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed

packages/bloc_test/lib/src/bloc_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,56 @@ class _TestBlocObserver extends BlocObserver {
260260
final BlocObserver _localObserver;
261261
final void Function(Object error) _onError;
262262

263+
@override
264+
void onCreate(BlocBase<dynamic> bloc) {
265+
_localObserver.onCreate(bloc);
266+
super.onCreate(bloc);
267+
}
268+
269+
@override
270+
void onEvent(Bloc<dynamic, dynamic> bloc, Object? event) {
271+
_localObserver.onEvent(bloc, event);
272+
super.onEvent(bloc, event);
273+
}
274+
275+
@override
276+
void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) {
277+
_localObserver.onChange(bloc, change);
278+
super.onChange(bloc, change);
279+
}
280+
281+
@override
282+
void onTransition(
283+
Bloc<dynamic, dynamic> bloc,
284+
Transition<dynamic, dynamic> transition,
285+
) {
286+
_localObserver.onTransition(bloc, transition);
287+
super.onTransition(bloc, transition);
288+
}
289+
263290
@override
264291
void onError(BlocBase<dynamic> bloc, Object error, StackTrace stackTrace) {
265292
_localObserver.onError(bloc, error, stackTrace);
266293
_onError(error);
267294
super.onError(bloc, error, stackTrace);
268295
}
296+
297+
@override
298+
void onDone(
299+
Bloc<dynamic, dynamic> bloc,
300+
Object? event, [
301+
Object? error,
302+
StackTrace? stackTrace,
303+
]) {
304+
_localObserver.onDone(bloc, event, error, stackTrace);
305+
super.onDone(bloc, event, error, stackTrace);
306+
}
307+
308+
@override
309+
void onClose(BlocBase<dynamic> bloc) {
310+
_localObserver.onClose(bloc);
311+
super.onClose(bloc);
312+
}
269313
}
270314

271315
String _diff({required dynamic expected, required dynamic actual}) {

packages/bloc_test/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ environment:
1212
sdk: ">=2.14.0 <4.0.0"
1313

1414
dependencies:
15-
bloc: ^9.0.0
15+
bloc: ^9.1.0
1616
diff_match_patch: ^0.4.1
1717
meta: ^1.3.0
1818
mocktail: ^1.0.0
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import 'package:bloc/bloc.dart';
2+
import 'package:bloc_test/bloc_test.dart';
3+
import 'package:mocktail/mocktail.dart';
4+
import 'package:test/test.dart';
5+
6+
import 'blocs/counter_bloc.dart';
7+
import 'blocs/exception_counter_bloc.dart';
8+
9+
class _MockBlocObserver extends Mock implements BlocObserver {}
10+
11+
void main() {
12+
group('BlocObserver', () {
13+
late BlocObserver blocObserver;
14+
15+
setUp(() {
16+
blocObserver = _MockBlocObserver();
17+
final previousObserver = Bloc.observer;
18+
addTearDown(() => Bloc.observer = previousObserver);
19+
Bloc.observer = blocObserver;
20+
});
21+
22+
blocTest<CounterBloc, int>(
23+
'calls onCreate',
24+
build: () => CounterBloc(),
25+
verify: (bloc) {
26+
// ignore: invalid_use_of_protected_member
27+
verify(() => blocObserver.onCreate(bloc)).called(1);
28+
},
29+
);
30+
31+
blocTest<CounterBloc, int>(
32+
'calls onEvent',
33+
build: () => CounterBloc(),
34+
act: (bloc) => bloc.add(CounterEvent.increment),
35+
verify: (bloc) {
36+
verify(
37+
// ignore: invalid_use_of_protected_member
38+
() => blocObserver.onEvent(bloc, CounterEvent.increment),
39+
).called(1);
40+
},
41+
);
42+
43+
blocTest<CounterBloc, int>(
44+
'calls onChange',
45+
build: () => CounterBloc(),
46+
act: (bloc) => bloc.add(CounterEvent.increment),
47+
verify: (bloc) {
48+
const change = Change<int>(currentState: 0, nextState: 1);
49+
// ignore: invalid_use_of_protected_member
50+
verify(() => blocObserver.onChange(bloc, change)).called(1);
51+
},
52+
);
53+
54+
blocTest<CounterBloc, int>(
55+
'calls onTransition',
56+
build: () => CounterBloc(),
57+
act: (bloc) => bloc.add(CounterEvent.increment),
58+
verify: (bloc) {
59+
const transition = Transition<CounterEvent, int>(
60+
event: CounterEvent.increment,
61+
currentState: 0,
62+
nextState: 1,
63+
);
64+
// ignore: invalid_use_of_protected_member
65+
verify(() => blocObserver.onTransition(bloc, transition)).called(1);
66+
},
67+
);
68+
69+
blocTest<ExceptionCounterBloc, int>(
70+
'calls onError',
71+
build: () => ExceptionCounterBloc(),
72+
act: (bloc) => bloc.add(CounterEvent.increment),
73+
setUp: () {
74+
registerFallbackValue(StackTrace.empty);
75+
},
76+
verify: (bloc) {
77+
verify(
78+
// ignore: invalid_use_of_protected_member
79+
() => blocObserver.onError(
80+
bloc,
81+
ExceptionCounterBlocException(),
82+
any(),
83+
),
84+
).called(1);
85+
},
86+
errors: () => containsOnce(isA<ExceptionCounterBlocException>()),
87+
);
88+
89+
blocTest<CounterBloc, int>(
90+
'calls onDone',
91+
build: () => CounterBloc(),
92+
act: (bloc) => bloc.add(CounterEvent.increment),
93+
verify: (bloc) {
94+
verify(
95+
// ignore: invalid_use_of_protected_member
96+
() => blocObserver.onDone(bloc, CounterEvent.increment),
97+
).called(1);
98+
},
99+
);
100+
101+
blocTest<CounterBloc, int>(
102+
'calls onClose',
103+
build: () => CounterBloc(),
104+
verify: (bloc) {
105+
// ignore: invalid_use_of_protected_member
106+
verify(() => blocObserver.onClose(bloc)).called(1);
107+
},
108+
);
109+
});
110+
}

0 commit comments

Comments
 (0)