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

Commit 02cd40e

Browse files
vikermanmhevery
authored andcommitted
feat(spec): add a 'tick' callback to flush() (#866)
This can be used to advance a secondary test scheduler like the Jasmine mock Date every time flush advances to the next task to be processed.
1 parent 7fbd655 commit 02cd40e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Diff for: lib/zone-spec/fake-async-test.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@
9191
this._currentTime = finalTime;
9292
}
9393

94-
flush(limit: number = 20, flushPeriodic = false): number {
94+
flush(limit = 20, flushPeriodic = false, tick?: (elapsed: number) => void): number {
9595
const startTime = this._currentTime;
96+
let lastCurrentTime = this._currentTime;
9697
let count = 0;
97-
let seenTimers: number[] = [];
98+
const seenTimers: number[] = [];
9899
while (this._schedulerQueue.length > 0) {
99100
count++;
100101
if (count > limit) {
@@ -120,12 +121,17 @@
120121
break;
121122
}
122123
}
123-
let current = this._schedulerQueue.shift();
124+
const current = this._schedulerQueue.shift();
124125
if (seenTimers.indexOf(current.id) === -1) {
125126
seenTimers.push(current.id);
126127
}
128+
lastCurrentTime = this._currentTime;
127129
this._currentTime = current.endTime;
128-
let retval = current.func.apply(global, current.args);
130+
if (tick) {
131+
// Tick any secondary schedulers like Jasmine mock Date.
132+
tick(this._currentTime - lastCurrentTime);
133+
}
134+
const retval = current.func.apply(global, current.args);
129135
if (!retval) {
130136
// Uncaught exception in the current scheduled function. Stop processing the queue.
131137
break;
@@ -271,10 +277,10 @@
271277
flushErrors();
272278
}
273279

274-
flush(limit?: number, flushPeriodic?: boolean): number {
280+
flush(limit?: number, flushPeriodic?: boolean, tick?: (elapsed: number) => void): number {
275281
FakeAsyncTestZoneSpec.assertInZone();
276282
this.flushMicrotasks();
277-
let elapsed = this._scheduler.flush(limit, flushPeriodic);
283+
const elapsed = this._scheduler.flush(limit, flushPeriodic, tick);
278284
if (this._lastError !== null) {
279285
this._resetLastErrorAndThrow();
280286
}

0 commit comments

Comments
 (0)