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

Commit eca04b0

Browse files
vikermanmhevery
authored andcommitted
fix(spec): do not count requestAnimationFrame as a pending timer (#854)
We added a new fature to FakeAsyncTestSpec to treat a requestAnimationFrame as a timer with 16ms. However this breaks existing Angular project tests because the RAF is reported as a pending timer at the end of the test. This change makes sure that existing Angular tests are not broken by not counting RAF-s as pending timers at the end of a test.
1 parent ccf002d commit eca04b0

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

lib/zone-spec/fake-async-test.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
args: any[];
1515
delay: number;
1616
isPeriodic: boolean;
17+
isRequestAnimationFrame: boolean;
1718
}
1819

1920
interface MicroTaskScheduledFunction {
@@ -35,7 +36,7 @@
3536

3637
scheduleFunction(
3738
cb: Function, delay: number, args: any[] = [], isPeriodic: boolean = false,
38-
id: number = -1): number {
39+
isRequestAnimationFrame: boolean = false, id: number = -1): number {
3940
let currentId: number = id < 0 ? this.nextId++ : id;
4041
let endTime = this._currentTime + delay;
4142

@@ -46,7 +47,8 @@
4647
func: cb,
4748
args: args,
4849
delay: delay,
49-
isPeriodic: isPeriodic
50+
isPeriodic: isPeriodic,
51+
isRequestAnimationFrame: isRequestAnimationFrame
5052
};
5153
let i = 0;
5254
for (; i < this._schedulerQueue.length; i++) {
@@ -100,7 +102,8 @@
100102
' tasks. Does your code use a polling timeout?');
101103
}
102104
// If the only remaining tasks are periodic, finish flushing.
103-
if (!(this._schedulerQueue.filter(task => !task.isPeriodic).length)) {
105+
if (!(this._schedulerQueue.filter(task => !task.isPeriodic && !task.isRequestAnimationFrame)
106+
.length)) {
104107
break;
105108
}
106109
let current = this._schedulerQueue.shift();
@@ -131,7 +134,7 @@
131134
pendingPeriodicTimers: number[] = [];
132135
pendingTimers: number[] = [];
133136

134-
constructor(namePrefix: string) {
137+
constructor(namePrefix: string, private trackPendingRequestAnimationFrame = false) {
135138
this.name = 'fakeAsyncTestZone for ' + namePrefix;
136139
}
137140

@@ -174,7 +177,7 @@
174177
return () => {
175178
// Requeue the timer callback if it's not been canceled.
176179
if (this.pendingPeriodicTimers.indexOf(id) !== -1) {
177-
this._scheduler.scheduleFunction(fn, interval, args, true, id);
180+
this._scheduler.scheduleFunction(fn, interval, args, true, false, id);
178181
}
179182
};
180183
}
@@ -185,12 +188,14 @@
185188
};
186189
}
187190

188-
private _setTimeout(fn: Function, delay: number, args: any[]): number {
191+
private _setTimeout(fn: Function, delay: number, args: any[], isTimer = true): number {
189192
let removeTimerFn = this._dequeueTimer(this._scheduler.nextId);
190193
// Queue the callback and dequeue the timer on success and error.
191194
let cb = this._fnAndFlush(fn, {onSuccess: removeTimerFn, onError: removeTimerFn});
192-
let id = this._scheduler.scheduleFunction(cb, delay, args);
193-
this.pendingTimers.push(id);
195+
let id = this._scheduler.scheduleFunction(cb, delay, args, false, !isTimer);
196+
if (isTimer) {
197+
this.pendingTimers.push(id);
198+
}
194199
return id;
195200
}
196201

@@ -302,7 +307,9 @@
302307
case 'mozRequestAnimationFrame':
303308
// Simulate a requestAnimationFrame by using a setTimeout with 16 ms.
304309
// (60 frames per second)
305-
task.data['handleId'] = this._setTimeout(task.invoke, 16, (task.data as any)['args']);
310+
task.data['handleId'] = this._setTimeout(
311+
task.invoke, 16, (task.data as any)['args'],
312+
this.trackPendingRequestAnimationFrame);
306313
break;
307314
default:
308315
throw new Error('Unknown macroTask scheduled in fake async test: ' + task.source);

test/zone-spec/fake-async-test.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,13 @@ describe('FakeAsyncTestZoneSpec', () => {
555555
expect(ran).toEqual(true);
556556
});
557557
});
558+
it('does not count as a pending timer', () => {
559+
fakeAsyncTestZone.run(() => {
560+
requestAnimationFrame(() => {});
561+
});
562+
expect(testZoneSpec.pendingTimers.length).toBe(0);
563+
expect(testZoneSpec.pendingPeriodicTimers.length).toBe(0);
564+
});
558565
it('should cancel a scheduled requestAnimatiomFrame', () => {
559566
fakeAsyncTestZone.run(() => {
560567
let ran = false;

0 commit comments

Comments
 (0)