Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit c7b1bb1

Browse files
committed
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.
1 parent d2b444a commit c7b1bb1

File tree

1 file changed

+87
-74
lines changed

1 file changed

+87
-74
lines changed

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

Lines changed: 87 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -748,92 +748,105 @@ describe('FakeAsyncTestZoneSpec', () => {
748748
});
749749
});
750750

751-
describe('requestAnimationFrame', () => {
752-
const functions =
753-
['requestAnimationFrame', 'webkitRequestAnimationFrame', 'mozRequestAnimationFrame'];
754-
functions.forEach((fnName) => {
755-
if ((global as any)[fnName] !== undefined) {
756-
describe(fnName, () => {
757-
it('should schedule a requestAnimationFrame with timeout of 16ms', () => {
758-
fakeAsyncTestZone.run(() => {
759-
let ran = false;
760-
requestAnimationFrame(() => {
761-
ran = true;
751+
const animationFrameFns = [
752+
['requestAnimationFrame', 'cancelAnimationFrame'],
753+
['webkitRequestAnimationFrame', 'webkitCancelAnimationFrame'],
754+
['mozRequestAnimationFrame', 'mozCancelAnimationFrame'],
755+
];
756+
757+
const availableAnimationFrameFns =
758+
animationFrameFns
759+
.map(([fnName,
760+
cancelFnName]) => [fnName, (global as any)[fnName], (global as any)[cancelFnName]])
761+
.filter(
762+
([_, fn]) => fn !==
763+
undefined) as [string, typeof requestAnimationFrame, typeof cancelAnimationFrame][];
764+
765+
if (availableAnimationFrameFns.length > 0) {
766+
describe('requestAnimationFrame', () => {
767+
availableAnimationFrameFns.forEach(
768+
([name, requestAnimationFrameFn, cancelAnimationFrameFn]) => {
769+
describe(name, () => {
770+
it('should schedule a requestAnimationFrame with timeout of 16ms', () => {
771+
fakeAsyncTestZone.run(() => {
772+
let ran = false;
773+
requestAnimationFrameFn(() => {
774+
ran = true;
775+
});
776+
777+
testZoneSpec.tick(6);
778+
expect(ran).toEqual(false);
779+
780+
testZoneSpec.tick(10);
781+
expect(ran).toEqual(true);
782+
});
762783
});
763-
764-
testZoneSpec.tick(6);
765-
expect(ran).toEqual(false);
766-
767-
testZoneSpec.tick(10);
768-
expect(ran).toEqual(true);
769-
});
770-
});
771-
it('does not count as a pending timer', () => {
772-
fakeAsyncTestZone.run(() => {
773-
requestAnimationFrame(() => {});
774-
});
775-
expect(testZoneSpec.pendingTimers.length).toBe(0);
776-
expect(testZoneSpec.pendingPeriodicTimers.length).toBe(0);
777-
});
778-
it('should cancel a scheduled requestAnimatiomFrame', () => {
779-
fakeAsyncTestZone.run(() => {
780-
let ran = false;
781-
const id = requestAnimationFrame(() => {
782-
ran = true;
784+
it('does not count as a pending timer', () => {
785+
fakeAsyncTestZone.run(() => {
786+
requestAnimationFrameFn(() => {});
787+
});
788+
expect(testZoneSpec.pendingTimers.length).toBe(0);
789+
expect(testZoneSpec.pendingPeriodicTimers.length).toBe(0);
783790
});
791+
it('should cancel a scheduled requestAnimationFrame', () => {
792+
fakeAsyncTestZone.run(() => {
793+
let ran = false;
794+
const id = requestAnimationFrameFn(() => {
795+
ran = true;
796+
});
784797

785-
testZoneSpec.tick(6);
786-
expect(ran).toEqual(false);
798+
testZoneSpec.tick(6);
799+
expect(ran).toEqual(false);
787800

788-
cancelAnimationFrame(id);
801+
cancelAnimationFrameFn(id);
789802

790-
testZoneSpec.tick(10);
791-
expect(ran).toEqual(false);
792-
});
793-
});
794-
it('is not flushed when flushPeriodic is false', () => {
795-
let ran = false;
796-
fakeAsyncTestZone.run(() => {
797-
requestAnimationFrame(() => {
798-
ran = true;
803+
testZoneSpec.tick(10);
804+
expect(ran).toEqual(false);
805+
});
799806
});
800-
testZoneSpec.flush(20);
801-
expect(ran).toEqual(false);
802-
});
803-
});
804-
it('is flushed when flushPeriodic is true', () => {
805-
let ran = false;
806-
fakeAsyncTestZone.run(() => {
807-
requestAnimationFrame(() => {
808-
ran = true;
807+
it('is not flushed when flushPeriodic is false', () => {
808+
let ran = false;
809+
fakeAsyncTestZone.run(() => {
810+
requestAnimationFrameFn(() => {
811+
ran = true;
812+
});
813+
testZoneSpec.flush(20);
814+
expect(ran).toEqual(false);
815+
});
809816
});
810-
const elapsed = testZoneSpec.flush(20, true);
811-
expect(elapsed).toEqual(16);
812-
expect(ran).toEqual(true);
813-
});
814-
});
815-
it('should pass timestamp as parameter', () => {
816-
let timestamp = 0;
817-
let timestamp1 = 0;
818-
fakeAsyncTestZone.run(() => {
819-
requestAnimationFrame((ts) => {
820-
timestamp = ts;
821-
requestAnimationFrame(ts1 => {
822-
timestamp1 = ts1;
817+
it('is flushed when flushPeriodic is true', () => {
818+
let ran = false;
819+
fakeAsyncTestZone.run(() => {
820+
requestAnimationFrameFn(() => {
821+
ran = true;
822+
});
823+
const elapsed = testZoneSpec.flush(20, true);
824+
expect(elapsed).toEqual(16);
825+
expect(ran).toEqual(true);
826+
});
827+
});
828+
it('should pass timestamp as parameter', () => {
829+
let timestamp = 0;
830+
let timestamp1 = 0;
831+
fakeAsyncTestZone.run(() => {
832+
requestAnimationFrameFn((ts) => {
833+
timestamp = ts;
834+
requestAnimationFrameFn(ts1 => {
835+
timestamp1 = ts1;
836+
});
837+
});
838+
const elapsed = testZoneSpec.flush(20, true);
839+
const elapsed1 = testZoneSpec.flush(20, true);
840+
expect(elapsed).toEqual(16);
841+
expect(elapsed1).toEqual(16);
842+
expect(timestamp).toEqual(16);
843+
expect(timestamp1).toEqual(32);
823844
});
824845
});
825-
const elapsed = testZoneSpec.flush(20, true);
826-
const elapsed1 = testZoneSpec.flush(20, true);
827-
expect(elapsed).toEqual(16);
828-
expect(elapsed1).toEqual(16);
829-
expect(timestamp).toEqual(16);
830-
expect(timestamp1).toEqual(32);
831846
});
832847
});
833-
});
834-
}
835848
});
836-
});
849+
}
837850

838851
describe(
839852
'XHRs', ifEnvSupports('XMLHttpRequest', () => {

0 commit comments

Comments
 (0)