mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
test: fix fake-async-test spec to properly test prefixed animation frame functions
In the fake async test for `zone.js` there is some logic to test various prefixes of the request animation frame functions. This logic does not have any effect currently, except for generating the same test at maximum three times. This commit fixes the test to actually test what it meant to do initially.
This commit is contained in:
@@ -748,92 +748,105 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('requestAnimationFrame', () => {
|
||||
const functions =
|
||||
['requestAnimationFrame', 'webkitRequestAnimationFrame', 'mozRequestAnimationFrame'];
|
||||
functions.forEach((fnName) => {
|
||||
if ((global as any)[fnName] !== undefined) {
|
||||
describe(fnName, () => {
|
||||
it('should schedule a requestAnimationFrame with timeout of 16ms', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
requestAnimationFrame(() => {
|
||||
ran = true;
|
||||
});
|
||||
const animationFrameFns = [
|
||||
['requestAnimationFrame', 'cancelAnimationFrame'],
|
||||
['webkitRequestAnimationFrame', 'webkitCancelAnimationFrame'],
|
||||
['mozRequestAnimationFrame', 'mozCancelAnimationFrame'],
|
||||
];
|
||||
|
||||
testZoneSpec.tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
const availableAnimationFrameFns =
|
||||
animationFrameFns
|
||||
.map(([fnName,
|
||||
cancelFnName]) => [fnName, (global as any)[fnName], (global as any)[cancelFnName]])
|
||||
.filter(
|
||||
([_, fn]) => fn !==
|
||||
undefined) as [string, typeof requestAnimationFrame, typeof cancelAnimationFrame][];
|
||||
|
||||
testZoneSpec.tick(10);
|
||||
expect(ran).toEqual(true);
|
||||
});
|
||||
});
|
||||
it('does not count as a pending timer', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrame(() => {});
|
||||
});
|
||||
expect(testZoneSpec.pendingTimers.length).toBe(0);
|
||||
expect(testZoneSpec.pendingPeriodicTimers.length).toBe(0);
|
||||
});
|
||||
it('should cancel a scheduled requestAnimatiomFrame', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
const id = requestAnimationFrame(() => {
|
||||
ran = true;
|
||||
});
|
||||
if (availableAnimationFrameFns.length > 0) {
|
||||
describe('requestAnimationFrame', () => {
|
||||
availableAnimationFrameFns.forEach(
|
||||
([name, requestAnimationFrameFn, cancelAnimationFrameFn]) => {
|
||||
describe(name, () => {
|
||||
it('should schedule a requestAnimationFrame with timeout of 16ms', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
requestAnimationFrameFn(() => {
|
||||
ran = true;
|
||||
});
|
||||
|
||||
testZoneSpec.tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
testZoneSpec.tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
cancelAnimationFrame(id);
|
||||
|
||||
testZoneSpec.tick(10);
|
||||
expect(ran).toEqual(false);
|
||||
});
|
||||
});
|
||||
it('is not flushed when flushPeriodic is false', () => {
|
||||
let ran = false;
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrame(() => {
|
||||
ran = true;
|
||||
});
|
||||
testZoneSpec.flush(20);
|
||||
expect(ran).toEqual(false);
|
||||
});
|
||||
});
|
||||
it('is flushed when flushPeriodic is true', () => {
|
||||
let ran = false;
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrame(() => {
|
||||
ran = true;
|
||||
});
|
||||
const elapsed = testZoneSpec.flush(20, true);
|
||||
expect(elapsed).toEqual(16);
|
||||
expect(ran).toEqual(true);
|
||||
});
|
||||
});
|
||||
it('should pass timestamp as parameter', () => {
|
||||
let timestamp = 0;
|
||||
let timestamp1 = 0;
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrame((ts) => {
|
||||
timestamp = ts;
|
||||
requestAnimationFrame(ts1 => {
|
||||
timestamp1 = ts1;
|
||||
testZoneSpec.tick(10);
|
||||
expect(ran).toEqual(true);
|
||||
});
|
||||
});
|
||||
it('does not count as a pending timer', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrameFn(() => {});
|
||||
});
|
||||
expect(testZoneSpec.pendingTimers.length).toBe(0);
|
||||
expect(testZoneSpec.pendingPeriodicTimers.length).toBe(0);
|
||||
});
|
||||
it('should cancel a scheduled requestAnimationFrame', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
const id = requestAnimationFrameFn(() => {
|
||||
ran = true;
|
||||
});
|
||||
|
||||
testZoneSpec.tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
cancelAnimationFrameFn(id);
|
||||
|
||||
testZoneSpec.tick(10);
|
||||
expect(ran).toEqual(false);
|
||||
});
|
||||
});
|
||||
it('is not flushed when flushPeriodic is false', () => {
|
||||
let ran = false;
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrameFn(() => {
|
||||
ran = true;
|
||||
});
|
||||
testZoneSpec.flush(20);
|
||||
expect(ran).toEqual(false);
|
||||
});
|
||||
});
|
||||
it('is flushed when flushPeriodic is true', () => {
|
||||
let ran = false;
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrameFn(() => {
|
||||
ran = true;
|
||||
});
|
||||
const elapsed = testZoneSpec.flush(20, true);
|
||||
expect(elapsed).toEqual(16);
|
||||
expect(ran).toEqual(true);
|
||||
});
|
||||
});
|
||||
it('should pass timestamp as parameter', () => {
|
||||
let timestamp = 0;
|
||||
let timestamp1 = 0;
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrameFn((ts) => {
|
||||
timestamp = ts;
|
||||
requestAnimationFrameFn(ts1 => {
|
||||
timestamp1 = ts1;
|
||||
});
|
||||
});
|
||||
const elapsed = testZoneSpec.flush(20, true);
|
||||
const elapsed1 = testZoneSpec.flush(20, true);
|
||||
expect(elapsed).toEqual(16);
|
||||
expect(elapsed1).toEqual(16);
|
||||
expect(timestamp).toEqual(16);
|
||||
expect(timestamp1).toEqual(32);
|
||||
});
|
||||
});
|
||||
const elapsed = testZoneSpec.flush(20, true);
|
||||
const elapsed1 = testZoneSpec.flush(20, true);
|
||||
expect(elapsed).toEqual(16);
|
||||
expect(elapsed1).toEqual(16);
|
||||
expect(timestamp).toEqual(16);
|
||||
expect(timestamp1).toEqual(32);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe(
|
||||
'XHRs', ifEnvSupports('XMLHttpRequest', () => {
|
||||
|
||||
Reference in New Issue
Block a user