diff --git a/lib/zone.ts b/lib/zone.ts index 4de4e077d..74fb76536 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -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); } diff --git a/test/common/toString.spec.ts b/test/common/toString.spec.ts index 9d9b57090..a8a29f788 100644 --- a/test/common/toString.spec.ts +++ b/test/common/toString.spec.ts @@ -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') { + expect(macroTask1Str).not.toEqual(macroTask2Str); + } + expect(typeof microTask.toString()).toEqual('string'); + }); + }); +});