forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtake-spec.ts
138 lines (109 loc) · 4.31 KB
/
take-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
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions};
const Observable = Rx.Observable;
/** @test {take} */
describe('Observable.prototype.take', () => {
asDiagram('take(2)')('should take two values of an observable with many values', () => {
const e1 = cold('--a-----b----c---d--|');
const e1subs = '^ ! ';
const expected = '--a-----(b|) ';
expectObservable(e1.take(2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should work with empty', () => {
const e1 = cold('|');
const e1subs = '(^!)';
const expected = '|';
expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should go on forever on never', () => {
const e1 = cold('-');
const e1subs = '^';
const expected = '-';
expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should be empty on take(0)', () => {
const e1 = hot('--a--^--b----c---d--|');
const e1subs = []; // Don't subscribe at all
const expected = '|';
expectObservable(e1.take(0)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should take one value of an observable with one value', () => {
const e1 = hot('---(a|)');
const e1subs = '^ ! ';
const expected = '---(a|)';
expectObservable(e1.take(1)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should take one values of an observable with many values', () => {
const e1 = hot('--a--^--b----c---d--|');
const e1subs = '^ ! ';
const expected = '---(b|) ';
expectObservable(e1.take(1)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should error on empty', () => {
const e1 = hot('--a--^----|');
const e1subs = '^ !';
const expected = '-----|';
expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should propagate error from the source observable', () => {
const e1 = hot('---^---#', null, 'too bad');
const e1subs = '^ !';
const expected = '----#';
expectObservable(e1.take(42)).toBe(expected, null, 'too bad');
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should propagate error from an observable with values', () => {
const e1 = hot('---^--a--b--#');
const e1subs = '^ !';
const expected = '---a--b--#';
expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should allow unsubscribing explicitly and early', () => {
const e1 = hot('---^--a--b-----c--d--e--|');
const unsub = ' ! ';
const e1subs = '^ ! ';
const expected = '---a--b--- ';
expectObservable(e1.take(42), unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should work with throw', () => {
const e1 = cold('#');
const e1subs = '(^!)';
const expected = '#';
expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should throw if total is less than zero', () => {
expect(() => { Observable.range(0, 10).take(-1); })
.to.throw(Rx.ArgumentOutOfRangeError);
});
it('should not break unsubscription chain when unsubscribed explicitly', () => {
const e1 = hot('---^--a--b-----c--d--e--|');
const unsub = ' ! ';
const e1subs = '^ ! ';
const expected = '---a--b--- ';
const result = e1
.mergeMap((x: string) => Observable.of(x))
.take(42)
.mergeMap((x: string) => Observable.of(x));
expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should unsubscribe from the source when it reaches the limit', () => {
const source = Observable.create(observer => {
expect(observer.isUnsubscribed).to.be.false;
observer.next(42);
expect(observer.isUnsubscribed).to.be.true;
}).take(1);
source.subscribe();
});
});