From c7b1bb1f2839b7ec160eb04c53add1cf32258153 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Sat, 2 Jul 2022 08:31:23 +0000 Subject: [PATCH] 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. --- .../test/zone-spec/fake-async-test.spec.ts | 167 ++++++++++-------- 1 file changed, 90 insertions(+), 77 deletions(-) diff --git a/packages/zone.js/test/zone-spec/fake-async-test.spec.ts b/packages/zone.js/test/zone-spec/fake-async-test.spec.ts index 58a0a7cd6a4..a6a289f5806 100644 --- a/packages/zone.js/test/zone-spec/fake-async-test.spec.ts +++ b/packages/zone.js/test/zone-spec/fake-async-test.spec.ts @@ -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', () => {