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

Commit a85db4c

Browse files
kasiditimhevery
authored andcommitted
fix: run all timers in passage of time in a single fakeAsync's tick call
Closes #454
1 parent b52cf02 commit a85db4c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,24 @@
5151
}
5252

5353
tick(millis: number = 0): void {
54-
this._currentTime += millis;
55-
while (this._schedulerQueue.length > 0) {
54+
let finalTime = this._currentTime + millis;
55+
while (this._schedulerQueue.length > 0) {
5656
let current = this._schedulerQueue[0];
57-
if (this._currentTime < current.endTime) {
57+
if (finalTime < current.endTime) {
5858
// Done processing the queue since it's sorted by endTime.
5959
break;
6060
} else {
6161
// Time to run scheduled function. Remove it from the head of queue.
6262
let current = this._schedulerQueue.shift();
63+
this._currentTime = current.endTime;
6364
let retval = current.func.apply(global, current.args);
6465
if (!retval) {
6566
// Uncaught exception in the current scheduled function. Stop processing the queue.
6667
break;
6768
}
6869
}
6970
}
71+
this._currentTime = finalTime;
7072
}
7173
}
7274

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

+26
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ describe('FakeAsyncTestZoneSpec', () => {
119119
});
120120
});
121121

122+
it('should run queued timer created by timer callback', () => {
123+
fakeAsyncTestZone.run(() => {
124+
let counter = 0;
125+
const startCounterLoop = () => {
126+
counter++;
127+
setTimeout(startCounterLoop, 10);
128+
}
129+
130+
startCounterLoop();
131+
132+
expect(counter).toEqual(1);
133+
134+
testZoneSpec.tick(10);
135+
expect(counter).toEqual(2);
136+
137+
testZoneSpec.tick(10);
138+
expect(counter).toEqual(3);
139+
140+
testZoneSpec.tick(30);
141+
expect(counter).toEqual(6);
142+
});
143+
});
144+
122145
it('should run queued timer only once', () => {
123146
fakeAsyncTestZone.run(() => {
124147
let cycles = 0;
@@ -160,6 +183,9 @@ describe('FakeAsyncTestZoneSpec', () => {
160183

161184
testZoneSpec.tick(10);
162185
expect(cycles).toEqual(3);
186+
187+
testZoneSpec.tick(30);
188+
expect(cycles).toEqual(6);
163189
});
164190
});
165191

0 commit comments

Comments
 (0)