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

Commit 68682cd

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(fakeAsync): fix #1056, fakeAsync timerId should not be zero (#1057)
1 parent a4b42cd commit 68682cd

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
class Scheduler {
5858
// Next scheduler id.
59-
public nextId: number = 0;
59+
public nextId: number = 1;
6060

6161
// Scheduler queue with the tuple of end time and callback function - sorted by end time.
6262
private _schedulerQueue: ScheduledFunction[] = [];
@@ -409,12 +409,14 @@
409409
case 'macroTask':
410410
switch (task.source) {
411411
case 'setTimeout':
412-
task.data['handleId'] =
413-
this._setTimeout(task.invoke, task.data['delay'], Array.prototype.slice.call((task.data as any)['args'], 2));
412+
task.data['handleId'] = this._setTimeout(
413+
task.invoke, task.data['delay'],
414+
Array.prototype.slice.call((task.data as any)['args'], 2));
414415
break;
415416
case 'setInterval':
416-
task.data['handleId'] =
417-
this._setInterval(task.invoke, task.data['delay'], Array.prototype.slice.call((task.data as any)['args'], 2));
417+
task.data['handleId'] = this._setInterval(
418+
task.invoke, task.data['delay'],
419+
Array.prototype.slice.call((task.data as any)['args'], 2));
418420
break;
419421
case 'XMLHttpRequest.send':
420422
throw new Error(
@@ -480,7 +482,9 @@
480482
}
481483
}
482484

483-
onInvoke(delegate: ZoneDelegate, current: Zone, target: Zone, callback: Function, applyThis: any, applyArgs: any[], source: string): any {
485+
onInvoke(
486+
delegate: ZoneDelegate, current: Zone, target: Zone, callback: Function, applyThis: any,
487+
applyArgs: any[], source: string): any {
484488
try {
485489
FakeAsyncTestZoneSpec.patchDate();
486490
return delegate.invoke(target, callback, applyThis, applyArgs, source);

test/test-env-setup-mocha.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ declare const global: any;
7676
throw new Error(`Expected ${expected} to be ${actual}`);
7777
}
7878
},
79+
toBeGreaterThan: function(actual: number) {
80+
if (expected <= actual) {
81+
throw new Error(`Expected ${expected} to be greater than ${actual}`);
82+
}
83+
},
7984
toBeDefined: function() {
8085
if (!expected) {
8186
throw new Error(`Expected ${expected} to be defined`);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ describe('FakeAsyncTestZoneSpec', () => {
226226
cycles++;
227227
}, 10);
228228

229+
expect(id).toBeGreaterThan(0);
230+
229231
testZoneSpec.tick(10);
230232
expect(cycles).toEqual(1);
231233

0 commit comments

Comments
 (0)