66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { zoneSymbol } from '../../lib/common/utils' ;
9+ import { isBrowser , isMix , zoneSymbol } from '../../lib/common/utils' ;
1010import { ifEnvSupports } from '../test-util' ;
11+
12+ import Spy = jasmine . Spy ;
1113declare const global : any ;
1214
1315function windowPrototype ( ) {
@@ -18,6 +20,19 @@ function promiseUnhandleRejectionSupport() {
1820 return ! ! global [ 'PromiseRejectionEvent' ] ;
1921}
2022
23+ function canPatchOnProperty ( obj : any , prop : string ) {
24+ if ( ! obj ) {
25+ return false ;
26+ }
27+ const desc = Object . getOwnPropertyDescriptor ( obj , prop ) ;
28+ if ( ! desc || ! desc . configurable ) {
29+ return false ;
30+ }
31+ return true ;
32+ }
33+
34+ ( canPatchOnProperty as any ) . message = 'patchOnProperties' ;
35+
2136describe ( 'Zone' , function ( ) {
2237 const rootZone = Zone . current ;
2338
@@ -51,6 +66,80 @@ describe('Zone', function() {
5166 expect ( confirmSpy ) . toHaveBeenCalledWith ( 'confirmMsg' ) ;
5267 } ) ;
5368
69+ describe ( 'DOM onProperty hooks' , ifEnvSupports ( canPatchOnProperty , function ( ) {
70+ let mouseEvent = document . createEvent ( 'Event' ) ;
71+ let hookSpy : Spy , eventListenerSpy : Spy ;
72+ const zone = rootZone . fork ( {
73+ name : 'spy' ,
74+ onScheduleTask : ( parentZoneDelegate : ZoneDelegate , currentZone : Zone ,
75+ targetZone : Zone , task : Task ) : any => {
76+ hookSpy ( ) ;
77+ return parentZoneDelegate . scheduleTask ( targetZone , task ) ;
78+ }
79+ } ) ;
80+
81+ beforeEach ( function ( ) {
82+ mouseEvent . initEvent ( 'mousedown' , true , true ) ;
83+ hookSpy = jasmine . createSpy ( 'hook' ) ;
84+ eventListenerSpy = jasmine . createSpy ( 'eventListener' ) ;
85+ } ) ;
86+
87+ it ( 'window onclick should be in zone' ,
88+ ifEnvSupports (
89+ ( ) => {
90+ return canPatchOnProperty ( window , 'onmousedown' ) ;
91+ } ,
92+ function ( ) {
93+ zone . run ( function ( ) {
94+ window . onmousedown = eventListenerSpy ;
95+ } ) ;
96+
97+ window . dispatchEvent ( mouseEvent ) ;
98+
99+ expect ( hookSpy ) . toHaveBeenCalled ( ) ;
100+ expect ( eventListenerSpy ) . toHaveBeenCalled ( ) ;
101+ window . removeEventListener ( 'mousedown' , eventListenerSpy ) ;
102+ } ) ) ;
103+
104+ it ( 'document onclick should be in zone' ,
105+ ifEnvSupports (
106+ ( ) => {
107+ return canPatchOnProperty ( Document . prototype , 'onmousedown' ) ;
108+ } ,
109+ function ( ) {
110+ zone . run ( function ( ) {
111+ document . onmousedown = eventListenerSpy ;
112+ } ) ;
113+
114+ document . dispatchEvent ( mouseEvent ) ;
115+
116+ expect ( hookSpy ) . toHaveBeenCalled ( ) ;
117+ expect ( eventListenerSpy ) . toHaveBeenCalled ( ) ;
118+ document . removeEventListener ( 'mousedown' , eventListenerSpy ) ;
119+ } ) ) ;
120+
121+ it ( 'SVGElement onclick should be in zone' ,
122+ ifEnvSupports (
123+ ( ) => {
124+ return typeof SVGElement !== 'undefined' &&
125+ canPatchOnProperty ( SVGElement . prototype , 'onmousedown' ) ;
126+ } ,
127+ function ( ) {
128+ const svg = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'svg' ) ;
129+ document . body . appendChild ( svg ) ;
130+ zone . run ( function ( ) {
131+ svg . onmousedown = eventListenerSpy ;
132+ } ) ;
133+
134+ svg . dispatchEvent ( mouseEvent ) ;
135+
136+ expect ( hookSpy ) . toHaveBeenCalled ( ) ;
137+ expect ( eventListenerSpy ) . toHaveBeenCalled ( ) ;
138+ svg . removeEventListener ( 'mouse' , eventListenerSpy ) ;
139+ document . body . removeChild ( svg ) ;
140+ } ) ) ;
141+ } ) ) ;
142+
54143 describe ( 'eventListener hooks' , function ( ) {
55144 let button : HTMLButtonElement ;
56145 let clickEvent : Event ;
0 commit comments