forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservable-spec.ts
626 lines (540 loc) · 17.2 KB
/
Observable-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
import {expect} from 'chai';
import * as Rx from '../dist/cjs/Rx';
declare const {asDiagram, expectObservable};
const Subscriber = Rx.Subscriber;
const Observable = Rx.Observable;
declare const __root__: any;
function expectFullObserver(val) {
expect(val).to.be.a('object');
expect(val.next).to.be.a('function');
expect(val.error).to.be.a('function');
expect(val.complete).to.be.a('function');
expect(val.isUnsubscribed).to.be.a('boolean');
}
/** @test {Observable} */
describe('Observable', () => {
it('should be constructed with a subscriber function', (done: MochaDone) => {
const source = new Observable(function (observer) {
expectFullObserver(observer);
observer.next(1);
observer.complete();
});
source.subscribe(function (x) { expect(x).to.equal(1); }, null, done);
});
describe('forEach', () => {
it('should iterate and return a Promise', (done: MochaDone) => {
const expected = [1, 2, 3];
const result = Observable.of(1, 2, 3).forEach(function (x) {
expect(x).to.equal(expected.shift());
}, Promise)
.then(() => {
done();
});
expect(result.then).to.be.a('function');
});
it('should reject promise when in error', (done: MochaDone) => {
Observable.throw('bad').forEach((x: any) => {
done(new Error('should not be called'));
}, Promise).then(() => {
done(new Error('should not complete'));
}, (err: any) => {
expect(err).to.equal('bad');
done();
});
});
it('should allow Promise to be globally configured', (done: MochaDone) => {
let wasCalled = false;
__root__.Rx = {};
__root__.Rx.config = {};
__root__.Rx.config.Promise = function MyPromise(callback) {
wasCalled = true;
return new Promise(callback);
};
Observable.of(42).forEach((x: number) => {
expect(x).to.equal(42);
}).then(() => {
expect(wasCalled).to.be.true;
delete __root__.Rx;
done();
});
});
it('should reject promise if nextHandler throws', (done: MochaDone) => {
const results = [];
Observable.of(1, 2, 3).forEach((x: number) => {
if (x === 3) {
throw new Error('NO THREES!');
}
results.push(x);
}, Promise)
.then(() => {
done(new Error('should not be called'));
}, (err) => {
expect(err).to.be.an('error', 'NO THREES!');
expect(results).to.deep.equal([1, 2]);
}).then(() => {
done();
});
});
it('should handle a synchronous throw from the next handler and tear down', (done: MochaDone) => {
const expected = new Error('I told, you Bobby Boucher, twos are the debil!');
let unsubscribeCalled = false;
const syncObservable = new Observable<number>((observer: Rx.Observer<number>) => {
observer.next(1);
observer.next(2);
observer.next(3);
return () => {
unsubscribeCalled = true;
};
});
const results = [];
syncObservable.forEach((x) => {
results.push(x);
if (x === 2) {
throw expected;
}
}).then(
() => {
done(new Error('should not be called'));
},
(err) => {
results.push(err);
expect(results).to.deep.equal([1, 2, expected]);
expect(unsubscribeCalled).to.be.true;
done();
});
});
it('should handle an asynchronous throw from the next handler and tear down', (done: MochaDone) => {
const expected = new Error('I told, you Bobby Boucher, twos are the debil!');
let unsubscribeCalled = false;
const syncObservable = new Observable<number>((observer: Rx.Observer<number>) => {
let i = 1;
const id = setInterval(() => observer.next(i++), 1);
return () => {
clearInterval(id);
unsubscribeCalled = true;
};
});
const results = [];
syncObservable.forEach((x) => {
results.push(x);
if (x === 2) {
throw expected;
}
}).then(
() => {
done(new Error('should not be called'));
},
(err) => {
results.push(err);
expect(results).to.deep.equal([1, 2, expected]);
expect(unsubscribeCalled).to.be.true;
done();
});
});
});
describe('subscribe', () => {
it('should be synchronous', () => {
let subscribed = false;
let nexted;
let completed;
const source = new Observable((observer: Rx.Observer<string>) => {
subscribed = true;
observer.next('wee');
expect(nexted).to.equal('wee');
observer.complete();
expect(completed).to.be.true;
});
expect(subscribed).to.be.false;
let mutatedByNext = false;
let mutatedByComplete = false;
source.subscribe((x: string) => {
nexted = x;
mutatedByNext = true;
}, null, () => {
completed = true;
mutatedByComplete = true;
});
expect(mutatedByNext).to.be.true;
expect(mutatedByComplete).to.be.true;
});
it('should work when subscribe is called with no arguments', () => {
const source = new Observable((subscriber: Rx.Subscriber<string>) => {
subscriber.next('foo');
subscriber.complete();
});
source.subscribe();
});
it('should return a Subscription that calls the unsubscribe function returned by the subscriber', () => {
let unsubscribeCalled = false;
const source = new Observable(() => {
return () => {
unsubscribeCalled = true;
};
});
const sub = source.subscribe(() => {
//noop
});
expect(sub instanceof Rx.Subscription).to.be.true;
expect(unsubscribeCalled).to.be.false;
expect(sub.unsubscribe).to.be.a('function');
sub.unsubscribe();
expect(unsubscribeCalled).to.be.true;
});
it('should run unsubscription logic when an error is thrown sending messages synchronously', () => {
let messageError = false;
let messageErrorValue = false;
let unsubscribeCalled = false;
let sub;
const source = new Observable((observer: Rx.Observer<string>) => {
observer.next('boo!');
return () => {
unsubscribeCalled = true;
};
});
try {
sub = source.subscribe((x: string) => { throw x; });
} catch (e) {
messageError = true;
messageErrorValue = e;
}
expect(sub).to.be.a('undefined');
expect(unsubscribeCalled).to.be.true;
expect(messageError).to.be.true;
expect(messageErrorValue).to.equal('boo!');
});
it('should dispose of the subscriber when an error is thrown sending messages synchronously', () => {
let messageError = false;
let messageErrorValue = false;
let unsubscribeCalled = false;
let sub;
const subscriber = new Subscriber((x: string) => { throw x; });
const source = new Observable((observer: Rx.Observer<string>) => {
observer.next('boo!');
return () => {
unsubscribeCalled = true;
};
});
try {
sub = source.subscribe(subscriber);
} catch (e) {
messageError = true;
messageErrorValue = e;
}
expect(sub).to.be.a('undefined');
expect(subscriber.isUnsubscribed).to.be.true;
expect(unsubscribeCalled).to.be.true;
expect(messageError).to.be.true;
expect(messageErrorValue).to.equal('boo!');
});
it('should ignore next messages after unsubscription', () => {
let times = 0;
new Observable((observer: Rx.Observer<number>) => {
observer.next(0);
observer.next(0);
observer.next(0);
observer.next(0);
})
.do(() => times += 1)
.subscribe(
function() {
if (times === 2) {
this.unsubscribe();
}
}
);
expect(times).to.equal(2);
});
it('should ignore error messages after unsubscription', () => {
let times = 0;
let errorCalled = false;
new Observable((observer: Rx.Observer<number>) => {
observer.next(0);
observer.next(0);
observer.next(0);
observer.error(0);
})
.do(() => times += 1)
.subscribe(
function() {
if (times === 2) {
this.unsubscribe();
}
},
function() { errorCalled = true; }
);
expect(times).to.equal(2);
expect(errorCalled).to.be.false;
});
it('should ignore complete messages after unsubscription', () => {
let times = 0;
let completeCalled = false;
new Observable((observer: Rx.Observer<number>) => {
observer.next(0);
observer.next(0);
observer.next(0);
observer.complete();
})
.do(() => times += 1)
.subscribe(
function() {
if (times === 2) {
this.unsubscribe();
}
},
null,
function() { completeCalled = true; }
);
expect(times).to.equal(2);
expect(completeCalled).to.be.false;
});
describe('when called with an anonymous observer', () => {
it('should accept an anonymous observer with just a next function and call the next function in the context' +
' of the anonymous observer', (done: MochaDone) => {
//intentionally not using lambda to avoid typescript's this context capture
const o = {
next: function next(x) {
expect(this).to.equal(o);
expect(x).to.equal(1);
done();
}
};
Observable.of(1).subscribe(o);
});
it('should accept an anonymous observer with just an error function and call the error function in the context' +
' of the anonymous observer', (done: MochaDone) => {
//intentionally not using lambda to avoid typescript's this context capture
const o = {
error: function error(err) {
expect(this).to.equal(o);
expect(err).to.equal('bad');
done();
}
};
Observable.throw('bad').subscribe(o);
});
it('should accept an anonymous observer with just a complete function and call the complete function in the' +
' context of the anonymous observer', (done: MochaDone) => {
//intentionally not using lambda to avoid typescript's this context capture
const o = {
complete: function complete() {
expect(this).to.equal(o);
done();
}
};
Observable.empty().subscribe(o);
});
it('should accept an anonymous observer with no functions at all', () => {
expect(() => {
Observable.empty().subscribe(<any>{});
}).not.to.throw();
});
it('should run unsubscription logic when an error is thrown sending messages synchronously to an' +
' anonymous observer', () => {
let messageError = false;
let messageErrorValue = false;
let unsubscribeCalled = false;
//intentionally not using lambda to avoid typescript's this context capture
const o = {
next: function next(x) {
expect(this).to.equal(o);
throw x;
}
};
let sub;
const source = new Observable((observer: Rx.Observer<string>) => {
observer.next('boo!');
return () => {
unsubscribeCalled = true;
};
});
try {
sub = source.subscribe(o);
} catch (e) {
messageError = true;
messageErrorValue = e;
}
expect(sub).to.be.a('undefined');
expect(unsubscribeCalled).to.be.true;
expect(messageError).to.be.true;
expect(messageErrorValue).to.equal('boo!');
});
it('should ignore next messages after unsubscription', () => {
let times = 0;
new Observable((observer: Rx.Observer<number>) => {
observer.next(0);
observer.next(0);
observer.next(0);
observer.next(0);
})
.do(() => times += 1)
.subscribe({
next() {
if (times === 2) {
this.unsubscribe();
}
}
});
expect(times).to.equal(2);
});
it('should ignore error messages after unsubscription', () => {
let times = 0;
let errorCalled = false;
new Observable((observer: Rx.Observer<number>) => {
observer.next(0);
observer.next(0);
observer.next(0);
observer.error(0);
})
.do(() => times += 1)
.subscribe({
next() {
if (times === 2) {
this.unsubscribe();
}
},
error() { errorCalled = true; }
});
expect(times).to.equal(2);
expect(errorCalled).to.be.false;
});
it('should ignore complete messages after unsubscription', () => {
let times = 0;
let completeCalled = false;
new Observable((observer: Rx.Observer<number>) => {
observer.next(0);
observer.next(0);
observer.next(0);
observer.complete();
})
.do(() => times += 1)
.subscribe({
next() {
if (times === 2) {
this.unsubscribe();
}
},
complete() { completeCalled = true; }
});
expect(times).to.equal(2);
expect(completeCalled).to.be.false;
});
});
});
});
/** @test {Observable} */
describe('Observable.create', () => {
asDiagram('create(obs => { obs.next(1); })')
('should create a cold observable that emits just 1', () => {
const e1 = Observable.create(obs => { obs.next(1); });
const expected = 'x';
expectObservable(e1).toBe(expected, {x: 1});
});
it('should create an Observable', () => {
const result = Observable.create(() => {
//noop
});
expect(result instanceof Observable).to.be.true;
});
it('should provide an observer to the function', () => {
let called = false;
const result = Observable.create((observer: Rx.Observer<any>) => {
called = true;
expectFullObserver(observer);
observer.complete();
});
expect(called).to.be.false;
result.subscribe(() => {
//noop
});
expect(called).to.be.true;
});
});
/** @test {Observable} */
describe('Observable.lift', () => {
it('should be overrideable in a custom Observable type that composes', (done: MochaDone) => {
class MyCustomObservable<T> extends Rx.Observable<T> {
lift<R>(operator: Rx.Operator<T, R>): Rx.Observable<R> {
const observable = new MyCustomObservable<R>();
(<any>observable).source = this;
(<any>observable).operator = operator;
return observable;
}
}
const result = new MyCustomObservable((observer: Rx.Observer<number>) => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
}).map((x: number) => { return 10 * x; });
expect(result instanceof MyCustomObservable).to.be.true;
const expected = [10, 20, 30];
result.subscribe(
function (x) {
expect(x).to.equal(expected.shift());
}, (x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
it('should allow injecting behaviors into all subscribers in an operator ' +
'chain when overridden', (done: MochaDone) => {
// The custom Subscriber
const log: Array<string> = [];
class LogSubscriber<T> extends Rx.Subscriber<T> {
next(value?: T): void {
log.push('next ' + value);
if (!this.isStopped) {
this._next(value);
}
}
}
// The custom Operator
class LogOperator<T, R> extends Rx.Operator<T, R> {
constructor(private childOperator: Rx.Operator<T, R>) {
super();
}
call(subscriber: Rx.Subscriber<R>, source: any): Rx.Subscription | Function | void {
return this.childOperator.call(new LogSubscriber<R>(subscriber), source);
}
}
// The custom Observable
class LogObservable<T> extends Observable<T> {
lift<R>(operator: Rx.Operator<T, R>): Rx.Observable<R> {
const observable = new LogObservable<R>();
(<any>observable).source = this;
(<any>observable).operator = new LogOperator(operator);
return observable;
}
}
// Use the LogObservable
const result = new LogObservable((observer: Rx.Observer<number>) => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
})
.map((x: number) => { return 10 * x; })
.filter((x: number) => { return x > 15; })
.count();
expect(result instanceof LogObservable).to.be.true;
const expected = [2];
result.subscribe(
function (x) {
expect(x).to.equal(expected.shift());
},
(x) => {
done(new Error('should not be called'));
}, () => {
expect(log).to.deep.equal([
'next 10', // map
'next 20', // map
'next 20', // filter
'next 30', // map
'next 30', // filter
'next 2' // count
]);
done();
});
});
});