forked from ReactiveX/rxjs
/
buffer-spec.ts
398 lines (359 loc) · 13.6 KB
/
buffer-spec.ts
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { buffer, mergeMap, take, window, toArray } from 'rxjs/operators';
import { EMPTY, NEVER, throwError, of, Subject, interval } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
import { expect } from 'chai';
/** @test {buffer} */
describe('Observable.prototype.buffer', () => {
let testScheduler: TestScheduler;
beforeEach(() => {
testScheduler = new TestScheduler(observableMatcher);
});
it('should emit buffers that close and reopen', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot(' -a-b-c-d-e-f-g-h-i-|');
const b = hot(' -----B-----B-----B-|');
const expected = '-----x-----y-----z-(F|)';
const expectedValues = {
x: ['a', 'b', 'c'],
y: ['d', 'e', 'f'],
z: ['g', 'h', 'i'],
F: [],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues);
});
});
it('should emit a final buffer if the closingNotifier is already complete', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot(' -a-b-c-d-e-f-g-h-i-|');
const b = hot(' -----B-----B--|');
const expected = '-----x-----y-------(F|)';
const expectedValues = {
x: ['a', 'b', 'c'],
y: ['d', 'e', 'f'],
F: ['g', 'h', 'i'],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues);
});
});
it('should emit all buffered values if the source completes before the closingNotifier does', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const source = hot('---^---a---b---c---d---e--f----|');
const sourceSubs = ' ^---------------------------!';
const closer = hot('---^-------------B----------------');
const closerSubs = ' ^---------------------------!';
const expected = ' --------------x-------------(F|)';
const result = source.pipe(buffer(closer));
const expectedValues = {
x: ['a', 'b', 'c'],
F: ['d', 'e', 'f'],
};
expectObservable(result).toBe(expected, expectedValues);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
expectSubscriptions(closer.subscriptions).toBe(closerSubs);
});
});
it('should work with empty and empty selector', () => {
testScheduler.run(({ expectObservable }) => {
const a = EMPTY;
const b = EMPTY;
const expected = '(F|)';
expectObservable(a.pipe(buffer(b))).toBe(expected, { F: [] });
});
});
it('should work with empty and non-empty selector', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = EMPTY;
const b = hot('-----a-----');
const expected = '(F|)';
expectObservable(a.pipe(buffer(b))).toBe(expected, { F: [] });
});
});
it('should work with non-empty and empty selector', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
const b = EMPTY;
const expected = ' --------------------------------(F|)';
expectObservable(a.pipe(buffer(b))).toBe(expected, {
F: ['3', '4', '5', '6', '7', '8', '9', '0'],
});
});
});
it('should work with never and never selector', () => {
testScheduler.run(({ expectObservable }) => {
const a = NEVER;
const b = NEVER;
const expected = '-';
expectObservable(a.pipe(buffer(b))).toBe(expected);
});
});
it('should work with never and empty selector', () => {
testScheduler.run(({ expectObservable }) => {
const a = NEVER;
const b = EMPTY;
const expected = '-';
expectObservable(a.pipe(buffer(b))).toBe(expected);
});
});
it('should work with empty and never selector', () => {
testScheduler.run(({ expectObservable }) => {
const a = EMPTY;
const b = NEVER;
const expected = '(F|)';
expectObservable(a.pipe(buffer(b))).toBe(expected, { F: [] });
});
});
it('should work with non-empty and throw selector', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot('---^--a--');
const b = throwError(() => new Error('too bad'));
const expected = '#';
expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad'));
});
});
it('should work with throw and non-empty selector', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = throwError(() => new Error('too bad'));
const b = hot('---^--a--');
const expected = '#';
expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad'));
});
});
it('should work with error', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot('---^-------#', undefined, new Error('too bad'));
const b = hot('---^--------');
const expected = '--------#';
expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad'));
});
});
it('should work with error and non-empty selector', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot('---^-------#', undefined, new Error('too bad'));
const b = hot('---^---a----');
const expected = '----a---#';
expectObservable(a.pipe(buffer(b))).toBe(expected, { a: [] }, new Error('too bad'));
});
});
it('should work with selector', () => {
// Buffer Boundaries Simple (RxJS 4)
testScheduler.run(({ hot, expectObservable }) => {
const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
const b = hot('--------^--a-------b---cd---------e---f---|');
const expected = ' ---a-------b---cd---------e---f-(F|)';
const expectedValues = {
a: ['3'],
b: ['4', '5'],
c: ['6'],
d: [] as string[],
e: ['7', '8', '9'],
f: ['0'],
F: [],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues);
});
});
it('should work with selector completed', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
const subs = ' ^-------------------------------!';
const b = hot('--------^--a-------b---cd| ');
const expected = ' ---a-------b---cd---------------(F|)';
const expectedValues = {
a: ['3'],
b: ['4', '5'],
c: ['6'],
d: [] as string[],
F: ['7', '8', '9', '0'],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues);
expectSubscriptions(a.subscriptions).toBe(subs);
});
});
it('should allow unsubscribing the result Observable early', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
const unsub = ' --------------! ';
const subs = ' ^-------------! ';
const b = hot('--------^--a-------b---cd| ');
const expected = ' ---a-------b--- ';
const expectedValues = {
a: ['3'],
b: ['4', '5'],
};
expectObservable(a.pipe(buffer(b)), unsub).toBe(expected, expectedValues);
expectSubscriptions(a.subscriptions).toBe(subs);
});
});
it('should not break unsubscription chains when unsubscribed explicitly', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
const subs = ' ^-------------! ';
const b = hot('--------^--a-------b---cd| ');
const expected = ' ---a-------b--- ';
const unsub = ' --------------! ';
const expectedValues = {
a: ['3'],
b: ['4', '5'],
};
const result = a.pipe(
mergeMap((x: any) => of(x)),
buffer(b),
mergeMap((x: any) => of(x))
);
expectObservable(result, unsub).toBe(expected, expectedValues);
expectSubscriptions(a.subscriptions).toBe(subs);
});
});
it('should work with non-empty and selector error', () => {
// Buffer Boundaries onErrorSource (RxJS 4)
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const a = hot('--1--2--^--3-----#', { '3': 3 }, new Error('too bad'));
const subs = ' ^--------!';
const b = hot('--------^--a--b---');
const expected = ' ---a--b--#';
const expectedValues = {
a: [3],
b: [] as string[],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues, new Error('too bad'));
expectSubscriptions(a.subscriptions).toBe(subs);
});
});
it('should work with non-empty and empty selector error', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
const b = hot('--------^----------------#', undefined, new Error('too bad'));
const expected = ' -----------------#';
expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad'));
});
});
it('should work with non-empty and selector error', () => {
// Buffer Boundaries onErrorBoundaries (RxJS 4)
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const obj = { a: true, b: true, c: true };
const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
const subs = ' ^----------------!';
const b = hot('--------^--a-------b---c-#', obj, new Error('too bad'));
const expected = ' ---a-------b---c-#';
const expectedValues = {
a: ['3'],
b: ['4', '5'],
c: ['6'],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues, new Error('too bad'));
expectSubscriptions(a.subscriptions).toBe(subs);
});
});
it('should unsubscribe notifier when source unsubscribed', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
const unsub = ' --------------! ';
const subs = ' ^-------------! ';
const b = hot('--------^--a-------b---cd| ');
const bsubs = ' ^-------------! ';
const expected = ' ---a-------b--- ';
const expectedValues = {
a: ['3'],
b: ['4', '5'],
};
expectObservable(a.pipe(buffer(b)), unsub).toBe(expected, expectedValues);
expectSubscriptions(a.subscriptions).toBe(subs);
expectSubscriptions(b.subscriptions).toBe(bsubs);
});
});
it('should unsubscribe notifier when source unsubscribed', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const a = hot(' -a-b-c-d-e-f-g-h-i-|');
const b = hot(' -----1-----2-----3-|');
const bsubs = ' ^----!';
const expected = '-----(x|)';
const expectedValues = {
x: ['a', 'b', 'c'],
};
expectObservable(a.pipe(buffer(b), take(1))).toBe(expected, expectedValues);
expectSubscriptions(b.subscriptions).toBe(bsubs);
});
});
it('should emit properly with an observable using itself as a notifier', () => {
const results: any[] = [];
const subject = new Subject<number>();
subject.pipe(buffer(subject)).subscribe({
next: (value) => results.push(value),
complete: () => results.push('complete'),
});
subject.next(1);
expect(results).to.deep.equal([[1]]);
subject.next(2);
expect(results).to.deep.equal([[1], [2]]);
subject.complete();
expect(results).to.deep.equal([[1], [2], [], 'complete']);
});
it('should buffer when Promise resolves', (done) => {
const e1 = interval(3).pipe(take(5));
const expected = [
[0, 1],
[2, 3, 4],
];
e1.pipe(buffer(new Promise<void>((resolve) => setTimeout(() => resolve(), 8)))).subscribe({
next: (x) => {
expect(x).to.deep.equal(expected.shift());
},
error: () => done(new Error('should not be called')),
complete: () => {
expect(expected.length).to.equal(0);
done();
},
});
});
it('should raise error when Promise rejects', (done) => {
const e1 = interval(1).pipe(take(5));
const error = new Error('err');
e1.pipe(buffer(Promise.reject(error))).subscribe({
next: () => {
done(new Error('should not be called'));
},
error: (err: any) => {
expect(err).to.be.an('error');
done();
},
complete: () => {
done(new Error('should not be called'));
},
});
});
describe('equivalence with the window operator', () => {
const cases = [
{
source: ' -a-b-c-d-e-f-g-h-i-|',
notifier: ' -----B-----B-----B-|',
},
{
source: ' -a-b-c-d-e-f-g-h-i-|',
notifier: ' -----B-----B--| ',
},
{
source: ' -a-b-c-d-e---------|',
notifier: ' -----B-----B-----B-|',
},
{
source: ' -a-b-c-d-e-f-g-h-i-|',
notifier: ' -------------------|',
},
];
cases.forEach(({ source, notifier }, index) => {
it(`should be equivalent for case ${index}`, () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot(source);
const b = hot(notifier);
expectObservable(a.pipe(buffer(b))).toEqual(
a.pipe(
window(b),
mergeMap((w) => w.pipe(toArray()))
)
);
});
});
});
});
});