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

Commit

Permalink
fix(fakeAsync): fix #1056, fakeAsync timerId should not be zero (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Mar 30, 2018
1 parent a4b42cd commit 68682cd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

class Scheduler {
// Next scheduler id.
public nextId: number = 0;
public nextId: number = 1;

// Scheduler queue with the tuple of end time and callback function - sorted by end time.
private _schedulerQueue: ScheduledFunction[] = [];
Expand Down Expand Up @@ -409,12 +409,14 @@
case 'macroTask':
switch (task.source) {
case 'setTimeout':
task.data['handleId'] =
this._setTimeout(task.invoke, task.data['delay'], Array.prototype.slice.call((task.data as any)['args'], 2));
task.data['handleId'] = this._setTimeout(
task.invoke, task.data['delay'],
Array.prototype.slice.call((task.data as any)['args'], 2));
break;
case 'setInterval':
task.data['handleId'] =
this._setInterval(task.invoke, task.data['delay'], Array.prototype.slice.call((task.data as any)['args'], 2));
task.data['handleId'] = this._setInterval(
task.invoke, task.data['delay'],
Array.prototype.slice.call((task.data as any)['args'], 2));
break;
case 'XMLHttpRequest.send':
throw new Error(
Expand Down Expand Up @@ -480,7 +482,9 @@
}
}

onInvoke(delegate: ZoneDelegate, current: Zone, target: Zone, callback: Function, applyThis: any, applyArgs: any[], source: string): any {
onInvoke(
delegate: ZoneDelegate, current: Zone, target: Zone, callback: Function, applyThis: any,
applyArgs: any[], source: string): any {
try {
FakeAsyncTestZoneSpec.patchDate();
return delegate.invoke(target, callback, applyThis, applyArgs, source);
Expand Down
5 changes: 5 additions & 0 deletions test/test-env-setup-mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ declare const global: any;
throw new Error(`Expected ${expected} to be ${actual}`);
}
},
toBeGreaterThan: function(actual: number) {
if (expected <= actual) {
throw new Error(`Expected ${expected} to be greater than ${actual}`);
}
},
toBeDefined: function() {
if (!expected) {
throw new Error(`Expected ${expected} to be defined`);
Expand Down
2 changes: 2 additions & 0 deletions test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ describe('FakeAsyncTestZoneSpec', () => {
cycles++;
}, 10);

expect(id).toBeGreaterThan(0);

testZoneSpec.tick(10);
expect(cycles).toEqual(1);

Expand Down

0 comments on commit 68682cd

Please sign in to comment.