@@ -5,11 +5,14 @@ describe('Zone.patch', function () {
5
5
6
6
beforeEach ( function ( ) {
7
7
zone . mark = 'root' ;
8
- jasmine . Clock . useMock ( ) ;
9
8
} ) ;
10
9
11
10
describe ( 'setTimeout' , function ( ) {
12
11
12
+ beforeEach ( function ( ) {
13
+ jasmine . Clock . useMock ( ) ;
14
+ } ) ;
15
+
13
16
it ( 'should work with setTimeout' , function ( ) {
14
17
15
18
var childZone = window . zone . fork ( {
@@ -49,6 +52,10 @@ describe('Zone.patch', function () {
49
52
50
53
describe ( 'setInterval' , function ( ) {
51
54
55
+ beforeEach ( function ( ) {
56
+ jasmine . Clock . useMock ( ) ;
57
+ } ) ;
58
+
52
59
it ( 'should work with setInterval' , function ( ) {
53
60
54
61
var childZone = window . zone . fork ( {
@@ -81,10 +88,35 @@ describe('Zone.patch', function () {
81
88
} ) ;
82
89
83
90
describe ( 'requestAnimationFrame' , function ( ) {
84
- var flag , hasParent ;
91
+ var flag , hasParent , skip = false ;
85
92
86
93
it ( 'should work' , function ( done ) {
87
94
95
+ if ( ! window . requestAnimationFrame ) {
96
+ console . log ( 'WARNING: skipping requestAnimationFrame test (missing this API)' ) ;
97
+ return ;
98
+ }
99
+
100
+ // Some browsers (especially Safari) do not fire requestAnimationFrame
101
+ // if they are offscreen. We can disable this test for those browsers and
102
+ // assume the patch works if setTimeout works, since they are mechanically
103
+ // the same
104
+ runs ( function ( ) {
105
+ flag = false ;
106
+ window . requestAnimationFrame ( function ( ) {
107
+ flag = true ;
108
+ } ) ;
109
+ setTimeout ( function ( ) {
110
+ skip = true ;
111
+ flag = true ;
112
+ console . log ( 'WARNING: skipping requestAnimationFrame test (not firing rAF)' ) ;
113
+ } , 50 ) ;
114
+ } ) ;
115
+
116
+ waitsFor ( function ( ) {
117
+ return flag ;
118
+ } , "requestAnimationFrame to run" , 100 ) ;
119
+
88
120
runs ( function ( ) {
89
121
flag = false ;
90
122
hasParent = false ;
@@ -95,12 +127,63 @@ describe('Zone.patch', function () {
95
127
} ) ;
96
128
} ) ;
97
129
130
+ waitsFor ( function ( ) {
131
+ return flag || skip ;
132
+ } , "requestAnimationFrame to run" , 100 ) ;
133
+
134
+ runs ( function ( ) {
135
+ expect ( hasParent || skip ) . toBe ( true ) ;
136
+ } ) ;
137
+
138
+ } ) ;
139
+ } ) ;
140
+
141
+ describe ( 'webkitRequestAnimationFrame' , function ( ) {
142
+ var flag , hasParent , skip = false ;
143
+
144
+ it ( 'should work' , function ( done ) {
145
+
146
+ if ( ! window . webkitRequestAnimationFrame ) {
147
+ console . log ( 'WARNING: skipping webkitRequestAnimationFrame test (missing this API)' ) ;
148
+ return ;
149
+ }
150
+
151
+ // Some browsers (especially Safari) do not fire webkitRequestAnimationFrame
152
+ // if they are offscreen. We can disable this test for those browsers and
153
+ // assume the patch works if setTimeout works, since they are mechanically
154
+ // the same
155
+ runs ( function ( ) {
156
+ flag = false ;
157
+ window . webkitRequestAnimationFrame ( function ( ) {
158
+ flag = true ;
159
+ } ) ;
160
+ setTimeout ( function ( ) {
161
+ skip = true ;
162
+ flag = true ;
163
+ console . log ( 'WARNING: skipping webkitRequestAnimationFrame test (not firing rAF)' ) ;
164
+ } , 50 ) ;
165
+ } ) ;
166
+
98
167
waitsFor ( function ( ) {
99
168
return flag ;
100
- } , "requestAnimationFrame to run" , 1 ) ;
169
+ } , 'webkitRequestAnimationFrame to run' , 100 ) ;
101
170
102
171
runs ( function ( ) {
103
- expect ( hasParent ) . toBe ( true ) ;
172
+ flag = false ;
173
+ hasParent = false ;
174
+
175
+ window . webkitRequestAnimationFrame ( function ( ) {
176
+ hasParent = ! ! window . zone . parent ;
177
+ flag = true ;
178
+ } ) ;
179
+ } ) ;
180
+
181
+ waitsFor ( function ( ) {
182
+ return flag || skip ;
183
+ } , 'webkitRequestAnimationFrame to run' , 100 ) ;
184
+
185
+ runs ( function ( ) {
186
+ expect ( hasParent || skip ) . toBe ( true ) ;
104
187
} ) ;
105
188
106
189
} ) ;
@@ -110,27 +193,31 @@ describe('Zone.patch', function () {
110
193
var flag , hasParent ;
111
194
112
195
beforeEach ( function ( ) {
196
+ jasmine . Clock . useMock ( ) ;
113
197
flag = false ;
114
198
hasParent = false ;
115
199
} ) ;
116
200
117
201
it ( 'should work with .then' , function ( ) {
118
202
if ( ! window . Promise ) {
203
+ console . log ( 'WARNING: skipping Promise test (missing this API)' ) ;
119
204
return ;
120
205
}
121
206
122
- runs ( function ( ) {
207
+ runs ( function ( ) {
123
208
new Promise ( function ( resolve ) {
124
- requestAnimationFrame ( resolve ) ;
209
+ setTimeout ( resolve , 0 ) ;
125
210
} ) . then ( function ( ) {
126
211
hasParent = ! ! window . zone . parent ;
127
212
flag = true ;
128
213
} ) ;
214
+ jasmine . Clock . tick ( 1 ) ;
129
215
} ) ;
130
216
217
+
131
218
waitsFor ( function ( ) {
132
219
return flag ;
133
- } , "requestAnimationFrame to run" , 1 ) ;
220
+ } , 'promise to resolve' , 100 ) ;
134
221
135
222
runs ( function ( ) {
136
223
expect ( hasParent ) . toBe ( true ) ;
@@ -144,16 +231,17 @@ describe('Zone.patch', function () {
144
231
145
232
runs ( function ( ) {
146
233
new Promise ( function ( resolve , reject ) {
147
- requestAnimationFrame ( reject ) ;
234
+ setTimeout ( reject , 0 ) ;
148
235
} ) . catch ( function ( ) {
149
236
hasParent = ! ! window . zone . parent ;
150
237
flag = true ;
151
238
} ) ;
239
+ jasmine . Clock . tick ( 1 ) ;
152
240
} ) ;
153
241
154
242
waitsFor ( function ( ) {
155
243
return flag ;
156
- } , "requestAnimationFrame to run " , 1 ) ;
244
+ } , "promise to reject " , 100 ) ;
157
245
158
246
runs ( function ( ) {
159
247
expect ( hasParent ) . toBe ( true ) ;
@@ -237,6 +325,11 @@ describe('Zone.patch', function () {
237
325
} ) ;
238
326
239
327
describe ( 'hooks' , function ( ) {
328
+
329
+ beforeEach ( function ( ) {
330
+ jasmine . Clock . useMock ( ) ;
331
+ } ) ;
332
+
240
333
it ( 'should fire onZoneEnter before a zone runs a function' , function ( ) {
241
334
var enterSpy = jasmine . createSpy ( ) ;
242
335
var myZone = zone . fork ( {
0 commit comments