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

Commit

Permalink
feat(dom): fix #664, patch window,document,SVGElement onProperties (#687
Browse files Browse the repository at this point in the history
)
  • Loading branch information
JiaLiPassion authored and mhevery committed Mar 21, 2017
1 parent 5635ac0 commit 61aee2e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/browser/property-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export function propertyDescriptorPatch(_global: any) {
if (canPatchViaPropertyDescriptor()) {
// for browsers that we can patch the descriptor: Chrome & Firefox
if (isBrowser) {
patchOnProperties(window, eventNames);
patchOnProperties(Document.prototype, eventNames);
if (typeof SVGElement !== 'undefined') {
patchOnProperties(SVGElement.prototype, eventNames);
}
patchOnProperties(HTMLElement.prototype, eventNames);
}
patchOnProperties(XMLHttpRequest.prototype, null);
Expand Down
91 changes: 90 additions & 1 deletion test/browser/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {zoneSymbol} from '../../lib/common/utils';
import {isBrowser, isMix, zoneSymbol} from '../../lib/common/utils';
import {ifEnvSupports} from '../test-util';

import Spy = jasmine.Spy;
declare const global: any;

function windowPrototype() {
Expand All @@ -18,6 +20,19 @@ function promiseUnhandleRejectionSupport() {
return !!global['PromiseRejectionEvent'];
}

function canPatchOnProperty(obj: any, prop: string) {
if (!obj) {
return false;
}
const desc = Object.getOwnPropertyDescriptor(obj, prop);
if (!desc || !desc.configurable) {
return false;
}
return true;
}

(canPatchOnProperty as any).message = 'patchOnProperties';

describe('Zone', function() {
const rootZone = Zone.current;

Expand Down Expand Up @@ -51,6 +66,80 @@ describe('Zone', function() {
expect(confirmSpy).toHaveBeenCalledWith('confirmMsg');
});

describe('DOM onProperty hooks', ifEnvSupports(canPatchOnProperty, function() {
let mouseEvent = document.createEvent('Event');
let hookSpy: Spy, eventListenerSpy: Spy;
const zone = rootZone.fork({
name: 'spy',
onScheduleTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone,
targetZone: Zone, task: Task): any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});

beforeEach(function() {
mouseEvent.initEvent('mousedown', true, true);
hookSpy = jasmine.createSpy('hook');
eventListenerSpy = jasmine.createSpy('eventListener');
});

it('window onclick should be in zone',
ifEnvSupports(
() => {
return canPatchOnProperty(window, 'onmousedown');
},
function() {
zone.run(function() {
window.onmousedown = eventListenerSpy;
});

window.dispatchEvent(mouseEvent);

expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
window.removeEventListener('mousedown', eventListenerSpy);
}));

it('document onclick should be in zone',
ifEnvSupports(
() => {
return canPatchOnProperty(Document.prototype, 'onmousedown');
},
function() {
zone.run(function() {
document.onmousedown = eventListenerSpy;
});

document.dispatchEvent(mouseEvent);

expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
document.removeEventListener('mousedown', eventListenerSpy);
}));

it('SVGElement onclick should be in zone',
ifEnvSupports(
() => {
return typeof SVGElement !== 'undefined' &&
canPatchOnProperty(SVGElement.prototype, 'onmousedown');
},
function() {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
document.body.appendChild(svg);
zone.run(function() {
svg.onmousedown = eventListenerSpy;
});

svg.dispatchEvent(mouseEvent);

expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
svg.removeEventListener('mouse', eventListenerSpy);
document.body.removeChild(svg);
}));
}));

describe('eventListener hooks', function() {
let button: HTMLButtonElement;
let clickEvent: Event;
Expand Down

0 comments on commit 61aee2e

Please sign in to comment.