6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { zoneSymbol } from '../../lib/common/utils' ;
9
+ import { isBrowser , isMix , zoneSymbol } from '../../lib/common/utils' ;
10
10
import { ifEnvSupports } from '../test-util' ;
11
+
12
+ import Spy = jasmine . Spy ;
11
13
declare const global : any ;
12
14
13
15
function windowPrototype ( ) {
@@ -18,6 +20,19 @@ function promiseUnhandleRejectionSupport() {
18
20
return ! ! global [ 'PromiseRejectionEvent' ] ;
19
21
}
20
22
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
+
21
36
describe ( 'Zone' , function ( ) {
22
37
const rootZone = Zone . current ;
23
38
@@ -51,6 +66,80 @@ describe('Zone', function() {
51
66
expect ( confirmSpy ) . toHaveBeenCalledWith ( 'confirmMsg' ) ;
52
67
} ) ;
53
68
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
+
54
143
describe ( 'eventListener hooks' , function ( ) {
55
144
let button : HTMLButtonElement ;
56
145
let clickEvent : Event ;
0 commit comments