-
-
Notifications
You must be signed in to change notification settings - Fork 500
/
stream_provider_test.dart
195 lines (151 loc) · 4.67 KB
/
stream_provider_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
import 'common.dart';
class ErrorBuilderMock<T> extends Mock {
ErrorBuilderMock(this.fallback);
final T fallback;
T call(BuildContext? context, Object? error) {
return super.noSuchMethod(
Invocation.method(#call, [context, error]),
returnValue: fallback,
returnValueForMissingStub: fallback,
) as T;
}
}
void main() {
testWidgets('works with MultiProvider', (tester) async {
await tester.pumpWidget(
MultiProvider(
providers: [
StreamProvider(
initialData: 0,
create: (_) => Stream.value(42),
),
],
child: TextOf<int>(),
),
);
expect(find.text('0'), findsOneWidget);
await Future.microtask(tester.pump);
expect(find.text('42'), findsOneWidget);
});
testWidgets(
'transition from stream to stream preserve state',
(tester) async {
final controller = StreamController<int>(sync: true);
final controller2 = StreamController<int>(sync: true);
await tester.pumpWidget(
StreamProvider.value(
initialData: 0,
value: controller.stream,
child: TextOf<int>(),
),
);
expect(find.text('0'), findsOneWidget);
controller.add(1);
await tester.pump();
expect(find.text('1'), findsOneWidget);
await tester.pumpWidget(
StreamProvider.value(
initialData: 0,
value: controller2.stream,
child: TextOf<int>(),
),
);
expect(find.text('1'), findsOneWidget);
controller.add(0);
await tester.pump();
expect(find.text('1'), findsOneWidget);
controller2.add(2);
await tester.pump();
expect(find.text('2'), findsOneWidget);
await tester.pump();
// ignore: unawaited_futures
controller.close();
// ignore: unawaited_futures
controller2.close();
},
);
testWidgets('throws if stream has error and catchError is missing',
(tester) async {
final controller = StreamController<int>();
await tester.pumpWidget(
StreamProvider.value(
initialData: -1,
value: controller.stream,
child: TextOf<int>(),
),
);
controller.addError(42);
await Future.microtask(tester.pump);
final dynamic exception = tester.takeException();
expect(exception, isFlutterError);
expect(exception.toString(), equals('''
An exception was throw by _ControllerStream<int> listened by
StreamProvider<int>, but no `catchError` was provided.
Exception:
42
'''));
// ignore: unawaited_futures
controller.close();
});
testWidgets('calls catchError if present and stream has error',
(tester) async {
final controller = StreamController<int>(sync: true);
final catchError = ErrorBuilderMock<int>(0);
when(catchError(any, 42)).thenReturn(42);
await tester.pumpWidget(
StreamProvider.value(
initialData: -1,
value: controller.stream,
catchError: catchError,
child: TextOf<int>(),
),
);
expect(find.text('-1'), findsOneWidget);
controller.addError(42);
await Future.microtask(tester.pump);
expect(find.text('42'), findsOneWidget);
verify(catchError(argThat(isNotNull), 42)).called(1);
verifyNoMoreInteractions(catchError);
// ignore: unawaited_futures
controller.close();
});
testWidgets('works with null', (tester) async {
await tester.pumpWidget(
StreamProvider<int>.value(
initialData: 42,
value: null,
child: TextOf<int>(),
),
);
expect(find.text('42'), findsOneWidget);
await tester.pumpWidget(Container());
});
group('StreamProvider()', () {
testWidgets('create and dispose stream with builder', (tester) async {
final stream = StreamMock<int>();
final sub = StreamSubscriptionMock<int>();
when(stream.listen(any, onError: anyNamed('onError'))).thenReturn(sub);
final builder = InitialValueBuilderMock(stream);
await tester.pumpWidget(
StreamProvider<int>(
initialData: -1,
create: builder,
child: TextOf<int>(),
),
);
verify(builder(argThat(isNotNull))).called(1);
verify(stream.listen(any, onError: anyNamed('onError'))).called(1);
verifyNoMoreInteractions(stream);
await tester.pumpWidget(Container());
verifyNoMoreInteractions(builder);
verify(sub.cancel()).called(1);
verifyNoMoreInteractions(sub);
verifyNoMoreInteractions(stream);
});
});
}