forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergeMap-spec.ts
801 lines (668 loc) · 32.9 KB
/
mergeMap-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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
import { expect } from 'chai';
import { mergeMap, map } from 'rxjs/operators';
import { asapScheduler, defer, Observable, from, of, timer } from 'rxjs';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
declare const type: Function;
declare const asDiagram: Function;
/** @test {mergeMap} */
describe('mergeMap', () => {
asDiagram('mergeMap(i => 10*i\u2014\u201410*i\u2014\u201410*i\u2014| )')
('should map-and-flatten each item to an Observable', () => {
const e1 = hot('--1-----3--5-------|');
const e1subs = '^ !';
const e2 = cold('x-x-x| ', {x: 10});
const expected = '--x-x-x-y-yzyz-z---|';
const values = {x: 10, y: 30, z: 50};
const result = e1.pipe(mergeMap(x => e2.map(i => i * +x)));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should support the deprecated resultSelector', () => {
const results: Array<number[]> = [];
of(1, 2, 3).pipe(
mergeMap(
x => of(x, x + 1, x + 2),
(a, b, i, ii) => [a, b, i, ii]
)
)
.subscribe({
next (value) {
results.push(value);
},
error(err) {
throw err;
},
complete() {
expect(results).to.deep.equal([
[1, 1, 0, 0],
[1, 2, 0, 1],
[1, 3, 0, 2],
[2, 2, 1, 0],
[2, 3, 1, 1],
[2, 4, 1, 2],
[3, 3, 2, 0],
[3, 4, 2, 1],
[3, 5, 2, 2],
]);
}
});
});
it('should support a void resultSelector (still deprecated)', () => {
const results: number[] = [];
of(1, 2, 3).pipe(
mergeMap(
x => of(x, x + 1, x + 2),
void 0
)
)
.subscribe({
next (value) {
results.push(value);
},
error(err) {
throw err;
},
complete() {
expect(results).to.deep.equal([
1, 2, 3, 2, 3, 4, 3, 4, 5
]);
}
});
});
it('should support a void resultSelector (still deprecated) and concurrency limit', () => {
const results: number[] = [];
of(1, 2, 3).pipe(
mergeMap(
x => of(x, x + 1, x + 2),
void 0,
1,
)
)
.subscribe({
next (value) {
results.push(value);
},
error(err) {
throw err;
},
complete() {
expect(results).to.deep.equal([
1, 2, 3, 2, 3, 4, 3, 4, 5
]);
}
});
});
it('should mergeMap many regular interval inners', () => {
const a = cold('----a---a---a---(a|) ');
const b = cold( '----b---b---(b|) ');
const c = cold( '----c---c---c---c---(c|)');
const d = cold( '----(d|) ');
const e1 = hot('a---b-----------c-------d-------| ');
const e1subs = '^ ! ';
const expected = '----a---(ab)(ab)(ab)c---c---(cd)c---(c|)';
const observableLookup = { a: a, b: b, c: c, d: d };
const source = e1.pipe(mergeMap((value) => observableLookup[value]));
expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should map values to constant resolved promises and merge', (done) => {
const source = from([4, 3, 2, 1]);
const project = (value: any) => from(Promise.resolve(42));
const results: number[] = [];
source.pipe(mergeMap(project)).subscribe(
(x) => {
results.push(x);
},
(err) => {
done(new Error('Subscriber error handler not supposed to be called.'));
},
() => {
expect(results).to.deep.equal([42, 42, 42, 42]);
done();
});
});
it('should map values to constant rejected promises and merge', (done) => {
const source = from([4, 3, 2, 1]);
const project = function (value: any) {
return from(Promise.reject<number>(42));
};
source.pipe(mergeMap(project)).subscribe(
(x) => {
done(new Error('Subscriber next handler not supposed to be called.'));
},
(err) => {
expect(err).to.equal(42);
done();
},
() => {
done(new Error('Subscriber complete handler not supposed to be called.'));
});
});
it('should map values to resolved promises and merge', (done) => {
const source = from([4, 3, 2, 1]);
const project = function (value: number, index: number) {
return from(Promise.resolve(value + index));
};
const results: number[] = [];
source.pipe(mergeMap(project)).subscribe(
(x) => {
results.push(x);
},
(err) => {
done(new Error('Subscriber error handler not supposed to be called.'));
},
() => {
expect(results).to.deep.equal([4, 4, 4, 4]);
done();
});
});
it('should map values to rejected promises and merge', (done) => {
const source = from([4, 3, 2, 1]);
const project = function (value: number, index: number) {
return from(Promise.reject<string>('' + value + '-' + index));
};
source.pipe(mergeMap(project)).subscribe(
(x) => {
done(new Error('Subscriber next handler not supposed to be called.'));
},
(err) => {
expect(err).to.equal('4-0');
done();
},
() => {
done(new Error('Subscriber complete handler not supposed to be called.'));
});
});
it('should mergeMap many outer values to many inner values', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c-------d-------| ');
const e1subs = '^ ! ';
const inner = cold('----i---j---k---l---| ', values);
const innersubs = [' ^ ! ',
' ^ ! ',
' ^ ! ',
' ^ !'];
const expected = '-----i---j---(ki)(lj)(ki)(lj)(ki)(lj)k---l---|';
const result = e1.pipe(mergeMap((value) => inner));
expectObservable(result).toBe(expected, values);
expectSubscriptions(inner.subscriptions).toBe(innersubs);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to many inner, complete late', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c-------d-----------------------|');
const e1subs = '^ !';
const inner = cold('----i---j---k---l---| ', values);
const expected = '-----i---j---(ki)(lj)(ki)(lj)(ki)(lj)k---l-------|';
const result = e1.pipe(mergeMap((value) => inner));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to many inner, outer never completes', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c-------d-------e---------------f------');
const unsub = ' !';
const e1subs = '^ !';
const inner = cold('----i---j---k---l---| ', values);
const expected = '-----i---j---(ki)(lj)(ki)(lj)(ki)(lj)(ki)(lj)k---l---i--';
const source = e1.pipe(mergeMap((value) => inner));
expectObservable(source, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should not break unsubscription chains when result is unsubscribed explicitly', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c-------d-------e---------------f------');
const e1subs = '^ !';
const inner = cold('----i---j---k---l---| ', values);
const expected = '-----i---j---(ki)(lj)(ki)(lj)(ki)(lj)(ki)(lj)k---l---i--';
const unsub = ' !';
const source = e1.pipe(
map(x => x),
mergeMap((value) => inner),
map(x => x),
);
expectObservable(source, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to many inner, inner never completes', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c-------d-------| ');
const e1subs = '^ ! ';
const inner = cold('----i---j---k---l-------------------------', values);
const expected = '-----i---j---(ki)(lj)(ki)(lj)(ki)(lj)k---l-';
const result = e1.pipe(mergeMap((value) => inner));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to many inner, and inner throws', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c-------d-------|');
const e1subs = '^ ! ';
const inner = cold('----i---j---k---l-------# ', values);
const expected = '-----i---j---(ki)(lj)(ki)# ';
const result = e1.pipe(mergeMap((value) => inner));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to many inner, and outer throws', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c-------d-------#');
const e1subs = '^ !';
const inner = cold('----i---j---k---l---| ', values);
const expected = '-----i---j---(ki)(lj)(ki)(lj)(ki)#';
const result = e1.pipe(mergeMap((value) => inner));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to many inner, both inner and outer throw', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c-------d-------#');
const e1subs = '^ ! ';
const inner = cold('----i---j---k---l---# ', values);
const expected = '-----i---j---(ki)(lj)# ';
const result = e1.pipe(mergeMap((value) => inner));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap to many cold Observable, with parameter concurrency=1', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c---| ');
const e1subs = '^ ! ';
const inner = cold('----i---j---k---l---| ', values);
const innersubs = [' ^ ! ',
' ^ ! ',
' ^ !'];
const expected = '-----i---j---k---l-------i---j---k---l-------i---j---k---l---|';
function project() { return inner; }
const result = e1.pipe(mergeMap(project, 1));
expectObservable(result).toBe(expected, values);
expectSubscriptions(inner.subscriptions).toBe(innersubs);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap to many cold Observable, with parameter concurrency=2', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c---| ');
const e1subs = '^ ! ';
const inner = cold('----i---j---k---l---| ', values);
const innersubs = [' ^ ! ',
' ^ ! ',
' ^ !'];
const expected = '-----i---j---(ki)(lj)k---(li)j---k---l---|';
function project() { return inner; }
const result = e1.pipe(mergeMap(project, 2));
expectObservable(result).toBe(expected, values);
expectSubscriptions(inner.subscriptions).toBe(innersubs);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap to many hot Observable, with parameter concurrency=1', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c---| ');
const e1subs = '^ ! ';
const hotA = hot('x----i---j---k---l---| ', values);
const hotB = hot('-x-x-xxxx-x-x-xxxxx-x----i---j---k---l---| ', values);
const hotC = hot('x-xxxx---x-x-x-x-x-xx--x--x-x--x--xxxx-x-----i---j---k---l---|', values);
const asubs = ' ^ ! ';
const bsubs = ' ^ ! ';
const csubs = ' ^ !';
const expected = '-----i---j---k---l-------i---j---k---l-------i---j---k---l---|';
const inners = { a: hotA, b: hotB, c: hotC };
function project(x: string) { return inners[x]; }
const result = e1.pipe(mergeMap(project, 1));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(hotA.subscriptions).toBe(asubs);
expectSubscriptions(hotB.subscriptions).toBe(bsubs);
expectSubscriptions(hotC.subscriptions).toBe(csubs);
});
it('should mergeMap to many hot Observable, with parameter concurrency=2', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c---| ');
const e1subs = '^ ! ';
const hotA = hot('x----i---j---k---l---| ', values);
const hotB = hot('-x-x-xxxx----i---j---k---l---| ', values);
const hotC = hot('x-xxxx---x-x-x-x-x-xx----i---j---k---l---|', values);
const asubs = ' ^ ! ';
const bsubs = ' ^ ! ';
const csubs = ' ^ !';
const expected = '-----i---j---(ki)(lj)k---(li)j---k---l---|';
const inners = { a: hotA, b: hotB, c: hotC };
function project(x: string) { return inners[x]; }
const result = e1.pipe(mergeMap(project, 2));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(hotA.subscriptions).toBe(asubs);
expectSubscriptions(hotB.subscriptions).toBe(bsubs);
expectSubscriptions(hotC.subscriptions).toBe(csubs);
});
it('should mergeMap to many cold Observable, with parameter concurrency=1', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c---| ');
const e1subs = '^ ! ';
const inner = cold('----i---j---k---l---| ', values);
const innersubs = [' ^ ! ',
' ^ ! ',
' ^ !'];
const expected = '-----i---j---k---l-------i---j---k---l-------i---j---k---l---|';
function project() { return inner; }
const result = e1.pipe(mergeMap(project, 1));
expectObservable(result).toBe(expected, values);
expectSubscriptions(inner.subscriptions).toBe(innersubs);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap to many cold Observable, with parameter concurrency=2', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c---| ');
const e1subs = '^ ! ';
const inner = cold('----i---j---k---l---| ', values);
const innersubs = [' ^ ! ',
' ^ ! ',
' ^ !'];
const expected = '-----i---j---(ki)(lj)k---(li)j---k---l---|';
function project() { return inner; }
const result = e1.pipe(mergeMap(project, 2));
expectObservable(result).toBe(expected, values);
expectSubscriptions(inner.subscriptions).toBe(innersubs);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap to many hot Observable, with parameter concurrency=1', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c---| ');
const e1subs = '^ ! ';
const hotA = hot('x----i---j---k---l---| ', values);
const hotB = hot('-x-x-xxxx-x-x-xxxxx-x----i---j---k---l---| ', values);
const hotC = hot('x-xxxx---x-x-x-x-x-xx--x--x-x--x--xxxx-x-----i---j---k---l---|', values);
const asubs = ' ^ ! ';
const bsubs = ' ^ ! ';
const csubs = ' ^ !';
const expected = '-----i---j---k---l-------i---j---k---l-------i---j---k---l---|';
const inners = { a: hotA, b: hotB, c: hotC };
function project(x: string) { return inners[x]; }
const result = e1.pipe(mergeMap(project, 1));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(hotA.subscriptions).toBe(asubs);
expectSubscriptions(hotB.subscriptions).toBe(bsubs);
expectSubscriptions(hotC.subscriptions).toBe(csubs);
});
it('should mergeMap to many hot Observable, with parameter concurrency=2', () => {
const values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
const e1 = hot('-a-------b-------c---| ');
const e1subs = '^ ! ';
const hotA = hot('x----i---j---k---l---| ', values);
const hotB = hot('-x-x-xxxx----i---j---k---l---| ', values);
const hotC = hot('x-xxxx---x-x-x-x-x-xx----i---j---k---l---|', values);
const asubs = ' ^ ! ';
const bsubs = ' ^ ! ';
const csubs = ' ^ !';
const expected = '-----i---j---(ki)(lj)k---(li)j---k---l---|';
const inners = { a: hotA, b: hotB, c: hotC };
function project(x: string) { return inners[x]; }
const result = e1.pipe(mergeMap(project, 2));
expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(hotA.subscriptions).toBe(asubs);
expectSubscriptions(hotB.subscriptions).toBe(bsubs);
expectSubscriptions(hotC.subscriptions).toBe(csubs);
});
it('should mergeMap many complex, where all inners are finite', () => {
const a = cold( '-#' );
const b = cold( '-#' );
const c = cold( '-2--3--4--5------------------6-|' );
const d = cold( '-----------2--3|' );
const e = cold( '-1--------2--3-----4--5--------|');
const f = cold( '--|' );
const g = cold( '---1-2|' );
const e1 = hot('-a-b--^-c-----d------e----------------f-----g|' );
const e1subs = '^ !';
const expected = '---2--3--4--5---1--2--3--2--3--6--4--5---1-2--|';
const observableLookup = { a: a, b: b, c: c, d: d, e: e, f: f, g: g };
const result = e1.pipe(mergeMap((value) => observableLookup[value]));
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many complex, all inners finite except one', () => {
const a = cold( '-#' );
const b = cold( '-#' );
const c = cold( '-2--3--4--5------------------6-|' );
const d = cold( '-----------2--3-' );
const e = cold( '-1--------2--3-----4--5--------|');
const f = cold( '--|' );
const g = cold( '---1-2|' );
const e1 = hot('-a-b--^-c-----d------e----------------f-----g|' );
const e1subs = '^ !';
const expected = '---2--3--4--5---1--2--3--2--3--6--4--5---1-2----';
const observableLookup = { a: a, b: b, c: c, d: d, e: e, f: f, g: g };
const result = e1.pipe(mergeMap((value) => observableLookup[value]));
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many complex, inners finite, outer does not complete', () => {
const a = cold( '-#' );
const b = cold( '-#' );
const c = cold( '-2--3--4--5------------------6-|' );
const d = cold( '-----------2--3|' );
const e = cold( '-1--------2--3-----4--5--------|');
const f = cold( '--|' );
const g = cold( '---1-2|' );
const e1 = hot('-a-b--^-c-----d------e----------------f-----g--------');
const e1subs = '^ ';
const expected = '---2--3--4--5---1--2--3--2--3--6--4--5---1-2----';
const observableLookup = { a: a, b: b, c: c, d: d, e: e, f: f, g: g };
const result = e1.pipe(mergeMap((value) => observableLookup[value]));
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many complex, all inners finite, and outer throws', () => {
const a = cold( '-#' );
const b = cold( '-#' );
const c = cold( '-2--3--4--5------------------6-|' );
const d = cold( '-----------2--3|' );
const e = cold( '-1--------2--3-----4--5--------|');
const f = cold( '--|' );
const g = cold( '---1-2|' );
const e1 = hot('-a-b--^-c-----d------e----------------f-----g# ');
const e1subs = '^ ! ';
const expected = '---2--3--4--5---1--2--3--2--3--6--4--5-# ';
const observableLookup = { a: a, b: b, c: c, d: d, e: e, f: f, g: g };
const result = e1.pipe(mergeMap((value) => observableLookup[value]));
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many complex, all inners complete except one throws', () => {
const a = cold( '-#' );
const b = cold( '-#' );
const c = cold( '-2--3--4--5------------------6-#' );
const d = cold( '-----------2--3|' );
const e = cold( '-1--------2--3-----4--5--------|');
const f = cold( '--|' );
const g = cold( '---1-2|' );
const e1 = hot('-a-b--^-c-----d------e----------------f-----g|' );
const e1subs = '^ ! ';
const expected = '---2--3--4--5---1--2--3--2--3--6-# ';
const observableLookup = { a: a, b: b, c: c, d: d, e: e, f: f, g: g };
const result = e1.pipe(mergeMap((value) => observableLookup[value]));
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many complex, all inners finite, outer is unsubscribed', () => {
const a = cold( '-#' );
const b = cold( '-#' );
const c = cold( '-2--3--4--5------------------6-|' );
const d = cold( '-----------2--3|' );
const e = cold( '-1--------2--3-----4--5--------|');
const f = cold( '--|' );
const g = cold( '---1-2|' );
const e1 = hot('-a-b--^-c-----d------e----------------f-----g|' );
const unsub = ' ! ';
const e1subs = '^ ! ';
const expected = '---2--3--4--5---1--2--3--2--3-- ';
const observableLookup = { a: a, b: b, c: c, d: d, e: e, f: f, g: g };
const source = e1.pipe(mergeMap((value) => observableLookup[value]));
expectObservable(source, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many complex, all inners finite, project throws', () => {
const a = cold( '-#' );
const b = cold( '-#' );
const c = cold( '-2--3--4--5------------------6-|' );
const d = cold( '-----------2--3|' );
const e = cold( '-1--------2--3-----4--5--------|');
const f = cold( '--|' );
const g = cold( '---1-2|' );
const e1 = hot('-a-b--^-c-----d------e----------------f-----g|' );
const e1subs = '^ ! ';
const expected = '---2--3--4--5--# ';
const observableLookup = { a: a, b: b, c: c, d: d, e: e, f: f, g: g };
let invoked = 0;
const source = e1.pipe(mergeMap((value) => {
invoked++;
if (invoked === 3) {
throw 'error';
}
return observableLookup[value];
}));
expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
function arrayRepeat(value: any, times: number): any {
const results = [];
for (let i = 0; i < times; i++) {
results.push(value);
}
return results;
}
it('should mergeMap many outer to an array for each value', () => {
const e1 = hot('2-----4--------3--------2-------|');
const e1subs = '^ !';
const expected = '(22)--(4444)---(333)----(22)----|';
const source = e1.pipe(mergeMap((value) => arrayRepeat(value, +value)));
expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to inner arrays, and outer throws', () => {
const e1 = hot('2-----4--------3--------2-------#');
const e1subs = '^ !';
const expected = '(22)--(4444)---(333)----(22)----#';
const source = e1.pipe(mergeMap((value) => arrayRepeat(value, +value)));
expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to inner arrays, outer gets unsubscribed', () => {
const e1 = hot('2-----4--------3--------2-------|');
const unsub = ' ! ';
const e1subs = '^ ! ';
const expected = '(22)--(4444)-- ';
const source = e1.pipe(mergeMap((value) => arrayRepeat(value, +value)));
expectObservable(source, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should mergeMap many outer to inner arrays, project throws', () => {
const e1 = hot('2-----4--------3--------2-------|');
const e1subs = '^ ! ';
const expected = '(22)--(4444)---# ';
let invoked = 0;
const source = e1.pipe(mergeMap((value) => {
invoked++;
if (invoked === 3) {
throw 'error';
}
return arrayRepeat(value, +value);
}));
expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should map and flatten', () => {
const source = of(1, 2, 3, 4).pipe(mergeMap((x) => of(x + '!')));
const expected = ['1!', '2!', '3!', '4!'];
let completed = false;
source.subscribe((x) => {
expect(x).to.equal(expected.shift());
}, null, () => {
expect(expected.length).to.equal(0);
completed = true;
});
expect(completed).to.be.true;
});
it('should map and flatten an Array', () => {
const source = of(1, 2, 3, 4).pipe(mergeMap((x): any => [x + '!']));
const expected = ['1!', '2!', '3!', '4!'];
let completed = false;
source.subscribe((x) => {
expect(x).to.equal(expected.shift());
}, null, () => {
expect(expected.length).to.equal(0);
completed = true;
});
expect(completed).to.be.true;
});
it('should support nested merges', (done: MochaDone) => {
// Added as a failing test when investigating:
// https://github.com/ReactiveX/rxjs/issues/4071
const results: (number | string)[] = [];
of(1).pipe(
mergeMap(() => defer(() =>
of(2, asapScheduler)
).pipe(
mergeMap(() => defer(() =>
of(3, asapScheduler)
))
))
)
.subscribe({
next(value: any) { results.push(value); },
complete() { results.push('done'); }
});
setTimeout(() => {
expect(results).to.deep.equal([3, 'done']);
done();
}, 0);
});
it('should support nested merges with promises', (done: MochaDone) => {
// Added as a failing test when investigating:
// https://github.com/ReactiveX/rxjs/issues/4071
const results: (number | string)[] = [];
of(1).pipe(
mergeMap(() =>
from(Promise.resolve(2)).pipe(
mergeMap(() => Promise.resolve(3))
)
)
)
.subscribe({
next(value) { results.push(value); },
complete() { results.push('done'); }
});
setTimeout(() => {
expect(results).to.deep.equal([3, 'done']);
done();
}, 0);
});
it('should support wrapped sources', (done: MochaDone) => {
// Added as a failing test when investigating:
// https://github.com/ReactiveX/rxjs/issues/4095
const results: (number | string)[] = [];
const wrapped = new Observable<number>(subscriber => {
const subscription = timer(0, asapScheduler).subscribe(subscriber);
return () => subscription.unsubscribe();
});
wrapped.pipe(
mergeMap(() => timer(0, asapScheduler))
).subscribe({
next(value) { results.push(value); },
complete() { results.push('done'); }
});
setTimeout(() => {
expect(results).to.deep.equal([0, 'done']);
done();
}, 0);
});
type('should support type signatures', () => {
let o: Observable<number>;
/* tslint:disable:no-unused-variable */
let a1: Observable<string> = o.pipe(mergeMap(x => x.toString()));
let a2: Observable<string> = o.pipe(mergeMap(x => x.toString(), 3));
let a3: Observable<{ o: number; i: string; }> = o.pipe(mergeMap(x => x.toString(), (o, i) => ({ o, i })));
let a4: Observable<{ o: number; i: string; }> = o.pipe(mergeMap(x => x.toString(), (o, i) => ({ o, i }), 3));
/* tslint:enable:no-unused-variable */
});
});