-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathfirst-spec.ts
293 lines (239 loc) · 10.2 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
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
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
declare const { asDiagram };
declare const hot: typeof marbleTestingSignature.hot;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.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);
});
it('should support type guards without breaking previous behavior', () => {
// tslint:disable no-unused-variable
// type guards with interfaces and classes
{
interface Bar { bar?: string; }
interface Baz { baz?: number; }
class Foo implements Bar, Baz { constructor(public bar: string = 'name', public baz: number = 42) {} }
const isBar = (x: any): x is Bar => x && (<Bar>x).bar !== undefined;
const isBaz = (x: any): x is Baz => x && (<Baz>x).baz !== undefined;
const foo: Foo = new Foo();
Observable.of(foo).first()
.subscribe(x => x.baz); // x is Foo
Observable.of(foo).first(foo => foo.bar === 'name')
.subscribe(x => x.baz); // x is still Foo
Observable.of(foo).first(isBar)
.subscribe(x => x.bar); // x is Bar!
const foobar: Bar = new Foo(); // type is the interface, not the class
Observable.of(foobar).first()
.subscribe(x => x.bar); // x is Bar
Observable.of(foobar).first(foobar => foobar.bar === 'name')
.subscribe(x => x.bar); // x is still Bar
Observable.of(foobar).first(isBaz)
.subscribe(x => x.baz); // x is Baz!
const barish = { bar: 'quack', baz: 42 }; // type can quack like a Bar
Observable.of(barish).first()
.subscribe(x => x.baz); // x is still { bar: string; baz: number; }
Observable.of(barish).first(x => x.bar === 'quack')
.subscribe(x => x.bar); // x is still { bar: string; baz: number; }
Observable.of(barish).first(isBar)
.subscribe(x => x.bar); // x is Bar!
}
// type guards with primitive types
{
const xs: Rx.Observable<string | number> = Observable.from([ 1, 'aaa', 3, 'bb' ]);
// This type guard will narrow a `string | number` to a string in the examples below
const isString = (x: string | number): x is string => typeof x === 'string';
// missing predicate preserves the type
xs.first().subscribe(x => x); // x is still string | number
// After the type guard `first` predicates, the type is narrowed to string
xs.first(isString)
.subscribe(s => s.length); // s is string
xs.first(isString, s => s.substr(0)) // s is string in predicate
.subscribe(s => s.length); // s is string
// boolean predicates preserve the type
xs.first(x => typeof x === 'string')
.subscribe(x => x); // x is still string | number
xs.first(x => !!x, x => x)
.subscribe(x => x); // x is still string | number
xs.first(x => typeof x === 'string', x => x, '') // default is string; x remains string | number
.subscribe(x => x); // x is still string | number
// `first` still uses the `resultSelector` return type, if it exists.
xs.first(x => typeof x === 'string', x => ({ str: `${x}` })) // x remains string | number
.subscribe(o => o.str); // o is { str: string }
xs.first(x => typeof x === 'string', x => ({ str: `${x}` }), { str: '' })
.subscribe(o => o.str); // o is { str: string }
}
// tslint:disable enable
});
});