/
filter-spec.ts
342 lines (277 loc) · 11.5 KB
/
filter-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
import { expect } from 'chai';
import { filter, tap, map, mergeMap } from 'rxjs/operators';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { of, Observable, from } from 'rxjs';
declare function asDiagram(arg: string): Function;
/** @test {filter} */
describe('filter operator', () => {
function oddFilter(x: number | string) {
return (+x) % 2 === 1;
}
function isPrime(i: number | string) {
if (+i <= 1) { return false; }
const max = Math.floor(Math.sqrt(+i));
for (let j = 2; j <= max; ++j) {
if (+i % j === 0) { return false; }
}
return true;
}
asDiagram('filter(x => x % 2 === 1)')('should filter out even values', () => {
const source = hot('--0--1--2--3--4--|');
const subs = '^ !';
const expected = '-----1-----3-----|';
expectObservable(source.pipe(filter(oddFilter))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should filter in only prime numbers', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ !';
const expected = '--3---5----7-------|';
expectObservable(source.pipe(filter(isPrime))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should filter with an always-true predicate', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '--3-4-5-6--7-8--9--|';
const predicate = () => { return true; };
expectObservable(source.pipe(filter(predicate))).toBe(expected);
});
it('should filter with an always-false predicate', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '-------------------|';
const predicate = () => { return false; };
expectObservable(source.pipe(filter(predicate))).toBe(expected);
});
it('should filter in only prime numbers, source unsubscribes early', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const unsub = ' ! ';
const expected = '--3---5----7- ';
expectObservable(source.pipe(filter(isPrime)), unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should filter in only prime numbers, source throws', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--#');
const subs = '^ !';
const expected = '--3---5----7-------#';
expectObservable(source.pipe(filter(isPrime))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should filter in only prime numbers, but predicate throws', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const expected = '--3---5-# ';
let invoked = 0;
function predicate(x: any, index: number) {
invoked++;
if (invoked === 4) {
throw 'error';
}
return isPrime(x);
}
expectObservable((<any>source).pipe(filter(predicate))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should filter in only prime numbers, predicate with index', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ !';
const expected = '--3--------7-------|';
function predicate(x: any, i: number) {
return isPrime((+x) + i * 10);
}
expectObservable((<any>source).pipe(filter(predicate))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should invoke predicate once for each checked value', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '--3---5----7-------|';
let invoked = 0;
const predicate = (x: any) => {
invoked++;
return isPrime(x);
};
const r = source.pipe(
filter(predicate),
tap(null, null, () => {
expect(invoked).to.equal(7);
})
);
expectObservable(r).toBe(expected);
});
it('should filter in only prime numbers, predicate with index, ' +
'source unsubscribes early', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const unsub = ' ! ';
const expected = '--3--------7- ';
function predicate(x: any, i: number) {
return isPrime((+x) + i * 10);
}
expectObservable((<any>source).pipe(filter(predicate)), unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should filter in only prime numbers, predicate with index, source throws', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--#');
const subs = '^ !';
const expected = '--3--------7-------#';
function predicate(x: any, i: number) {
return isPrime((+x) + i * 10);
}
expectObservable((<any>source).pipe(filter(predicate))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should filter in only prime numbers, predicate with index and throws', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const expected = '--3-----# ';
let invoked = 0;
function predicate(x: any, i: number) {
invoked++;
if (invoked === 4) {
throw 'error';
}
return isPrime((+x) + i * 10);
}
expectObservable((<any>source).pipe(filter(predicate))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should compose with another filter to allow multiples of six', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '--------6----------|';
expectObservable(
source.pipe(
filter((x: string) => (+x) % 2 === 0),
filter((x: string) => (+x) % 3 === 0)
)
).toBe(expected);
});
it('should be able to accept and use a thisArg', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '--------6----------|';
class Filterer {
filter1 = (x: string) => (+x) % 2 === 0;
filter2 = (x: string) => (+x) % 3 === 0;
}
const filterer = new Filterer();
expectObservable(
source.pipe(
filter(function (this: any, x) { return this.filter1(x); }, filterer),
filter(function (this: any, x) { return this.filter2(x); }, filterer),
filter(function (this: any, x) { return this.filter1(x); }, filterer)
)
).toBe(expected);
});
it('should be able to use filter and map composed', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '----a---b----c-----|';
const values = { a: 16, b: 36, c: 64 };
expectObservable(
source.pipe(
filter((x: string) => (+x) % 2 === 0),
map((x: string) => (+x) * (+x))
)
).toBe(expected, values);
});
it('should propagate errors from the source', () => {
const source = hot('--0--1--2--3--4--#');
const subs = '^ !';
const expected = '-----1-----3-----#';
expectObservable(source.pipe(filter(oddFilter))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should support Observable.empty', () => {
const source = cold('|');
const subs = '(^!)';
const expected = '|';
expectObservable(source.pipe(filter(oddFilter))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should support Observable.never', () => {
const source = cold('-');
const subs = '^';
const expected = '-';
expectObservable(source.pipe(filter(oddFilter))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should support Observable.throw', () => {
const source = cold('#');
const subs = '(^!)';
const expected = '#';
expectObservable(source.pipe(filter(oddFilter))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should send errors down the error path', (done: MochaDone) => {
of(42).pipe(
filter(<any>((x: number, index: number) => {
throw 'bad';
}))
).subscribe((x: number) => {
done(new Error('should not be called'));
}, (err: any) => {
expect(err).to.equal('bad');
done();
}, () => {
done(new Error('should not be called'));
});
});
it('should not break unsubscription chain when unsubscribed explicitly', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const unsub = ' ! ';
const expected = '--3---5----7- ';
const r = source.pipe(
mergeMap((x: any) => of(x)),
filter(isPrime),
mergeMap((x: any) => of(x))
);
expectObservable(r, unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
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();
of(foo).pipe(filter(foo => foo.baz === 42))
.subscribe(x => x.baz); // x is still Foo
of(foo).pipe(filter(isBar))
.subscribe(x => x.bar); // x is Bar!
const foobar: Bar = new Foo(); // type is interface, not the class
of(foobar).pipe(filter(foobar => foobar.bar === 'name'))
.subscribe(x => x.bar); // <-- x is still Bar
of(foobar).pipe(filter(isBar))
.subscribe(x => x.bar); // <--- x is Bar!
const barish = { bar: 'quack', baz: 42 }; // type can quack like a Bar
of(barish).pipe(filter(x => x.bar === 'quack'))
.subscribe(x => x.bar); // x is still { bar: string; baz: number; }
of(barish).pipe(filter(isBar))
.subscribe(bar => bar.bar); // x is Bar!
}
// type guards with primitive types
{
const xs: Observable<string | number> = 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';
xs.pipe(filter(isString))
.subscribe(s => s.length); // s is string
// In contrast, this type of regular boolean predicate still maintains the original type
xs.pipe(filter(x => typeof x === 'number'))
.subscribe(x => x); // x is still string | number
xs.pipe(filter((x, i) => typeof x === 'number' && x > i))
.subscribe(x => x); // x is still string | number
}
// tslint:disable enable
});
it('should support Boolean as a predicate', () => {
const source = hot('-t--f--^-t-f-t-f--t-f--f--|', { t: 1, f: 0 });
const subs = '^ !';
const expected = '--t---t----t-------|';
expectObservable(source.pipe(filter(Boolean))).toBe(expected, { t: 1, f: 0 });
expectSubscriptions(source.subscriptions).toBe(subs);
});
});