From 7dc5c1bbb3042a038aaaf23bf5cb239a756188b8 Mon Sep 17 00:00:00 2001 From: "JiaLi.Passion" Date: Sat, 26 Aug 2017 05:21:14 +0900 Subject: [PATCH] fix(patch): patchOnProperty getter should return original listener --- lib/common/utils.ts | 5 +++-- test/browser/XMLHttpRequest.spec.ts | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/common/utils.ts b/lib/common/utils.ts index 0015abdcf..8bf55f79f 100644 --- a/lib/common/utils.ts +++ b/lib/common/utils.ts @@ -170,8 +170,9 @@ export function patchProperty(obj: any, prop: string, prototype?: any) { if (!target) { return null; } - if (target[eventNameSymbol]) { - return wrapFn; + const listener = target[eventNameSymbol]; + if (listener) { + return listener; } else if (originalDescGet) { // result will be null when use inline event attribute, // such as diff --git a/test/browser/XMLHttpRequest.spec.ts b/test/browser/XMLHttpRequest.spec.ts index 41f5d62c9..de413a591 100644 --- a/test/browser/XMLHttpRequest.spec.ts +++ b/test/browser/XMLHttpRequest.spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {ifEnvSupports, supportPatchXHROnProperty} from '../test-util'; +import {ifEnvSupports, ifEnvSupportsWithDone, supportPatchXHROnProperty} from '../test-util'; describe('XMLHttpRequest', function() { let testZone: Zone; @@ -280,4 +280,24 @@ describe('XMLHttpRequest', function() { req.send(); }); }); + + it('should return origin listener when call xhr.onreadystatechange', + ifEnvSupportsWithDone(supportPatchXHROnProperty, function(done: Function) { + testZone.run(function() { + // sometimes this case will cause timeout + // so we set it longer + const req = new XMLHttpRequest(); + req.open('get', '/', true); + const listener = req.onreadystatechange = function() { + if (req.readyState === 4) { + done(); + } + }; + expect(req.onreadystatechange).toBe(listener); + req.onreadystatechange = function() { + return listener.apply(this, arguments); + }; + req.send(); + }); + })); });