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

Commit 8260f1d

Browse files
vikermanmhevery
authored andcommitted
fix(spec): fakeAsyncTestSpec should handle requestAnimationFrame (#805)
Fixes #804.
1 parent 3511159 commit 8260f1d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Diff for: lib/zone-spec/fake-async-test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,15 @@
297297
break;
298298
case 'XMLHttpRequest.send':
299299
throw new Error('Cannot make XHRs from within a fake async test.');
300+
case 'requestAnimationFrame':
301+
case 'webkitRequestAnimationFrame':
302+
case 'mozRequestAnimationFrame':
303+
// Simulate a requestAnimationFrame by using a setTimeout with 16 ms.
304+
// (60 frames per second)
305+
task.data['handleId'] = this._setTimeout(task.invoke, 16, (task.data as any)['args']);
306+
break;
300307
default:
301-
task = delegate.scheduleTask(target, task);
308+
throw new Error('Unknown macroTask scheduled in fake async test: ' + task.source);
302309
}
303310
break;
304311
case 'eventTask':
@@ -311,6 +318,9 @@
311318
onCancelTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): any {
312319
switch (task.source) {
313320
case 'setTimeout':
321+
case 'requestAnimationFrame':
322+
case 'webkitRequestAnimationFrame':
323+
case 'mozRequestAnimationFrame':
314324
return this._clearTimeout(task.data['handleId']);
315325
case 'setInterval':
316326
return this._clearInterval(task.data['handleId']);

Diff for: test/zone-spec/fake-async-test.spec.ts

+39
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,45 @@ describe('FakeAsyncTestZoneSpec', () => {
516516
});
517517
});
518518

519+
describe('requestAnimationFrame', () => {
520+
const functions =
521+
['requestAnimationFrame', 'webkitRequestAnimationFrame', 'mozRequestAnimationFrame'];
522+
functions.forEach((fnName) => {
523+
describe(fnName, ifEnvSupports(fnName, () => {
524+
it('should schedule a requestAnimationFrame with timeout of 16ms', () => {
525+
fakeAsyncTestZone.run(() => {
526+
let ran = false;
527+
requestAnimationFrame(() => {
528+
ran = true;
529+
});
530+
531+
testZoneSpec.tick(6);
532+
expect(ran).toEqual(false);
533+
534+
testZoneSpec.tick(10);
535+
expect(ran).toEqual(true);
536+
});
537+
});
538+
it('should cancel a scheduled requestAnimatiomFrame', () => {
539+
fakeAsyncTestZone.run(() => {
540+
let ran = false;
541+
const id = requestAnimationFrame(() => {
542+
ran = true;
543+
});
544+
545+
testZoneSpec.tick(6);
546+
expect(ran).toEqual(false);
547+
548+
cancelAnimationFrame(id);
549+
550+
testZoneSpec.tick(10);
551+
expect(ran).toEqual(false);
552+
});
553+
});
554+
}));
555+
});
556+
});
557+
519558
describe('XHRs', ifEnvSupports('XMLHttpRequest', () => {
520559
it('should throw an exception if an XHR is initiated in the zone', () => {
521560
expect(() => {

0 commit comments

Comments
 (0)