Skip to content

Commit

Permalink
Ensure FakeAsyncTestZoneSpec tick always doTick (angular#1099)
Browse files Browse the repository at this point in the history
In the case where there is pending work in the scheduler queue, but the duration
of the tick did not causes it to run the doTick callback would not be called (or
would not be called with intervals summing to the total time ellapsed).
  • Loading branch information
mitchellwills authored and mhevery committed Jul 27, 2018
1 parent 6ba3169 commit 9c96904
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ class Scheduler {
}
}
}
lastCurrentTime = this._currentTime;
this._currentTime = finalTime;
if (doTick) {
doTick(this._currentTime - lastCurrentTime);
}
}

flush(limit = 20, flushPeriodic = false, doTick?: (elapsed: number) => void): number {
Expand Down
19 changes: 19 additions & 0 deletions test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ describe('FakeAsyncTestZoneSpec', () => {
});
});

it('should run doTick callback even if no work ran', () => {
fakeAsyncTestZone.run(() => {
let totalElapsed = 0;
function doTick(elapsed: number) {
totalElapsed += elapsed;
}
setTimeout(() => {}, 10);

testZoneSpec.tick(6, doTick);
expect(totalElapsed).toEqual(6);

testZoneSpec.tick(6, doTick);
expect(totalElapsed).toEqual(12);

testZoneSpec.tick(6, doTick);
expect(totalElapsed).toEqual(18);
});
});

it('should run queued timer created by timer callback', () => {
fakeAsyncTestZone.run(() => {
let counter = 0;
Expand Down

0 comments on commit 9c96904

Please sign in to comment.