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

Commit f8d17ac

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(spec): fix #760, fakeAsyncTestSpec should handle microtask with additional args (#762)
1 parent 4baeb5c commit f8d17ac

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

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

+24-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
isPeriodic: boolean;
1717
}
1818

19+
interface MicroTaskScheduledFunction {
20+
func: Function;
21+
args: any[];
22+
target: any;
23+
}
24+
1925
class Scheduler {
2026
// Next scheduler id.
2127
public nextId: number = 0;
@@ -117,7 +123,7 @@
117123
}
118124

119125
private _scheduler: Scheduler = new Scheduler();
120-
private _microtasks: Function[] = [];
126+
private _microtasks: MicroTaskScheduledFunction[] = [];
121127
private _lastError: Error = null;
122128
private _uncaughtPromiseErrors: {rejection: any}[] =
123129
(Promise as any)[(Zone as any).__symbol__('uncaughtPromiseErrors')];
@@ -238,7 +244,7 @@
238244
};
239245
while (this._microtasks.length > 0) {
240246
let microtask = this._microtasks.shift();
241-
microtask();
247+
microtask.func.apply(microtask.target, microtask.args);
242248
}
243249
flushErrors();
244250
}
@@ -262,7 +268,22 @@
262268
onScheduleTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): Task {
263269
switch (task.type) {
264270
case 'microTask':
265-
this._microtasks.push(task.invoke);
271+
let args = task.data && (task.data as any).args;
272+
// should pass additional arguments to callback if have any
273+
// currently we know process.nextTick will have such additional
274+
// arguments
275+
let addtionalArgs: any[];
276+
if (args) {
277+
let callbackIndex = (task.data as any).callbackIndex;
278+
if (typeof args.length === 'number' && args.length > callbackIndex + 1) {
279+
addtionalArgs = Array.prototype.slice.call(args, callbackIndex + 1);
280+
}
281+
}
282+
this._microtasks.push({
283+
func: task.invoke,
284+
args: addtionalArgs,
285+
target: task.data && (task.data as any).target
286+
});
266287
break;
267288
case 'macroTask':
268289
switch (task.source) {

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

+39
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
*/
88

99
import '../../lib/zone-spec/fake-async-test';
10+
11+
import {isNode} from '../../lib/common/utils';
1012
import {ifEnvSupports} from '../test-util';
1113

14+
function supportNode() {
15+
return isNode;
16+
}
17+
18+
(supportNode as any).message = 'support node';
19+
1220
describe('FakeAsyncTestZoneSpec', () => {
1321
let FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
1422
let testZoneSpec: any;
@@ -527,4 +535,35 @@ describe('FakeAsyncTestZoneSpec', () => {
527535
}).toThrowError('Cannot make XHRs from within a fake async test.');
528536
});
529537
}));
538+
539+
describe('node process', ifEnvSupports(supportNode, () => {
540+
it('should be able to schedule microTask with additional arguments', () => {
541+
const process = global['process'];
542+
const nextTick = process && process['nextTick'];
543+
if (!nextTick) {
544+
return;
545+
}
546+
fakeAsyncTestZone.run(() => {
547+
let tickRun = false;
548+
let cbArgRun = false;
549+
nextTick(
550+
(strArg: string, cbArg: Function) => {
551+
tickRun = true;
552+
expect(strArg).toEqual('stringArg');
553+
cbArg();
554+
},
555+
'stringArg',
556+
() => {
557+
cbArgRun = true;
558+
});
559+
560+
expect(tickRun).toEqual(false);
561+
562+
testZoneSpec.flushMicrotasks();
563+
expect(tickRun).toEqual(true);
564+
expect(cbArgRun).toEqual(true);
565+
});
566+
567+
});
568+
}));
530569
});

0 commit comments

Comments
 (0)