Skip to content

Commit 3d833f3

Browse files
Andre Medeirosbenlesh
authored andcommitted
test(filter): add filter tests from RxJS legacy
Add most RxJS legacy tests. Just 3 tests are missing still, which are related to test helpers not yet available in RxJS Next.
1 parent 75427a9 commit 3d833f3

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

spec/operators/filter-spec.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,170 @@ describe('Observable.prototype.filter()', function () {
77
return (+x) % 2 === 1;
88
}
99

10+
function isPrime(i) {
11+
if (+i <= 1) { return false; }
12+
var max = Math.floor(Math.sqrt(+i));
13+
for (var j = 2; j <= max; ++j) {
14+
if (+i % j === 0) { return false; }
15+
}
16+
return true;
17+
}
18+
1019
it('should filter out even values', function () {
1120
var source = hot('--0--1--2--3--4--|');
1221
var expected = '-----1-----3-----|';
1322

1423
expectObservable(source.filter(oddFilter)).toBe(expected);
1524
});
1625

26+
it('should filter in only prime numbers', function () {
27+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
28+
var expected = '--3---5----7-------|';
29+
30+
expectObservable(source.filter(isPrime)).toBe(expected);
31+
});
32+
33+
it('should filter with an always-true predicate', function () {
34+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
35+
var expected = '--3-4-5-6--7-8--9--|';
36+
var predicate = function () { return true; };
37+
38+
expectObservable(source.filter(predicate)).toBe(expected);
39+
});
40+
41+
it('should filter with an always-false predicate', function () {
42+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
43+
var expected = '-------------------|';
44+
var predicate = function () { return false; };
45+
46+
expectObservable(source.filter(predicate)).toBe(expected);
47+
});
48+
49+
it('should filter in only prime numbers, source unsubscribes early', function () {
50+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
51+
var unsub = '------------!';
52+
var expected = '--3---5----7-';
53+
54+
expectObservable(source.filter(isPrime), unsub).toBe(expected);
55+
});
56+
57+
it('should filter in only prime numbers, source throws', function () {
58+
var source = hot('-1--2--^-3-4-5-6--7-8--9--#');
59+
var expected = '--3---5----7-------#';
60+
61+
expectObservable(source.filter(isPrime)).toBe(expected);
62+
});
63+
64+
it('should filter in only prime numbers, but predicate throws', function () {
65+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
66+
var expected = '--3---5-#';
67+
68+
var invoked = 0;
69+
var predicate = function (x) {
70+
invoked++;
71+
if (invoked === 4) {
72+
throw 'error';
73+
}
74+
return isPrime(x);
75+
};
76+
77+
expectObservable(source.filter(predicate)).toBe(expected);
78+
});
79+
80+
it('should filter in only prime numbers, predicate with index', function () {
81+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
82+
var expected = '--3--------7-------|';
83+
84+
var predicate = function (x, i) {
85+
return isPrime((+x) + i * 10);
86+
};
87+
88+
expectObservable(source.filter(predicate)).toBe(expected);
89+
});
90+
91+
it('should filter in only prime numbers, predicate with index, ' +
92+
'source unsubscribes early',
93+
function () {
94+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
95+
var unsub = '------------!';
96+
var expected = '--3--------7-';
97+
98+
var predicate = function (x, i) {
99+
return isPrime((+x) + i * 10);
100+
};
101+
102+
expectObservable(source.filter(predicate), unsub).toBe(expected);
103+
});
104+
105+
it('should filter in only prime numbers, predicate with index, source throws', function () {
106+
var source = hot('-1--2--^-3-4-5-6--7-8--9--#');
107+
var expected = '--3--------7-------#';
108+
109+
var predicate = function (x, i) {
110+
return isPrime((+x) + i * 10);
111+
};
112+
113+
expectObservable(source.filter(predicate)).toBe(expected);
114+
});
115+
116+
it('should filter in only prime numbers, predicate with index and throws', function () {
117+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
118+
var expected = '--3-----#';
119+
120+
var invoked = 0;
121+
var predicate = function (x, i) {
122+
invoked++;
123+
if (invoked === 4) {
124+
throw 'error';
125+
}
126+
return isPrime((+x) + i * 10);
127+
};
128+
129+
expectObservable(source.filter(predicate)).toBe(expected);
130+
});
131+
132+
it('should compose with another filter to allow multiples of six', function () {
133+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
134+
var expected = '--------6----------|';
135+
136+
expectObservable(
137+
source
138+
.filter(function (x) { return x % 2 === 0; })
139+
.filter(function (x) { return x % 3 === 0; })
140+
).toBe(expected);
141+
});
142+
143+
it('should be able to accept and use a thisArg', function () {
144+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
145+
var expected = '--------6----------|';
146+
147+
function Filterer() {
148+
this.filter1 = function (x) { return x % 2 === 0; };
149+
this.filter2 = function (x) { return x % 3 === 0; };
150+
}
151+
152+
var filterer = new Filterer();
153+
154+
expectObservable(
155+
source
156+
.filter(function (x) { return this.filter1(x); }, filterer)
157+
.filter(function (x) { return this.filter2(x); }, filterer)
158+
.filter(function (x) { return this.filter1(x); }, filterer)
159+
).toBe(expected);
160+
});
161+
162+
it('should be able to use filter and map composed', function () {
163+
var source = hot('-1--2--^-3-4-5-6--7-8--9--|');
164+
var expected = '----a---b----c-----|';
165+
var values = { a: 16, b: 36, c: 64 };
166+
167+
expectObservable(
168+
source
169+
.filter(function (x) { return x % 2 === 0; })
170+
.map(function (x) { return x * x; })
171+
).toBe(expected, values);
172+
});
173+
17174
it('should propagate errors from the source', function () {
18175
var source = hot('--0--1--2--3--4--#');
19176
var expected = '-----1-----3-----#';

0 commit comments

Comments
 (0)