/
QueueScheduler-spec.ts
84 lines (71 loc) · 2.37 KB
/
QueueScheduler-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
import { expect } from 'chai';
import * as sinon from 'sinon';
import { queueScheduler, Subscription, merge } from 'rxjs';
import { delay } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
const queue = queueScheduler;
/** @test {Scheduler} */
describe('Scheduler.queue', () => {
let testScheduler: TestScheduler;
beforeEach(() => {
testScheduler = new TestScheduler(observableMatcher);
});
it('should act like the async scheduler if delay > 0', () => {
testScheduler.run(({ cold, expectObservable, time }) => {
const a = cold(' a ');
const ta = time(' ----| ');
const b = cold(' b ');
const tb = time(' --------| ');
const expected = '----a---b----';
const result = merge(
a.pipe(delay(ta, queue)),
b.pipe(delay(tb, queue))
);
expectObservable(result).toBe(expected);
});
});
it('should switch from synchronous to asynchronous at will', () => {
const sandbox = sinon.createSandbox();
const fakeTimer = sandbox.useFakeTimers();
let asyncExec = false;
let state: Array<number> = [];
queue.schedule(function (index) {
state.push(index!);
if (index === 0) {
this.schedule(1, 100);
} else if (index === 1) {
asyncExec = true;
this.schedule(2, 0);
}
}, 0, 0);
expect(asyncExec).to.be.false;
expect(state).to.be.deep.equal([0]);
fakeTimer.tick(100);
expect(asyncExec).to.be.true;
expect(state).to.be.deep.equal([0, 1, 2]);
sandbox.restore();
});
it('should unsubscribe the rest of the scheduled actions if an action throws an error', () => {
const actions: Subscription[] = [];
let action2Exec = false;
let action3Exec = false;
let errorValue: any = undefined;
try {
queue.schedule(() => {
actions.push(
queue.schedule(() => { throw new Error('oops'); }),
queue.schedule(() => { action2Exec = true; }),
queue.schedule(() => { action3Exec = true; })
);
});
} catch (e) {
errorValue = e;
}
expect(actions.every((action) => action.closed)).to.be.true;
expect(action2Exec).to.be.false;
expect(action3Exec).to.be.false;
expect(errorValue).exist;
expect(errorValue.message).to.equal('oops');
});
});