diff --git a/lib/zone-spec/fake-async-test.ts b/lib/zone-spec/fake-async-test.ts index 9d2ce990e..3add4a68d 100644 --- a/lib/zone-spec/fake-async-test.ts +++ b/lib/zone-spec/fake-async-test.ts @@ -51,15 +51,16 @@ } tick(millis: number = 0): void { - this._currentTime += millis; - while (this._schedulerQueue.length > 0) { + let finalTime = this._currentTime + millis; + while (this._schedulerQueue.length > 0) { let current = this._schedulerQueue[0]; - if (this._currentTime < current.endTime) { + if (finalTime < current.endTime) { // Done processing the queue since it's sorted by endTime. break; } else { // Time to run scheduled function. Remove it from the head of queue. let current = this._schedulerQueue.shift(); + this._currentTime = current.endTime; let retval = current.func.apply(global, current.args); if (!retval) { // Uncaught exception in the current scheduled function. Stop processing the queue. @@ -67,6 +68,7 @@ } } } + this._currentTime = finalTime; } } diff --git a/test/zone-spec/fake-async-test.spec.ts b/test/zone-spec/fake-async-test.spec.ts index 12639fb4f..f6fa38407 100644 --- a/test/zone-spec/fake-async-test.spec.ts +++ b/test/zone-spec/fake-async-test.spec.ts @@ -119,6 +119,29 @@ describe('FakeAsyncTestZoneSpec', () => { }); }); + it('should run queued timer created by timer callback', () => { + fakeAsyncTestZone.run(() => { + let counter = 0; + const startCounterLoop = () => { + counter++; + setTimeout(startCounterLoop, 10); + } + + startCounterLoop(); + + expect(counter).toEqual(1); + + testZoneSpec.tick(10); + expect(counter).toEqual(2); + + testZoneSpec.tick(10); + expect(counter).toEqual(3); + + testZoneSpec.tick(30); + expect(counter).toEqual(6); + }); + }); + it('should run queued timer only once', () => { fakeAsyncTestZone.run(() => { let cycles = 0; @@ -160,6 +183,9 @@ describe('FakeAsyncTestZoneSpec', () => { testZoneSpec.tick(10); expect(cycles).toEqual(3); + + testZoneSpec.tick(30); + expect(cycles).toEqual(6); }); });