forked from ReactiveX/rxjs
/
TestScheduler-spec.ts
490 lines (417 loc) · 17.6 KB
/
TestScheduler-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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import { expect } from 'chai';
import { hot, cold, expectObservable, expectSubscriptions, time } from '../helpers/marble-testing';
import { AsyncScheduler } from 'rxjs/internal/scheduler/AsyncScheduler';
import { TestScheduler } from 'rxjs/testing';
import { Observable, NEVER, EMPTY, Subject, of, concat, merge, Notification } from 'rxjs';
import { delay, debounceTime, concatMap } from 'rxjs/operators';
declare const rxTestScheduler: TestScheduler;
/** @test {TestScheduler} */
describe('TestScheduler', () => {
it('should exist', () => {
expect(TestScheduler).exist;
expect(TestScheduler).to.be.a('function');
});
it('should have frameTimeFactor set initially', () => {
expect(TestScheduler.frameTimeFactor).to.equal(10);
});
describe('parseMarbles()', () => {
it('should parse a marble string into a series of notifications and types', () => {
const result = TestScheduler.parseMarbles('-------a---b---|', { a: 'A', b: 'B' });
expect(result).deep.equal([
{ frame: 70, notification: Notification.createNext('A') },
{ frame: 110, notification: Notification.createNext('B') },
{ frame: 150, notification: Notification.createComplete() }
]);
});
it('should parse a marble string, allowing spaces too', () => {
const result = TestScheduler.parseMarbles('--a--b--| ', { a: 'A', b: 'B' });
expect(result).deep.equal([
{ frame: 20, notification: Notification.createNext('A') },
{ frame: 50, notification: Notification.createNext('B') },
{ frame: 80, notification: Notification.createComplete() }
]);
});
it('should parse a marble string with a subscription point', () => {
const result = TestScheduler.parseMarbles('---^---a---b---|', { a: 'A', b: 'B' });
expect(result).deep.equal([
{ frame: 40, notification: Notification.createNext('A') },
{ frame: 80, notification: Notification.createNext('B') },
{ frame: 120, notification: Notification.createComplete() }
]);
});
it('should parse a marble string with an error', () => {
const result = TestScheduler.parseMarbles('-------a---b---#', { a: 'A', b: 'B' }, 'omg error!');
expect(result).deep.equal([
{ frame: 70, notification: Notification.createNext('A') },
{ frame: 110, notification: Notification.createNext('B') },
{ frame: 150, notification: Notification.createError('omg error!') }
]);
});
it('should default in the letter for the value if no value hash was passed', () => {
const result = TestScheduler.parseMarbles('--a--b--c--');
expect(result).deep.equal([
{ frame: 20, notification: Notification.createNext('a') },
{ frame: 50, notification: Notification.createNext('b') },
{ frame: 80, notification: Notification.createNext('c') },
]);
});
it('should handle grouped values', () => {
const result = TestScheduler.parseMarbles('---(abc)---');
expect(result).deep.equal([
{ frame: 30, notification: Notification.createNext('a') },
{ frame: 30, notification: Notification.createNext('b') },
{ frame: 30, notification: Notification.createNext('c') }
]);
});
it('should ignore whitespace when runMode=true', () => {
const runMode = true;
const result = TestScheduler.parseMarbles(' -a - b - c | ', { a: 'A', b: 'B', c: 'C' }, undefined, undefined, runMode);
expect(result).deep.equal([
{ frame: 10, notification: Notification.createNext('A') },
{ frame: 30, notification: Notification.createNext('B') },
{ frame: 50, notification: Notification.createNext('C') },
{ frame: 60, notification: Notification.createComplete() }
]);
});
it('should suppport time progression syntax when runMode=true', () => {
const runMode = true;
const result = TestScheduler.parseMarbles('10.2ms a 1.2s b 1m c|', { a: 'A', b: 'B', c: 'C' }, undefined, undefined, runMode);
expect(result).deep.equal([
{ frame: 10.2, notification: Notification.createNext('A') },
{ frame: 10.2 + 10 + (1.2 * 1000), notification: Notification.createNext('B') },
{ frame: 10.2 + 10 + (1.2 * 1000) + 10 + (1000 * 60), notification: Notification.createNext('C') },
{ frame: 10.2 + 10 + (1.2 * 1000) + 10 + (1000 * 60) + 10, notification: Notification.createComplete() }
]);
});
});
describe('parseMarblesAsSubscriptions()', () => {
it('should parse a subscription marble string into a subscriptionLog', () => {
const result = TestScheduler.parseMarblesAsSubscriptions('---^---!-');
expect(result.subscribedFrame).to.equal(30);
expect(result.unsubscribedFrame).to.equal(70);
});
it('should parse a subscription marble string with an unsubscription', () => {
const result = TestScheduler.parseMarblesAsSubscriptions('---^-');
expect(result.subscribedFrame).to.equal(30);
expect(result.unsubscribedFrame).to.equal(Number.POSITIVE_INFINITY);
});
it('should parse a subscription marble string with a synchronous unsubscription', () => {
const result = TestScheduler.parseMarblesAsSubscriptions('---(^!)-');
expect(result.subscribedFrame).to.equal(30);
expect(result.unsubscribedFrame).to.equal(30);
});
it('should ignore whitespace when runMode=true', () => {
const runMode = true;
const result = TestScheduler.parseMarblesAsSubscriptions(' - - - - ^ - - ! -- - ', runMode);
expect(result.subscribedFrame).to.equal(40);
expect(result.unsubscribedFrame).to.equal(70);
});
it('should suppport time progression syntax when runMode=true', () => {
const runMode = true;
const result = TestScheduler.parseMarblesAsSubscriptions('10.2ms ^ 1.2s - 1m !', runMode);
expect(result.subscribedFrame).to.equal(10.2);
expect(result.unsubscribedFrame).to.equal(10.2 + 10 + (1.2 * 1000) + 10 + (1000 * 60));
});
});
describe('createTime()', () => {
it('should parse a simple time marble string to a number', () => {
const scheduler = new TestScheduler(null);
const time = scheduler.createTime('-----|');
expect(time).to.equal(50);
});
it('should throw if not given good marble input', () => {
const scheduler = new TestScheduler(null);
expect(() => {
scheduler.createTime('-a-b-#');
}).to.throw();
});
});
describe('createColdObservable()', () => {
it('should create a cold observable', () => {
const expected = ['A', 'B'];
const scheduler = new TestScheduler(null);
const source = scheduler.createColdObservable('--a---b--|', { a: 'A', b: 'B' });
expect(source).to.be.an.instanceOf(Observable);
source.subscribe(x => {
expect(x).to.equal(expected.shift());
});
scheduler.flush();
expect(expected.length).to.equal(0);
});
});
describe('createHotObservable()', () => {
it('should create a hot observable', () => {
const expected = ['A', 'B'];
const scheduler = new TestScheduler(null);
const source = scheduler.createHotObservable('--a---b--|', { a: 'A', b: 'B' });
expect(source).to.be.an.instanceof(Subject);
source.subscribe(x => {
expect(x).to.equal(expected.shift());
});
scheduler.flush();
expect(expected.length).to.equal(0);
});
});
describe('jasmine helpers', () => {
describe('rxTestScheduler', () => {
it('should exist', () => {
expect(rxTestScheduler).to.be.an.instanceof(TestScheduler);
});
});
describe('cold()', () => {
it('should exist', () => {
expect(cold).to.exist;
expect(cold).to.be.a('function');
});
it('should create a cold observable', () => {
const expected = [1, 2];
const source = cold('-a-b-|', { a: 1, b: 2 });
source.subscribe((x: number) => {
expect(x).to.equal(expected.shift());
}, null, () => {
expect(expected.length).to.equal(0);
});
expectObservable(source).toBe('-a-b-|', { a: 1, b: 2 });
});
});
describe('hot()', () => {
it('should exist', () => {
expect(hot).to.exist;
expect(hot).to.be.a('function');
});
it('should create a hot observable', () => {
const source = hot('---^-a-b-|', { a: 1, b: 2 });
expect(source).to.be.an.instanceOf(Subject);
expectObservable(source).toBe('--a-b-|', { a: 1, b: 2 });
});
});
describe('time()', () => {
it('should exist', () => {
expect(time).to.exist;
expect(time).to.be.a('function');
});
it('should parse a simple time marble string to a number', () => {
expect(time('-----|')).to.equal(50);
});
});
describe('expectObservable()', () => {
it('should exist', () => {
expect(expectObservable).to.exist;
expect(expectObservable).to.be.a('function');
});
it('should return an object with a toBe function', () => {
expect(expectObservable(of(1)).toBe).to.be.a('function');
});
it('should append to flushTests array', () => {
expectObservable(EMPTY);
expect((<any>rxTestScheduler).flushTests.length).to.equal(1);
});
it('should handle empty', () => {
expectObservable(EMPTY).toBe('|', {});
});
it('should handle never', () => {
expectObservable(NEVER).toBe('-', {});
expectObservable(NEVER).toBe('---', {});
});
it('should accept an unsubscription marble diagram', () => {
const source = hot('---^-a-b-|');
const unsubscribe = '---!';
const expected = '--a';
expectObservable(source, unsubscribe).toBe(expected);
});
it('should accept a subscription marble diagram', () => {
const source = hot('-a-b-c|');
const subscribe = '---^';
const expected = '---b-c|';
expectObservable(source, subscribe).toBe(expected);
});
});
describe('expectSubscriptions()', () => {
it('should exist', () => {
expect(expectSubscriptions).to.exist;
expect(expectSubscriptions).to.be.a('function');
});
it('should return an object with a toBe function', () => {
expect(expectSubscriptions([]).toBe).to.be.a('function');
});
it('should append to flushTests array', () => {
expectSubscriptions([]);
expect((<any>rxTestScheduler).flushTests.length).to.equal(1);
});
it('should assert subscriptions of a cold observable', () => {
const source = cold('---a---b-|');
const subs = '^--------!';
expectSubscriptions(source.subscriptions).toBe(subs);
source.subscribe();
});
});
describe('end-to-end helper tests', () => {
it('should be awesome', () => {
const values = { a: 1, b: 2 };
const myObservable = cold('---a---b--|', values);
const subs = '^---------!';
expectObservable(myObservable).toBe('---a---b--|', values);
expectSubscriptions(myObservable.subscriptions).toBe(subs);
});
it('should support testing metastreams', () => {
const x = cold('-a-b|');
const y = cold('-c-d|');
const myObservable = hot('---x---y----|', { x: x, y: y });
const expected = '---x---y----|';
const expectedx = cold('-a-b|');
const expectedy = cold('-c-d|');
expectObservable(myObservable).toBe(expected, { x: expectedx, y: expectedy });
});
});
});
describe('TestScheduler.run()', () => {
const assertDeepEquals = (actual: any, expected: any) => {
expect(actual).deep.equal(expected);
};
describe('marble diagrams', () => {
it('should ignore whitespace', () => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const input = cold(' -a - b - c | ');
const output = input.pipe(
concatMap(d => of(d).pipe(
delay(10)
))
);
const expected = ' -- 9ms a 9ms b 9ms (c|) ';
expectObservable(output).toBe(expected);
expectSubscriptions(input.subscriptions).toBe(' ^- - - - - !');
});
});
it('should support time progression syntax', () => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(({ cold, hot, flush, expectObservable, expectSubscriptions }) => {
const output = cold('10.2ms a 1.2s b 1m c|');
const expected = ' 10.2ms a 1.2s b 1m c|';
expectObservable(output).toBe(expected);
});
});
});
it('should provide the correct helpers', () => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(({ cold, hot, flush, expectObservable, expectSubscriptions }) => {
expect(cold).to.be.a('function');
expect(hot).to.be.a('function');
expect(flush).to.be.a('function');
expect(expectObservable).to.be.a('function');
expect(expectSubscriptions).to.be.a('function');
const obs1 = cold('-a-c-e|');
const obs2 = hot(' ^-b-d-f|');
const output = merge(obs1, obs2);
const expected = ' -abcdef|';
expectObservable(output).toBe(expected);
expectSubscriptions(obs1.subscriptions).toBe('^-----!');
expectSubscriptions(obs2.subscriptions).toBe('^------!');
});
});
it('should have each frame represent a single virtual millisecond', () => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(({ cold, expectObservable }) => {
const output = cold('-a-b-c--------|').pipe(
debounceTime(5)
);
const expected = ' ------ 4ms c---|';
expectObservable(output).toBe(expected);
});
});
it('should have no maximum frame count', () => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(({ cold, expectObservable }) => {
const output = cold('-a|').pipe(
delay(1000 * 10)
);
const expected = ' - 10s a|';
expectObservable(output).toBe(expected);
});
});
it('should make operators that use AsyncScheduler automatically use TestScheduler for actual scheduling', () => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(({ cold, expectObservable }) => {
const output = cold('-a-b-c--------|').pipe(
debounceTime(5)
);
const expected = ' ----------c---|';
expectObservable(output).toBe(expected);
});
});
it('should flush automatically', () => {
const testScheduler = new TestScheduler((actual, expected) => {
expect(actual).deep.equal(expected);
});
testScheduler.run(({ cold, expectObservable }) => {
const output = cold('-a-b-c|').pipe(
concatMap(d => of(d).pipe(
delay(10)
))
);
const expected = ' -- 9ms a 9ms b 9ms (c|)';
expectObservable(output).toBe(expected);
expect(testScheduler['flushTests'].length).to.equal(1);
expect(testScheduler['actions'].length).to.equal(1);
});
expect(testScheduler['flushTests'].length).to.equal(0);
expect(testScheduler['actions'].length).to.equal(0);
});
it('should support explicit flushing', () => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(({ cold, expectObservable, flush }) => {
const output = cold('-a-b-c|').pipe(
concatMap(d => of(d).pipe(
delay(10)
))
);
const expected = ' -- 9ms a 9ms b 9ms (c|)';
expectObservable(output).toBe(expected);
expect(testScheduler['flushTests'].length).to.equal(1);
expect(testScheduler['actions'].length).to.equal(1);
flush();
expect(testScheduler['flushTests'].length).to.equal(0);
expect(testScheduler['actions'].length).to.equal(0);
});
expect(testScheduler['flushTests'].length).to.equal(0);
expect(testScheduler['actions'].length).to.equal(0);
});
it('should pass-through return values, e.g. Promises', (done) => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(() => {
return Promise.resolve('foo');
}).then(value => {
expect(value).to.equal('foo');
done();
});
});
it('should restore changes upon thrown errors', () => {
const testScheduler = new TestScheduler(assertDeepEquals);
const frameTimeFactor = TestScheduler['frameTimeFactor'];
const maxFrames = testScheduler.maxFrames;
const runMode = testScheduler['runMode'];
const delegate = AsyncScheduler.delegate;
try {
testScheduler.run(() => {
throw new Error('kaboom!');
});
} catch { /* empty */ }
expect(TestScheduler['frameTimeFactor']).to.equal(frameTimeFactor);
expect(testScheduler.maxFrames).to.equal(maxFrames);
expect(testScheduler['runMode']).to.equal(runMode);
expect(AsyncScheduler.delegate).to.equal(delegate);
});
it('should flush expectations correctly', () => {
expect(() => {
const testScheduler = new TestScheduler(assertDeepEquals);
testScheduler.run(({ cold, expectObservable, flush }) => {
expectObservable(cold('-x')).toBe('-x');
expectObservable(cold('-y')).toBe('-y');
const expectation = expectObservable(cold('-z'));
flush();
expectation.toBe('-q');
});
}).to.throw();
});
});
});