forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-spec.ts
254 lines (203 loc) · 7.74 KB
/
max-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
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions};
const Observable = Rx.Observable;
/** @test {max} */
describe('Observable.prototype.max', () => {
asDiagram('max')('should find the max of values of an observable', () => {
const e1 = hot('--a--b--c--|', { a: 42, b: -1, c: 3 });
const subs = '^ !';
const expected = '-----------(x|)';
expectObservable((<any>e1).max()).toBe(expected, { x: 42 });
expectSubscriptions(e1.subscriptions).toBe(subs);
});
it('should be never when source is never', () => {
const e1 = cold('-');
const e1subs = '^';
const expected = '-';
expectObservable((<any>e1).max()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should be zero when source is empty', () => {
const e1 = cold('|');
const e1subs = '(^!)';
const expected = '|';
expectObservable((<any>e1).max()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should be never when source doesn\'t complete', () => {
const e1 = hot('--x--^--y--');
const e1subs = '^ ';
const expected = '------';
expectObservable((<any>e1).max()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should be completes when source doesn\'t have values', () => {
const e1 = hot('-x-^---|');
const e1subs = '^ !';
const expected = '----|';
expectObservable((<any>e1).max()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should max the unique value of an observable', () => {
const e1 = hot('-x-^--y--|', { y: 42 });
const e1subs = '^ !';
const expected = '------(w|)';
expectObservable((<any>e1).max()).toBe(expected, { w: 42 });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should max the values of an ongoing hot observable', () => {
const e1 = hot('--a-^-b--c--d--|', { a: 42, b: -1, c: 0, d: 666 });
const subs = '^ !';
const expected = '-----------(x|)';
expectObservable((<any>e1).max()).toBe(expected, { x: 666 });
expectSubscriptions(e1.subscriptions).toBe(subs);
});
it('should allow unsubscribing explicitly and early', () => {
const e1 = hot('--a--b--c--|', { a: 42, b: -1, c: 0 });
const unsub = ' ! ';
const subs = '^ ! ';
const expected = '------- ';
expectObservable((<any>e1).max(), unsub).toBe(expected, { x: 42 });
expectSubscriptions(e1.subscriptions).toBe(subs);
});
it('should not break unsubscription chains when result is unsubscribed explicitly', () => {
const source = hot('--a--b--c--|', { a: 42, b: -1, c: 0 });
const subs = '^ ! ';
const expected = '------- ';
const unsub = ' ! ';
const result = (<any>source)
.mergeMap((x: string) => Observable.of(x))
.max()
.mergeMap((x: string) => Observable.of(x));
expectObservable(result, unsub).toBe(expected, { x: 42 });
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should max a range() source observable', (done: MochaDone) => {
(<any>Rx.Observable.range(1, 10000)).max().subscribe(
(value: number) => {
expect(value).to.equal(10000);
}, (x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
it('should max a range().skip(1) source observable', (done: MochaDone) => {
(<any>Rx.Observable.range(1, 10)).skip(1).max().subscribe(
(value: number) => {
expect(value).to.equal(10);
}, (x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
it('should max a range().take(1) source observable', (done: MochaDone) => {
(<any>Rx.Observable.range(1, 10)).take(1).max().subscribe(
(value: number) => {
expect(value).to.equal(1);
}, (x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
it('should work with error', () => {
const e1 = hot('-x-^--y--z--#', { x: 1, y: 2, z: 3 }, 'too bad');
const e1subs = '^ !';
const expected = '---------#';
expectObservable((<any>e1).max()).toBe(expected, null, 'too bad');
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should work with throw', () => {
const e1 = cold('#');
const e1subs = '(^!)';
const expected = '#';
expectObservable((<any>e1).max()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a constant predicate on an empty hot observable', () => {
const e1 = hot('-x-^---|');
const e1subs = '^ !';
const expected = '----|';
const predicate = function (x, y) {
return 42;
};
expectObservable((<any>e1).max(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a constant predicate on an never hot observable', () => {
const e1 = hot('-x-^----');
const e1subs = '^ ';
const expected = '-----';
const predicate = function (x, y) {
return 42;
};
expectObservable((<any>e1).max(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a constant predicate on a simple hot observable', () => {
const e1 = hot('-x-^-a-|', { a: 1 });
const e1subs = '^ !';
const expected = '----(w|)';
const predicate = function () {
return 42;
};
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: 1 });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a constant predicate on observable with many values', () => {
const e1 = hot('-x-^-a-b-c-d-e-f-g-|');
const e1subs = '^ !';
const expected = '----------------(w|)';
const predicate = () => {
return 42;
};
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: 42 });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a predicate on observable with many values', () => {
const e1 = hot('-a-^-b--c--d-|', { a: 42, b: -1, c: 0, d: 666 });
const e1subs = '^ !';
const expected = '----------(w|)';
const predicate = function (x, y) {
return Math.min(x, y);
};
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: -1 });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a predicate for string on observable with many values', () => {
const e1 = hot('-1-^-2--3--4-|');
const e1subs = '^ !';
const expected = '----------(w|)';
const predicate = function (x, y) {
return x > y ? x : y;
};
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: '4' });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a constant predicate on observable that throws', () => {
const e1 = hot('-1-^---#');
const e1subs = '^ !';
const expected = '----#';
const predicate = () => {
return 42;
};
expectObservable((<any>e1).max(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle a predicate that throws, on observable with many values', () => {
const e1 = hot('-1-^-2--3--|');
const e1subs = '^ ! ';
const expected = '-----# ';
const predicate = function (x, y) {
if (y === '3') {
throw 'error';
}
return x > y ? x : y;
};
expectObservable((<any>e1).max(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});