-
Notifications
You must be signed in to change notification settings - Fork 3k
/
first-spec.ts
217 lines (177 loc) · 6.91 KB
/
first-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
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, asDiagram, expectObservable, expectSubscriptions};
const Observable = Rx.Observable;
/** @test {first} */
describe('Observable.prototype.first', () => {
asDiagram('first')('should take the first value of an observable with many values', () => {
const e1 = hot('-----a--b--c---d---|');
const expected = '-----(a|) ';
const sub = '^ ! ';
expectObservable(e1.first()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should take the first value of an observable with one value', () => {
const e1 = hot('---(a|)');
const expected = '---(a|)';
const sub = '^ !';
expectObservable(e1.first()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should error on empty', () => {
const e1 = hot('--a--^----|');
const expected = '-----#';
const sub = '^ !';
expectObservable(e1.first()).toBe(expected, null, new Rx.EmptyError());
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should return the default value if source observable was empty', () => {
const e1 = hot('-----^----|');
const expected = '-----(a|)';
const sub = '^ !';
expectObservable(e1.first(null, null, 'a')).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should only emit one value in recursive cases', () => {
const subject = new Rx.Subject<number>();
const results = [];
subject.first().subscribe(x => {
results.push(x);
subject.next(x + 1);
});
subject.next(0);
expect(results).to.deep.equal([0]);
});
it('should propagate error from the source observable', () => {
const e1 = hot('---^---#');
const expected = '----#';
const sub = '^ !';
expectObservable(e1.first()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should go on forever on never', () => {
const e1 = hot('--^-------');
const expected = '--------';
const sub = '^ ';
expectObservable(e1.first()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should allow unsubscribing early and explicitly', () => {
const e1 = hot('--a--^-----b----c---d--|');
const e1subs = '^ ! ';
const expected = '---- ';
const unsub = ' ! ';
expectObservable(e1.first(), unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should not break unsubscription chains when result is unsubscribed explicitly', () => {
const e1 = hot('--a--^-----b----c---d--|');
const e1subs = '^ ! ';
const expected = '---- ';
const unsub = ' ! ';
const result = e1
.mergeMap((x: string) => Observable.of(x))
.first()
.mergeMap((x: string) => Observable.of(x));
expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should return first value that matches a predicate', () => {
const e1 = hot('--a-^--b--c--a--c--|');
const expected = '------(c|)';
const sub = '^ !';
const predicate = function (value) {
return value === 'c';
};
expectObservable(e1.first(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should return first value that matches a predicate for odd numbers', () => {
const e1 = hot('--a-^--b--c--d--e--|', {a: 1, b: 2, c: 3, d: 4, e: 5});
const expected = '------(c|)';
const sub = '^ !';
const predicate = function (value) {
return value % 2 === 1;
};
expectObservable(e1.first(predicate)).toBe(expected, {c: 3});
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should error when no value matches the predicate', () => {
const e1 = hot('--a-^--b--c--a--c--|');
const expected = '---------------#';
const sub = '^ !';
const predicate = function (value) {
return value === 's';
};
expectObservable(e1.first(predicate)).toBe(expected, null, new Rx.EmptyError());
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should return the default value when no value matches the predicate', () => {
const e1 = hot('--a-^--b--c--a--c--|');
const expected = '---------------(d|)';
const sub = '^ !';
const predicate = function (value) {
return value === 's';
};
expectObservable(e1.first(predicate, null, 'd')).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should propagate error when no value matches the predicate', () => {
const e1 = hot('--a-^--b--c--a--#');
const expected = '------------#';
const sub = '^ !';
const predicate = function (value) {
return value === 's';
};
expectObservable(e1.first(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should return first value that matches the index in the predicate', () => {
const e1 = hot('--a-^--b--c--a--c--|');
const expected = '---------(a|)';
const sub = '^ !';
const predicate = function (value, index) {
return index === 2;
};
expectObservable(e1.first(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should propagate error from predicate', () => {
const e1 = hot('--a-^--b--c--d--e--|', {a: 1, b: 2, c: 3, d: 4, e: 5});
const expected = '---------#';
const sub = '^ !';
const predicate = function (value) {
if (value < 4) {
return false;
} else {
throw 'error';
}
};
expectObservable(e1.first(predicate)).toBe(expected, null, 'error');
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should support a result selector argument', () => {
const e1 = hot('--a--^---b---c---d---e--|');
const expected = '--------(x|)';
const sub = '^ !';
const predicate = function (x) { return x === 'c'; };
const resultSelector = function (x, i) {
expect(i).to.equal(1);
expect(x).to.equal('c');
return 'x';
};
expectObservable(e1.first(predicate, resultSelector)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
it('should raise error when result selector throws', () => {
const e1 = hot('--a--^---b---c---d---e--|');
const expected = '--------#';
const sub = '^ !';
const predicate = function (x) { return x === 'c'; };
const resultSelector = function (x, i) {
throw 'error';
};
expectObservable(e1.first(predicate, resultSelector)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
});