forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
switch-spec.ts
316 lines (275 loc) · 11.1 KB
/
switch-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
import { expect } from 'chai';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { Observable, of, NEVER, queueScheduler, Subject } from 'rxjs';
import { map, switchAll } from 'rxjs/operators';
declare function asDiagram(arg: string): Function;
declare const type: Function;
/** @test {switch} */
describe('switchAll', () => {
asDiagram('switchAll')('should switch a hot observable of cold observables', () => {
const x = cold( '--a---b--c---d--| ');
const y = cold( '----e---f--g---|');
const e1 = hot( '--x------y-------| ', { x: x, y: y });
const expected = '----a---b----e---f--g---|';
expectObservable(e1.pipe(switchAll())).toBe(expected);
});
it('should switch to each immediately-scheduled inner Observable', (done) => {
const a = of<number>(1, 2, 3, queueScheduler);
const b = of<number>(4, 5, 6, queueScheduler);
const r = [1, 4, 5, 6];
let i = 0;
of(a, b, queueScheduler)
.pipe(switchAll())
.subscribe((x) => {
expect(x).to.equal(r[i++]);
}, null, done);
});
it('should unsub inner observables', () => {
const unsubbed: string[] = [];
of('a', 'b').map((x) =>
new Observable<string>((subscriber) => {
subscriber.complete();
return () => {
unsubbed.push(x);
};
}))
.pipe(switchAll())
.subscribe();
expect(unsubbed).to.deep.equal(['a', 'b']);
});
it('should switch to each inner Observable', (done) => {
const a = of(1, 2, 3);
const b = of(4, 5, 6);
const r = [1, 2, 3, 4, 5, 6];
let i = 0;
of(a, b).pipe(switchAll()).subscribe((x) => {
expect(x).to.equal(r[i++]);
}, null, done);
});
it('should handle a hot observable of observables', () => {
const x = cold( '--a---b---c--| ');
const xsubs = ' ^ ! ';
const y = cold( '---d--e---f---|');
const ysubs = ' ^ !';
const e1 = hot( '------x-------y------| ', { x: x, y: y });
const expected = '--------a---b----d--e---f---|';
expectObservable(e1.pipe(switchAll())).toBe(expected);
expectSubscriptions(x.subscriptions).toBe(xsubs);
expectSubscriptions(y.subscriptions).toBe(ysubs);
});
it('should handle a hot observable of observables, outer is unsubscribed early', () => {
const x = cold( '--a---b---c--| ');
const xsubs = ' ^ ! ';
const y = cold( '---d--e---f---|');
const ysubs = ' ^ ! ';
const e1 = hot( '------x-------y------| ', { x: x, y: y });
const unsub = ' ! ';
const expected = '--------a---b--- ';
expectObservable(e1.pipe(switchAll()), unsub).toBe(expected);
expectSubscriptions(x.subscriptions).toBe(xsubs);
expectSubscriptions(y.subscriptions).toBe(ysubs);
});
it('should not break unsubscription chains when result is unsubscribed explicitly', () => {
const x = cold( '--a---b---c--| ');
const xsubs = ' ^ ! ';
const y = cold( '---d--e---f---|');
const ysubs = ' ^ ! ';
const e1 = hot( '------x-------y------| ', { x: x, y: y });
const expected = '--------a---b---- ';
const unsub = ' ! ';
const result = e1
.mergeMap((x) => of(x))
.pipe(switchAll())
.mergeMap((x) => of(x));
expectObservable(result, unsub).toBe(expected);
expectSubscriptions(x.subscriptions).toBe(xsubs);
expectSubscriptions(y.subscriptions).toBe(ysubs);
});
it('should handle a hot observable of observables, inner never completes', () => {
const x = cold( '--a---b---c--| ');
const xsubs = ' ^ ! ';
const y = cold( '---d--e---f-----');
const ysubs = ' ^ ';
const e1 = hot( '------x-------y------| ', { x: x, y: y });
const expected = '--------a---b----d--e---f-----';
expectObservable(e1.pipe(switchAll())).toBe(expected);
expectSubscriptions(x.subscriptions).toBe(xsubs);
expectSubscriptions(y.subscriptions).toBe(ysubs);
});
it('should handle a synchronous switch to the second inner observable', () => {
const x = cold( '--a---b---c--| ');
const xsubs = ' (^!) ';
const y = cold( '---d--e---f---| ');
const ysubs = ' ^ ! ';
const e1 = hot( '------(xy)------------|', { x: x, y: y });
const expected = '---------d--e---f-----|';
expectObservable(e1.pipe(switchAll())).toBe(expected);
expectSubscriptions(x.subscriptions).toBe(xsubs);
expectSubscriptions(y.subscriptions).toBe(ysubs);
});
it('should handle a hot observable of observables, one inner throws', () => {
const x = cold( '--a---# ');
const xsubs = ' ^ ! ';
const y = cold( '---d--e---f---|');
const ysubs: string[] = [];
const e1 = hot( '------x-------y------| ', { x: x, y: y });
const expected = '--------a---# ';
expectObservable(e1.pipe(switchAll())).toBe(expected);
expectSubscriptions(x.subscriptions).toBe(xsubs);
expectSubscriptions(y.subscriptions).toBe(ysubs);
});
it('should handle a hot observable of observables, outer throws', () => {
const x = cold( '--a---b---c--| ');
const xsubs = ' ^ ! ';
const y = cold( '---d--e---f---|');
const ysubs = ' ^ ! ';
const e1 = hot( '------x-------y-------# ', { x: x, y: y });
const expected = '--------a---b----d--e-# ';
expectObservable(e1.pipe(switchAll())).toBe(expected);
expectSubscriptions(x.subscriptions).toBe(xsubs);
expectSubscriptions(y.subscriptions).toBe(ysubs);
});
it('should handle an empty hot observable', () => {
const e1 = hot('------|');
const e1subs = '^ !';
const expected = '------|';
expectObservable(e1.pipe(switchAll())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a never hot observable', () => {
const e1 = hot('-');
const e1subs = '^';
const expected = '-';
expectObservable(e1.pipe(switchAll())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should complete not before the outer completes', () => {
const x = cold( '--a---b---c--| ');
const xsubs = ' ^ ! ';
const e1 = hot( '------x---------------|', { x: x });
const e1subs = '^ !';
const expected = '--------a---b---c-----|';
expectObservable(e1.pipe(switchAll())).toBe(expected);
expectSubscriptions(x.subscriptions).toBe(xsubs);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle an observable of promises', (done) => {
const expected = [3];
of(Promise.resolve(1), Promise.resolve(2), Promise.resolve(3))
.pipe(switchAll())
.subscribe((x) => {
expect(x).to.equal(expected.shift());
}, null, () => {
expect(expected.length).to.equal(0);
done();
});
});
it('should handle an observable of promises, where last rejects', (done) => {
of<Promise<number>>(Promise.resolve(1), Promise.resolve(2), Promise.reject(3))
.pipe(switchAll())
.subscribe(() => {
done(new Error('should not be called'));
}, (err) => {
expect(err).to.equal(3);
done();
}, () => {
done(new Error('should not be called'));
});
});
it('should handle an observable with Arrays in it', () => {
const expected = [1, 2, 3, 4];
let completed = false;
of<any>(NEVER, NEVER, [1, 2, 3, 4])
.pipe(switchAll())
.subscribe((x) => {
expect(x).to.equal(expected.shift());
}, null, () => {
completed = true;
expect(expected.length).to.equal(0);
});
expect(completed).to.be.true;
});
it('should not leak when child completes before each switch (prevent memory leaks #2355)', () => {
let iStream: Subject<number>;
const oStreamControl = new Subject<number>();
const oStream = oStreamControl.pipe(
map(() => (iStream = new Subject<number>()))
);
const switcher = oStream.pipe(switchAll());
const result: number[] = [];
let sub = switcher.subscribe((x) => result.push(x));
[0, 1, 2, 3, 4].forEach((n) => {
oStreamControl.next(n); // creates inner
iStream.complete();
});
// Expect one child of switch(): The oStream
expect(
(<any>sub)._subscriptions[0]._subscriptions.length
).to.equal(1);
sub.unsubscribe();
});
it('should not leak if we switch before child completes (prevent memory leaks #2355)', () => {
const oStreamControl = new Subject<number>();
const oStream = oStreamControl.pipe(
map(() => new Subject<number>())
);
const switcher = oStream.pipe(switchAll());
const result: number[] = [];
let sub = switcher.subscribe((x) => result.push(x));
[0, 1, 2, 3, 4].forEach((n) => {
oStreamControl.next(n); // creates inner
});
// Expect one child of switch(): The oStream
expect(
(sub as any)._subscriptions[0]._subscriptions.length
).to.equal(1);
// Expect two children of subscribe(): The destination and the first inner
// See #4106 - inner subscriptions are now added to destinations
expect(
(sub as any)._subscriptions.length
).to.equal(2);
sub.unsubscribe();
});
type(() => {
/* tslint:disable:no-unused-variable */
const source1 = of(1, 2, 3);
const source2 = [1, 2, 3];
const source3 = new Promise<number>(d => d(1));
let result: Observable<number> = Observable
.of(source1, source2, source3)
.pipe(switchAll());
/* tslint:enable:no-unused-variable */
});
type(() => {
/* tslint:disable:no-unused-variable */
const source1 = of(1, 2, 3);
const source2 = [1, 2, 3];
const source3 = new Promise<number>(d => d(1));
let result: Observable<number> = Observable
.of(source1, source2, source3)
.pipe(switchAll());
/* tslint:enable:no-unused-variable */
});
type(() => {
// coerce type to a specific type
/* tslint:disable:no-unused-variable */
const source1 = of(1, 2, 3);
const source2 = [1, 2, 3];
const source3 = new Promise<number>(d => d(1));
let result: Observable<string> = Observable
.of(source1 as any, source2 as any, source3 as any)
.pipe(switchAll<string>());
/* tslint:enable:no-unused-variable */
});
type(() => {
// coerce type to a specific type
/* tslint:disable:no-unused-variable */
const source1 = of(1, 2, 3);
const source2 = [1, 2, 3];
const source3 = new Promise<number>(d => d(1));
let result: Observable<string> = Observable
.of(source1 as any, source2 as any, source3 as any)
.switch<string>();
/* tslint:enable:no-unused-variable */
});
});