Skip to content

Commit

Permalink
test(scheduler): add interval recycling tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Nov 9, 2017
1 parent d77e3d7 commit 81bfe55
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions spec/schedulers/AsapScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,57 @@ describe('Scheduler.asap', () => {
sandbox.restore();
});

it('should reuse the interval for recursively scheduled actions with the same delay', () => {
const sandbox = sinon.sandbox.create();
const fakeTimer = sandbox.useFakeTimers();
const stubSetInterval = sinon.stub(global, 'setInterval').callThrough();
function dispatch(state: any): void {
state.index += 1;
if (state.index < 3) {
(<any> this).schedule(state, state.period);
}
}
const period = 50;
const state = { index: 0, period };
asap.schedule(dispatch, period, state);
expect(state).to.have.property('index', 0);
expect(stubSetInterval).to.have.property('callCount', 1);
fakeTimer.tick(period);
expect(state).to.have.property('index', 1);
expect(stubSetInterval).to.have.property('callCount', 1);
fakeTimer.tick(period);
expect(state).to.have.property('index', 2);
expect(stubSetInterval).to.have.property('callCount', 1);
stubSetInterval.restore();
sandbox.restore();
});

it('should not reuse the interval for recursively scheduled actions with a different delay', () => {
const sandbox = sinon.sandbox.create();
const fakeTimer = sandbox.useFakeTimers();
const stubSetInterval = sinon.stub(global, 'setInterval').callThrough();
function dispatch(state: any): void {
state.index += 1;
state.period -= 1;
if (state.index < 3) {
(<any> this).schedule(state, state.period);
}
}
const period = 50;
const state = { index: 0, period };
asap.schedule(dispatch, period, state);
expect(state).to.have.property('index', 0);
expect(stubSetInterval).to.have.property('callCount', 1);
fakeTimer.tick(period);
expect(state).to.have.property('index', 1);
expect(stubSetInterval).to.have.property('callCount', 2);
fakeTimer.tick(period);
expect(state).to.have.property('index', 2);
expect(stubSetInterval).to.have.property('callCount', 3);
stubSetInterval.restore();
sandbox.restore();
});

it('should schedule an action to happen later', (done: MochaDone) => {
let actionHappened = false;
asap.schedule(() => {
Expand Down

0 comments on commit 81bfe55

Please sign in to comment.