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

fix(spec): fix #760, fakeAsyncTestSpec should handle microtask with additional args #762

Merged
merged 1 commit into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
isPeriodic: boolean;
}

interface MicroTaskScheduledFunction {
func: Function;
args: any[];
target: any;
}

class Scheduler {
// Next scheduler id.
public nextId: number = 0;
Expand Down Expand Up @@ -117,7 +123,7 @@
}

private _scheduler: Scheduler = new Scheduler();
private _microtasks: Function[] = [];
private _microtasks: MicroTaskScheduledFunction[] = [];
private _lastError: Error = null;
private _uncaughtPromiseErrors: {rejection: any}[] =
(Promise as any)[(Zone as any).__symbol__('uncaughtPromiseErrors')];
Expand Down Expand Up @@ -238,7 +244,7 @@
};
while (this._microtasks.length > 0) {
let microtask = this._microtasks.shift();
microtask();
microtask.func.apply(microtask.target, microtask.args);
}
flushErrors();
}
Expand All @@ -262,7 +268,22 @@
onScheduleTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): Task {
switch (task.type) {
case 'microTask':
this._microtasks.push(task.invoke);
let args = task.data && (task.data as any).args;
// should pass additional arguments to callback if have any
// currently we know process.nextTick will have such additional
// arguments
let addtionalArgs: any[];
if (args) {
let callbackIndex = (task.data as any).callbackIndex;
if (typeof args.length === 'number' && args.length > callbackIndex + 1) {
addtionalArgs = Array.prototype.slice.call(args, callbackIndex + 1);
}
}
this._microtasks.push({
func: task.invoke,
args: addtionalArgs,
target: task.data && (task.data as any).target
});
break;
case 'macroTask':
switch (task.source) {
Expand Down
39 changes: 39 additions & 0 deletions test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
*/

import '../../lib/zone-spec/fake-async-test';

import {isNode} from '../../lib/common/utils';
import {ifEnvSupports} from '../test-util';

function supportNode() {
return isNode;
}

(supportNode as any).message = 'support node';

describe('FakeAsyncTestZoneSpec', () => {
let FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
let testZoneSpec: any;
Expand Down Expand Up @@ -527,4 +535,35 @@ describe('FakeAsyncTestZoneSpec', () => {
}).toThrowError('Cannot make XHRs from within a fake async test.');
});
}));

describe('node process', ifEnvSupports(supportNode, () => {
it('should be able to schedule microTask with additional arguments', () => {
const process = global['process'];
const nextTick = process && process['nextTick'];
if (!nextTick) {
return;
}
fakeAsyncTestZone.run(() => {
let tickRun = false;
let cbArgRun = false;
nextTick(
(strArg: string, cbArg: Function) => {
tickRun = true;
expect(strArg).toEqual('stringArg');
cbArg();
},
'stringArg',
() => {
cbArgRun = true;
});

expect(tickRun).toEqual(false);

testZoneSpec.flushMicrotasks();
expect(tickRun).toEqual(true);
expect(cbArgRun).toEqual(true);
});

});
}));
});