From 9c9690415237bd5866d20a360ec4049740de4742 Mon Sep 17 00:00:00 2001 From: Mitchell Wills Date: Fri, 27 Jul 2018 10:43:08 -0700 Subject: [PATCH] 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). --- lib/zone-spec/fake-async-test.ts | 4 ++++ test/zone-spec/fake-async-test.spec.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/zone-spec/fake-async-test.ts b/lib/zone-spec/fake-async-test.ts index 724ce68fa..307470cb1 100644 --- a/lib/zone-spec/fake-async-test.ts +++ b/lib/zone-spec/fake-async-test.ts @@ -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 { diff --git a/test/zone-spec/fake-async-test.spec.ts b/test/zone-spec/fake-async-test.spec.ts index af2473765..21a2be440 100644 --- a/test/zone-spec/fake-async-test.spec.ts +++ b/test/zone-spec/fake-async-test.spec.ts @@ -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;