-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamgetcontroller_test.dart
281 lines (235 loc) · 8.97 KB
/
streamgetcontroller_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import 'package:flutter_test/flutter_test.dart';
import 'package:get_smart/get_smart.dart';
Stream<int> numberStream(
int dataBack, {
required bool fail,
int? delay,
}) async* {
if (fail) throw Exception('numberStream failed');
if (delay != null) await Future.delayed(Duration(milliseconds: delay));
yield dataBack;
}
Stream<String> textStream(
String dataBack, {
required bool fail,
int? delay,
}) async* {
if (fail) throw Exception('textStream failed');
if (delay != null) await Future.delayed(Duration(milliseconds: delay));
yield dataBack;
}
class TestStreamGetController extends StreamGetController<int> {
final bool fail;
final int delay;
TestStreamGetController({this.fail = false, this.delay = 0});
int? loadedData;
@override
get stream => numberStream(1, fail: fail, delay: delay);
@override
void onData(int? data) {
loadedData = data;
}
}
const String _numberStream = 'numberStream';
const String _stringStream = 'stringStream';
class TestMultipleStreamGetController extends MultipleStreamGetController {
final bool failOne;
final int delay;
TestMultipleStreamGetController({this.failOne = false, this.delay = 0});
int? loadedData;
int cancelledCalls = 0;
@override
Map<Object, StreamData> get streamsMap => {
_numberStream: StreamData(numberStream(
5,
fail: failOne,
delay: delay,
)),
_stringStream: StreamData(textStream(
"five",
fail: false,
delay: delay,
)),
};
@override
void onCancel(Object key) {
cancelledCalls++;
}
}
class TestMultipleStreamGetControllerWithOverrides
extends MultipleStreamGetController {
TestMultipleStreamGetControllerWithOverrides();
int? loadedData;
@override
Map<Object, StreamData> get streamsMap => {
_numberStream: StreamData(
numberStream(5, fail: false, delay: 0),
onData: _loadData,
)
};
void _loadData(data) {
loadedData = data;
}
}
void main() async {
group('StreamGetController', () {
test('When stream data is fetched data should be set and ready', () async {
var streamGetController = TestStreamGetController();
streamGetController.initialise();
await Future.delayed(1.milliseconds);
expect(streamGetController.data, 1);
expect(streamGetController.dataReady, true);
});
test('When stream lifecycle events are overriden they recieve correct data',
() async {
var streamGetController = TestStreamGetController();
streamGetController.initialise();
await Future.delayed(1.milliseconds);
expect(streamGetController.loadedData, 1);
});
test('When a stream fails it should indicate there\'s an error and no data',
() async {
var streamGetController = TestStreamGetController(fail: true);
streamGetController.initialise();
await Future.delayed(1.milliseconds);
expect(streamGetController.hasError, true);
expect(streamGetController.data, null,
reason: 'No data should be set when there\'s a failure.');
expect(streamGetController.dataReady, false);
});
test('Before a stream returns it should indicate not ready', () async {
var streamGetController = TestStreamGetController(delay: 1000);
streamGetController.initialise();
await Future.delayed(1.milliseconds);
expect(streamGetController.dataReady, false);
});
/* test('When a stream returns it should notifyListeners', () async {
var streamGetController = TestStreamGetController(delay: 50);
var listenersCalled = false;
streamGetController.addListener(() {
listenersCalled = true;
});
streamGetController.initialise();
await Future.delayed(Duration(milliseconds: 100));
expect(listenersCalled, true);
});*/
group('Data Source Change', () {
test(
'notifySourceChanged - When called should unsubscribe from original source',
() {
var streamGetController = TestStreamGetController(delay: 1000);
streamGetController.initialise();
streamGetController.notifySourceChanged();
expect(streamGetController.streamSubscription, null);
});
test(
'notifySourceChanged - When called and clearOldData is false should leave old data',
() async {
var streamGetController = TestStreamGetController(delay: 10);
streamGetController.initialise();
await Future.delayed(const Duration(milliseconds: 20));
streamGetController.notifySourceChanged();
expect(streamGetController.data, 1);
});
test(
'notifySourceChanged - When called and clearOldData is true should remove old data',
() async {
var streamGetController = TestStreamGetController(delay: 10);
streamGetController.initialise();
await Future.delayed(const Duration(milliseconds: 20));
streamGetController.notifySourceChanged(clearOldData: true);
expect(streamGetController.data, null);
});
});
});
group('MultipleStreamGetController', () {
test(
'When running multiple streams the associated key should hold the value when data is fetched',
() async {
var streamGetController = TestMultipleStreamGetController();
streamGetController.initialise();
await Future.delayed(4.milliseconds);
expect(streamGetController.dataMap[_numberStream], 5);
expect(streamGetController.dataMap[_stringStream], 'five');
});
test(
'When one of multiple streams fail only the failing one should have an error',
() async {
var streamGetController = TestMultipleStreamGetController(failOne: true);
streamGetController.initialise();
await Future.delayed(1.milliseconds);
expect(streamGetController.hasErrorFor(_numberStream), true);
// Make sure we only have 1 error
// expect(streamGetController.errorMap.values.where((v) => v == true).length, 1);
});
test(
'When one of multiple streams fail the passed one should have data and failing one not',
() async {
var streamGetController = TestMultipleStreamGetController(failOne: true);
streamGetController.initialise();
await Future.delayed(1.milliseconds);
expect(streamGetController.dataReady(_numberStream), false);
// Delay the first lifecycle can complete
await Future.delayed(1.milliseconds);
expect(streamGetController.dataReady(_stringStream), true);
});
test('When one onData is augmented the data will change', () async {
var streamGetController = TestMultipleStreamGetControllerWithOverrides();
streamGetController.initialise();
await Future.delayed(1.milliseconds);
expect(streamGetController.loadedData, 5);
});
/* test('When a stream returns it should notifyListeners', () async {
var streamGetController = TestMultipleStreamGetController(delay: 50);
var listenersCalled = false;
streamGetController.addListener(() {
listenersCalled = true;
});
streamGetController.initialise();
await Future.delayed(Duration(milliseconds: 100));
expect(listenersCalled, true);
});*/
test(
'When a stream is initialised should have a subscription for the given key',
() async {
var streamGetController = TestMultipleStreamGetController();
streamGetController.initialise();
expect(streamGetController.getSubscriptionForKey(_numberStream) != null,
true);
});
test('When disposed, should call onCancel for both streams', () async {
var streamGetController = TestMultipleStreamGetController();
streamGetController.initialise();
streamGetController.onClose();
expect(streamGetController.cancelledCalls, 2);
});
});
group('Data Source Change', () {
test(
'notifySourceChanged - When called should unsubscribe from original sources',
() {
var streamGetController = TestMultipleStreamGetController(delay: 50);
streamGetController.initialise();
streamGetController.notifySourceChanged();
expect(streamGetController.streamsSubscriptions!.length, 0);
});
test(
'notifySourceChanged - When called and clearOldData is false should leave old data',
() async {
var streamGetController = TestMultipleStreamGetController(delay: 10);
streamGetController.initialise();
await Future.delayed(const Duration(milliseconds: 20));
streamGetController.notifySourceChanged();
expect(streamGetController.dataMap[_numberStream], 5);
});
test(
'notifySourceChanged - When called and clearOldData is true should remove old data',
() async {
var streamGetController = TestMultipleStreamGetController(delay: 10);
streamGetController.initialise();
await Future.delayed(const Duration(milliseconds: 20));
streamGetController.notifySourceChanged(clearOldData: true);
expect(streamGetController.dataMap[_numberStream], null);
});
});
}