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

fix(core): fix #1153, ZoneTask.toString should always be a string #1166

Merged
merged 1 commit into from
Dec 12, 2018
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
2 changes: 1 addition & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ const Zone: ZoneType = (function(global: any) {

public toString() {
if (this.data && typeof this.data.handleId !== 'undefined') {
return this.data.handleId;
return this.data.handleId.toString();
} else {
return Object.prototype.toString.call(this);
}
Expand Down
40 changes: 40 additions & 0 deletions test/common/toString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,43 @@ describe('global function patch', () => {
}));
});
});

describe('ZoneTask', () => {
it('should return handleId.toString if handleId is available', () => {
let macroTask1: any = undefined;
let macroTask2: any = undefined;
let microTask: any = undefined;
const zone = Zone.current.fork({
name: 'timer',
onScheduleTask: (delegate: ZoneDelegate, curr: Zone, target: Zone, task: Task) => {
if (task.type === 'macroTask') {
if (!macroTask1) {
macroTask1 = task;
} else {
macroTask2 = task;
}
} else if (task.type === 'microTask') {
microTask = task;
}
return task;
}
});
zone.run(() => {
const id1 = setTimeout(() => {});
clearTimeout(id1);
const id2 = setTimeout(() => {});
clearTimeout(id2);
Promise.resolve().then(() => {});
const macroTask1Str = macroTask1.toString();
const macroTask2Str = macroTask2.toString();
expect(typeof macroTask1Str).toEqual('string');
expect(macroTask1Str).toEqual(id1.toString());
expect(typeof macroTask2Str).toEqual('string');
expect(macroTask2Str).toEqual(id2.toString());
if (macroTask1.data && typeof macroTask1.data.handleId === 'number') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the reason is this case will also run in nodejs environment, in nodejs the handleId will be a TimerObject, in that case, we will not check their equality.

expect(macroTask1Str).not.toEqual(macroTask2Str);
}
expect(typeof microTask.toString()).toEqual('string');
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be valueable to also verify that two different macrotasks have different string representations (just in case).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, case added.

});
});