From 78ffe47bfb45ca09ed1e76c6a3f3bcf84a2467ca Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Wed, 21 Nov 2018 21:35:22 +0900 Subject: [PATCH] fix(core): fix #1153, ZoneTask.toString should always be a string --- lib/zone.ts | 2 +- test/common/toString.spec.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) 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..b37a3f197 100644 --- a/test/common/toString.spec.ts +++ b/test/common/toString.spec.ts @@ -47,3 +47,39 @@ 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(() => {}); + expect(typeof macroTask1.toString()).toEqual('string'); + expect(macroTask1.toString()).toEqual(id1.toString()); + expect(typeof macroTask2.toString()).toEqual('string'); + expect(macroTask2.toString()).toEqual(id2.toString()); + expect(macroTask1.toString()).not.toEqual(macroTask2.toString()); + expect(typeof microTask.toString()).toEqual('string'); + }); + }); +});