Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 9c96904

Browse files
mitchellwillsmhevery
authored andcommitted
Ensure FakeAsyncTestZoneSpec tick always doTick (#1099)
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).
1 parent 6ba3169 commit 9c96904

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/zone-spec/fake-async-test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class Scheduler {
150150
}
151151
}
152152
}
153+
lastCurrentTime = this._currentTime;
153154
this._currentTime = finalTime;
155+
if (doTick) {
156+
doTick(this._currentTime - lastCurrentTime);
157+
}
154158
}
155159

156160
flush(limit = 20, flushPeriodic = false, doTick?: (elapsed: number) => void): number {

test/zone-spec/fake-async-test.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,25 @@ describe('FakeAsyncTestZoneSpec', () => {
172172
});
173173
});
174174

175+
it('should run doTick callback even if no work ran', () => {
176+
fakeAsyncTestZone.run(() => {
177+
let totalElapsed = 0;
178+
function doTick(elapsed: number) {
179+
totalElapsed += elapsed;
180+
}
181+
setTimeout(() => {}, 10);
182+
183+
testZoneSpec.tick(6, doTick);
184+
expect(totalElapsed).toEqual(6);
185+
186+
testZoneSpec.tick(6, doTick);
187+
expect(totalElapsed).toEqual(12);
188+
189+
testZoneSpec.tick(6, doTick);
190+
expect(totalElapsed).toEqual(18);
191+
});
192+
});
193+
175194
it('should run queued timer created by timer callback', () => {
176195
fakeAsyncTestZone.run(() => {
177196
let counter = 0;

0 commit comments

Comments
 (0)