forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapTo-spec.ts
102 lines (80 loc) · 3.01 KB
/
mapTo-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
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions};
const Observable = Rx.Observable;
// function shortcuts
const throwError = function () { throw new Error(); };
/** @test {mapTo} */
describe('Observable.prototype.mapTo', () => {
asDiagram('mapTo(\'a\')')('should map multiple values', () => {
const a = cold('--1--2--3--|');
const asubs = '^ !';
const expected = '--a--a--a--|';
expectObservable(a.mapTo('a')).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
});
it('should map one value', () => {
const a = cold('--7--|');
const asubs = '^ !';
const expected = '--y--|';
expectObservable(a.mapTo('y')).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
});
it('should allow unsubscribing explicitly and early', () => {
const a = cold('--1--2--3--|');
const unsub = ' ! ';
const asubs = '^ ! ';
const expected = '--x--x- ';
expectObservable(a.mapTo('x'), unsub).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
});
it('should propagate errors from observable that emits only errors', () => {
const a = cold('--#', null, 'too bad');
const asubs = '^ !';
const expected = '--#';
expectObservable(a.mapTo(1)).toBe(expected, null, 'too bad');
expectSubscriptions(a.subscriptions).toBe(asubs);
});
it('should propagate errors from observable that emit values', () => {
const a = cold('--1--2--#', undefined, 'too bad');
const asubs = '^ !';
const expected = '--x--x--#';
expectObservable(a.mapTo('x')).toBe(expected, undefined, 'too bad');
expectSubscriptions(a.subscriptions).toBe(asubs);
});
it('should propagate errors from subscribe', () => {
const r = () => {
Observable.of(1)
.mapTo(-1)
.subscribe(throwError);
};
expect(r).to.throw();
});
it('should not map an empty observable', () => {
const a = cold('|');
const asubs = '(^!)';
const expected = '|';
expectObservable(a.mapTo(-1)).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
});
it('should map twice', () => {
const a = hot('-0----1-^-2---3--4-5--6--7-8-|');
const asubs = '^ !';
const expected = '--h---h--h-h--h--h-h-|';
const r = a.mapTo(-1).mapTo('h');
expectObservable(r).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
});
it('should not break unsubscription chain when unsubscribed explicitly', () => {
const a = cold('--1--2--3--|');
const unsub = ' ! ';
const asubs = '^ ! ';
const expected = '--x--x- ';
const r = a
.mergeMap((x: string) => Observable.of(x))
.mapTo('x')
.mergeMap((x: string) => Observable.of(x));
expectObservable(r, unsub).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
});
});