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

Commit 1daa047

Browse files
committed
test: improve promise and requestAnimationFrame tests
- use setTimeout in promise tests - avoid using jasmine mock timers in promise tests - skip promise tests for browsers that are not running requestAnimationFrame - add test for webkitRequestAnimationFrame
1 parent 92d855b commit 1daa047

File tree

1 file changed

+102
-9
lines changed

1 file changed

+102
-9
lines changed

Diff for: test/zone.spec.js

+102-9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ describe('Zone.patch', function () {
55

66
beforeEach(function () {
77
zone.mark = 'root';
8-
jasmine.Clock.useMock();
98
});
109

1110
describe('setTimeout', function () {
1211

12+
beforeEach(function () {
13+
jasmine.Clock.useMock();
14+
});
15+
1316
it('should work with setTimeout', function () {
1417

1518
var childZone = window.zone.fork({
@@ -49,6 +52,10 @@ describe('Zone.patch', function () {
4952

5053
describe('setInterval', function () {
5154

55+
beforeEach(function () {
56+
jasmine.Clock.useMock();
57+
});
58+
5259
it('should work with setInterval', function () {
5360

5461
var childZone = window.zone.fork({
@@ -81,10 +88,35 @@ describe('Zone.patch', function () {
8188
});
8289

8390
describe('requestAnimationFrame', function () {
84-
var flag, hasParent;
91+
var flag, hasParent, skip = false;
8592

8693
it('should work', function (done) {
8794

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+
88120
runs(function() {
89121
flag = false;
90122
hasParent = false;
@@ -95,12 +127,63 @@ describe('Zone.patch', function () {
95127
});
96128
});
97129

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+
98167
waitsFor(function() {
99168
return flag;
100-
}, "requestAnimationFrame to run", 1);
169+
}, 'webkitRequestAnimationFrame to run', 100);
101170

102171
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);
104187
});
105188

106189
});
@@ -110,27 +193,31 @@ describe('Zone.patch', function () {
110193
var flag, hasParent;
111194

112195
beforeEach(function () {
196+
jasmine.Clock.useMock();
113197
flag = false;
114198
hasParent = false;
115199
});
116200

117201
it('should work with .then', function () {
118202
if (!window.Promise) {
203+
console.log('WARNING: skipping Promise test (missing this API)');
119204
return;
120205
}
121206

122-
runs(function() {
207+
runs(function () {
123208
new Promise(function (resolve) {
124-
requestAnimationFrame(resolve);
209+
setTimeout(resolve, 0);
125210
}).then(function () {
126211
hasParent = !!window.zone.parent;
127212
flag = true;
128213
});
214+
jasmine.Clock.tick(1);
129215
});
130216

217+
131218
waitsFor(function() {
132219
return flag;
133-
}, "requestAnimationFrame to run", 1);
220+
}, 'promise to resolve', 100);
134221

135222
runs(function() {
136223
expect(hasParent).toBe(true);
@@ -144,16 +231,17 @@ describe('Zone.patch', function () {
144231

145232
runs(function() {
146233
new Promise(function (resolve, reject) {
147-
requestAnimationFrame(reject);
234+
setTimeout(reject, 0);
148235
}).catch(function () {
149236
hasParent = !!window.zone.parent;
150237
flag = true;
151238
});
239+
jasmine.Clock.tick(1);
152240
});
153241

154242
waitsFor(function() {
155243
return flag;
156-
}, "requestAnimationFrame to run", 1);
244+
}, "promise to reject", 100);
157245

158246
runs(function() {
159247
expect(hasParent).toBe(true);
@@ -237,6 +325,11 @@ describe('Zone.patch', function () {
237325
});
238326

239327
describe('hooks', function () {
328+
329+
beforeEach(function () {
330+
jasmine.Clock.useMock();
331+
});
332+
240333
it('should fire onZoneEnter before a zone runs a function', function () {
241334
var enterSpy = jasmine.createSpy();
242335
var myZone = zone.fork({

0 commit comments

Comments
 (0)