forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshare-spec.ts
More file actions
590 lines (500 loc) · 20.9 KB
/
share-spec.ts
File metadata and controls
590 lines (500 loc) · 20.9 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/** @prettier */
import { expect } from 'chai';
import {
share,
retry,
mergeMapTo,
mergeMap,
tap,
repeat,
take,
takeUntil,
takeWhile,
map,
startWith,
withLatestFrom,
} from 'rxjs/operators';
import { Observable, EMPTY, NEVER, of, Subject, defer } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
import sinon = require('sinon');
/** @test {share} */
describe('share', () => {
let rxTest: TestScheduler;
beforeEach(() => {
rxTest = new TestScheduler(observableMatcher);
});
describe('share()', () => {
it('should mirror a simple source Observable', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('--1-2---3-4--5-|');
const sourceSubs = ' ^--------------!';
const expected = ' --1-2---3-4--5-|';
const shared = source.pipe(share());
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should share a single subscription', () => {
let subscriptionCount = 0;
const obs = new Observable<never>(() => {
subscriptionCount++;
});
const source = obs.pipe(share());
expect(subscriptionCount).to.equal(0);
source.subscribe();
source.subscribe();
expect(subscriptionCount).to.equal(1);
});
it('should not change the output of the observable when error', () => {
rxTest.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot('---a--^--b--c--d--e--#');
const e1subs = ' ^--------------!';
const expected = ' ---b--c--d--e--#';
expectObservable(e1.pipe(share())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});
it('should not change the output of the observable when successful with cold observable', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const e1 = cold(' ---a--b--c--d--e--|');
const e1subs = ' ^-----------------!';
const expected = '---a--b--c--d--e--|';
expectObservable(e1.pipe(share())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});
it('should not change the output of the observable when error with cold observable', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const e1 = cold(' ---a--b--c--d--e--#');
const e1subs = ' ^-----------------!';
const expected = '---a--b--c--d--e--#';
expectObservable(e1.pipe(share())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});
it('should retry just fine', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const e1 = cold(' ---a--b--c--d--e--# ');
// prettier-ignore
const e1subs = [
' ^-----------------! ',
' ------------------^-----------------!'
];
const expected = '---a--b--c--d--e-----a--b--c--d--e--#';
expectObservable(e1.pipe(share(), retry(1))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});
it('should share the same values to multiple observers', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-3----4-|');
const sourceSubs = ' ^-----------!';
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-3----4-|';
const subscriber2 = hot('----b| ');
const expected2 = ' -----3----4-|';
const subscriber3 = hot('--------c| ');
const expected3 = ' ----------4-|';
const shared = source.pipe(share());
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should share an error from the source to multiple observers', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-3----4-#');
const sourceSubs = ' ^-----------!';
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-3----4-#';
const subscriber2 = hot('----b| ');
const expected2 = ' -----3----4-#';
const subscriber3 = hot('--------c| ');
const expected3 = ' ----------4-#';
const shared = source.pipe(share());
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should share the same values to multiple observers, but is unsubscribed explicitly and early', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-3----4-|');
const sourceSubs = ' ^--------! ';
const unsub = ' ---------! ';
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-3---- ';
const subscriber2 = hot('----b| ');
const expected2 = ' -----3---- ';
const subscriber3 = hot('--------c| ');
const expected3 = ' ---------- ';
const shared = source.pipe(share());
expectObservable(subscriber1.pipe(mergeMapTo(shared)), unsub).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared)), unsub).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared)), unsub).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should share an empty source', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('| ');
const sourceSubs = ' (^!)';
const expected = ' | ';
const shared = source.pipe(share());
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should share a never source', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('-');
const sourceSubs = ' ^';
const expected = ' -';
const shared = source.pipe(share());
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should share a throw source', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('# ');
const sourceSubs = ' (^!)';
const expected = ' # ';
const shared = source.pipe(share());
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should connect when first subscriber subscribes', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-3----4-|');
const sourceSubs = ' ---^-----------!';
const subscriber1 = hot('---a| ');
const expected1 = ' ----1-2-3----4-|';
const subscriber2 = hot('-------b| ');
const expected2 = ' --------3----4-|';
const subscriber3 = hot('-----------c| ');
const expected3 = ' -------------4-|';
const shared = source.pipe(share());
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should disconnect when last subscriber unsubscribes', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-3----4-|');
const sourceSubs = ' ---^--------! ';
const subscriber1 = hot('---a| ');
const unsub1 = ' ----------! ';
const expected1 = ' ----1-2-3-- ';
const subscriber2 = hot('-------b| ');
const unsub2 = ' ------------! ';
const expected2 = ' --------3---- ';
const shared = source.pipe(share());
expectObservable(subscriber1.pipe(mergeMapTo(shared)), unsub1).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared)), unsub2).toBe(expected2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should not break unsubscription chain when last subscriber unsubscribes', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-3----4-|');
const sourceSubs = ' ---^--------! ';
const subscriber1 = hot('---a| ');
const unsub1 = ' ----------! ';
const expected1 = ' ----1-2-3-- ';
const subscriber2 = hot('-------b| ');
const unsub2 = ' ------------! ';
const expected2 = ' --------3---- ';
const shared = source.pipe(
mergeMap((x: string) => of(x)),
share(),
mergeMap((x: string) => of(x))
);
expectObservable(subscriber1.pipe(mergeMapTo(shared)), unsub1).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared)), unsub2).toBe(expected2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should be retryable when cold source is synchronous', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold('(123#) ');
const subscribe1 = ' s ';
const expected1 = ' (123123#) ';
const subscribe2 = ' -s ';
const expected2 = ' -(123123#)';
const sourceSubs = [
' (^!) ',
' (^!) ',
' -(^!) ',
' -(^!) ',
];
const shared = source.pipe(share());
expectObservable(
hot(subscribe1).pipe(
tap(() => {
expectObservable(shared.pipe(retry(1))).toBe(expected1);
})
)
).toBe(subscribe1);
expectObservable(
hot(subscribe2).pipe(
tap(() => {
expectObservable(shared.pipe(retry(1))).toBe(expected2);
})
)
).toBe(subscribe2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should be repeatable when cold source is synchronous', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold('(123|) ');
const subscribe1 = ' s ';
const expected1 = ' (123123|) ';
const subscribe2 = ' -s ';
const expected2 = ' -(123123|)';
const sourceSubs = [
' (^!) ',
' (^!) ',
' -(^!) ',
' -(^!) ',
];
const shared = source.pipe(share());
expectObservable(
hot(subscribe1).pipe(
tap(() => {
expectObservable(shared.pipe(repeat(2))).toBe(expected1);
})
)
).toBe(subscribe1);
expectObservable(
hot(subscribe2).pipe(
tap(() => {
expectObservable(shared.pipe(repeat(2))).toBe(expected2);
})
)
).toBe(subscribe2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should be retryable', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold('-1-2-3----4-# ');
const sourceSubs = [
' ^-----------! ',
' ------------^-----------! ',
' ------------------------^-----------!',
];
const subscribe1 = ' s------------------------------------';
const expected1 = ' -1-2-3----4--1-2-3----4--1-2-3----4-#';
const subscribe2 = ' ----s--------------------------------';
const expected2 = ' -----3----4--1-2-3----4--1-2-3----4-#';
const shared = source.pipe(share());
expectObservable(
hot(subscribe1).pipe(
tap(() => {
expectObservable(shared.pipe(retry(2))).toBe(expected1);
})
)
).toBe(subscribe1);
expectObservable(
hot(subscribe2).pipe(
tap(() => {
expectObservable(shared.pipe(retry(2))).toBe(expected2);
})
)
).toBe(subscribe2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should be repeatable', () => {
rxTest.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold('-1-2-3----4-| ');
const sourceSubs = [
' ^-----------! ',
' ------------^-----------! ',
' ------------------------^-----------!',
];
const subscribe1 = ' s------------------------------------';
const expected1 = ' -1-2-3----4--1-2-3----4--1-2-3----4-|';
const subscribe2 = ' ----s--------------------------------';
const expected2 = ' -----3----4--1-2-3----4--1-2-3----4-|';
const shared = source.pipe(share());
expectObservable(
hot(subscribe1).pipe(
tap(() => {
expectObservable(shared.pipe(repeat(3))).toBe(expected1);
})
)
).toBe(subscribe1);
expectObservable(
hot(subscribe2).pipe(
tap(() => {
expectObservable(shared.pipe(repeat(3))).toBe(expected2);
})
)
).toBe(subscribe2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should not change the output of the observable when never', () => {
rxTest.run(({ expectObservable }) => {
const e1 = NEVER;
const expected = '-';
expectObservable(e1.pipe(share())).toBe(expected);
});
});
it('should not change the output of the observable when empty', () => {
rxTest.run(({ expectObservable }) => {
const e1 = EMPTY;
const expected = '|';
expectObservable(e1.pipe(share())).toBe(expected);
});
});
it('should stop listening to a synchronous observable when unsubscribed', () => {
const sideEffects: number[] = [];
const synchronousObservable = new Observable<number>((subscriber) => {
// This will check to see if the subscriber was closed on each loop
// when the unsubscribe hits (from the `take`), it should be closed
for (let i = 0; !subscriber.closed && i < 10; i++) {
sideEffects.push(i);
subscriber.next(i);
}
});
synchronousObservable.pipe(share(), take(3)).subscribe(() => {
/* noop */
});
expect(sideEffects).to.deep.equal([0, 1, 2]);
});
it('should not fail on reentrant subscription', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
// https://github.com/ReactiveX/rxjs/issues/6144
const source = cold('(123|)');
const subs = ' (^!) ';
const expected = ' (136|)';
const deferred = defer(() => shared).pipe(startWith(0));
const shared: Observable<string> = source.pipe(
withLatestFrom(deferred),
map(([a, b]) => String(Number(a) + Number(b))),
share()
);
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
});
});
describe('share(config)', () => {
it('should not reset on error if configured to do so', () => {
rxTest.run(({ hot, expectObservable, expectSubscriptions }) => {
const source = hot('---a---b---c---d---e---f----#');
const expected = ' ---a---b---c---d---e---f----#';
const sourceSubs = [
' ^----------! ',
' -----------^-----------! ',
' -----------------------^----!',
];
const result = source.pipe(
// takes a, b, c... then repeat causes it to take d, e, f
take(3),
share({
resetOnError: false,
}),
repeat()
);
expectObservable(result).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should not reset on complete if configured to do so', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('---a---b---c---# ');
const expected = ' ---a---b---c------a---b---c------a---b---|';
const sourceSubs = [
' ^--------------! ',
' ---------------^--------------! ',
' ------------------------------^----------!',
];
// Used to trigger the source to complete at a given moment.
const triggerComplete = new Subject<void>();
// just used to count how many values have made it through the share.
let count = 0;
const result = source.pipe(
takeUntil(triggerComplete),
share({
resetOnComplete: false,
}),
// Retry on any error.
retry(),
tap(() => {
if (++count === 9) {
// If we see the ninth value, complete the source this time.
triggerComplete.next();
}
})
);
expectObservable(result).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should not reset on refCount 0 if configured to do so', () => {
rxTest.run(({ hot, expectObservable, expectSubscriptions }) => {
const source = hot(' ---v---v---v---E--v---v---v---C---v----v------v---');
const expected = ' ---v---v---v------v---v---v-------v----v---- ';
const subscription = '^-------------------------------------------! ';
const sourceSubs = [
' ^--------------!',
' ---------------^--------------!',
// Note this last subscription never ends, because refCount hitting zero isn't going to reset.
' ------------------------------^-------------- ',
];
const result = source.pipe(
tap((value) => {
if (value === 'E') {
throw new Error('E');
}
}),
takeWhile((value) => value !== 'C'),
share({
resetOnRefCountZero: false,
}),
retry(),
repeat()
);
expectObservable(result, subscription).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should use the connector function provided', () => {
const connector = sinon.spy(() => new Subject());
rxTest.run(({ hot, expectObservable }) => {
const source = hot(' ---v---v---v---E--v---v---v---C---v----v--------v----v---');
const subs1 = ' ^-------------------------------------------! ';
const expResult1 = ' ---v---v---v------v---v---v-------v----v----- ';
const subs2 = ' ----------------------------------------------^---------!';
const expResult2 = ' ------------------------------------------------v----v---';
const result = source.pipe(
tap((value) => {
if (value === 'E') {
throw new Error('E');
}
}),
takeWhile((value) => value !== 'C'),
share({
connector,
}),
retry(),
repeat()
);
expectObservable(result, subs1).toBe(expResult1);
expectObservable(result, subs2).toBe(expResult2);
});
expect(connector).to.have.callCount(4);
});
});
});