🎥 Make videos programmatically with React
cleanup and add tests for FPS
Jonny Burgera9817a4349d9
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsxindex a8a442bcac..2c2641eb7b 100644--- a/packages/core/src/Composition.tsx+++ b/packages/core/src/Composition.tsx@@ -6,6 +6,8 @@ import {getIsEvaluation,removeStaticComposition,} from './register-root';+import {validateDurationInFrames} from './validation/validate-duration-in-frames';+import {validateFps} from './validation/validate-fps';type CompProps<T> =| {@@ -81,31 +83,8 @@ export const Composition = <T,>({`The "height" of a composition must be positive, but got ${height}.`);}- if (typeof durationInFrames !== 'number') {- throw new Error(- `The "durationInFrames" of a composition must be a number, but you passed a ${typeof durationInFrames}`- );- }- if (durationInFrames <= 0) {- throw new TypeError(- `The "durationInFrames" of a composition must be positive, but got ${durationInFrames}.`- );- }- if (durationInFrames % 1 !== 0) {- throw new TypeError(- `The "durationInFrames" of a composition must be an integer, but got ${durationInFrames}.`- );- }- if (typeof fps !== 'number') {- throw new Error(- `The "fps" of a composition must be a number, but you passed a ${typeof fps}`- );- }- if (fps <= 0) {- throw new TypeError(- `The "fps" of a composition must be positive, but got ${fps}.`- );- }+ validateDurationInFrames(durationInFrames);+ validateFps(fps);registerComposition<T>({durationInFrames,fps,diff --git a/packages/core/src/spring/index.ts b/packages/core/src/spring/index.tsindex cbe3cd1459..0b535ab171 100644--- a/packages/core/src/spring/index.ts+++ b/packages/core/src/spring/index.ts@@ -1,11 +1,11 @@import {springCalculation, SpringConfig} from './spring-utils';export function spring({- from = 0,- to = 1,frame,fps,config = {},+ from = 0,+ to = 1,}: {frame: number;fps: number;diff --git a/packages/core/src/spring/measure-spring.ts b/packages/core/src/spring/measure-spring.tsindex de2368c991..a844fec81f 100644--- a/packages/core/src/spring/measure-spring.ts+++ b/packages/core/src/spring/measure-spring.ts@@ -1,11 +1,12 @@+import {validateFps} from '../validation/validate-fps';import {springCalculation, SpringConfig} from './spring-utils';export function measureSpring({- from = 0,- to = 1,fps,config = {},threshold = 0.005,+ from = 0,+ to = 1,}: {fps: number;config?: Partial<SpringConfig>;@@ -18,6 +19,12 @@ export function measureSpring({`threshold must be a number, got ${threshold} of type ${typeof threshold}`);}+ if (threshold === 0) {+ return Infinity;+ }+ if (threshold === 1) {+ return 0;+ }if (isNaN(threshold)) {throw new TypeError('Threshold is NaN');}@@ -27,9 +34,8 @@ export function measureSpring({if (threshold < 0) {throw new TypeError('Threshold is below 0');}- if (threshold > 1) {- throw new TypeError('Threshold is above 1');- }++ validateFps(fps);const range = Math.abs(from - to);let frame = 0;@@ -51,7 +57,7 @@ export function measureSpring({);};let difference = calcDifference();- while (difference > threshold) {+ while (difference >= threshold) {frame++;animation = calc();difference = calcDifference();@@ -64,7 +70,7 @@ export function measureSpring({frame++;animation = calc();difference = calcDifference();- if (difference > threshold) {+ if (difference >= threshold) {i = 0;finishedFrame = frame + 1;}diff --git a/packages/core/src/test/composition-validation.test.tsx b/packages/core/src/test/composition-validation.test.tsxindex fbcd2ba36d..da8092202f 100644--- a/packages/core/src/test/composition-validation.test.tsx+++ b/packages/core/src/test/composition-validation.test.tsx@@ -296,7 +296,7 @@ test('It should throw if fps is of a composition is negative', () => {/></RemotionRoot>),- /The "fps" of a composition must be positive, but got -30./+ /"fps" must be positive, but got -30./);});test('It should throw if fps=0 of a composition is boundary off-point', () => {@@ -314,7 +314,7 @@ test('It should throw if fps=0 of a composition is boundary off-point', () => {/></RemotionRoot>),- /The "fps" of a composition must be positive, but got 0./+ /"fps" must be positive, but got 0./);});test('It should not throw if fps=1 of a composition is boundary on-point', () => {@@ -347,6 +347,6 @@ test('It should throw if fps of a composition is not a number', () => {/></RemotionRoot>),- /The "fps" of a composition must be a number, but you passed a string/+ /"fps" must be a number, but you passed a string/);});diff --git a/packages/core/src/test/measure-spring.test.ts b/packages/core/src/test/measure-spring.test.tsindex 569c5afc48..0bb9dba819 100644--- a/packages/core/src/test/measure-spring.test.ts+++ b/packages/core/src/test/measure-spring.test.ts@@ -1,4 +1,5 @@import {measureSpring, spring} from '../spring';+import {expectToThrow} from './expect-to-throw';test('Measure spring should work', () => {const duration = measureSpring({@@ -18,3 +19,41 @@ test('Measure spring should work', () => {})).not.toBe(1);});++test('Higher threshold should lead to faster spring', () => {+ expect(measureSpring({fps: 30, threshold: 0.05})).toBeLessThan(+ measureSpring({fps: 30, threshold: 0.01})+ );+});++test('Lower threshold should lead to slower spring', () => {+ expect(measureSpring({fps: 30, threshold: 0.001})).toBeGreaterThan(+ measureSpring({fps: 30, threshold: 0.01})+ );+});++test('Threshold edge cases', () => {+ expect(measureSpring({fps: 30, threshold: 0})).toBe(Infinity);+ expect(measureSpring({fps: 30, threshold: 1})).toBe(0);++ expectToThrow(+ () => measureSpring({fps: 30, threshold: NaN}),+ /Threshold is NaN/+ );+ expectToThrow(+ () => measureSpring({fps: 30, threshold: Infinity}),+ /Threshold is not finite/+ );+ expectToThrow(+ // @ts-expect-error+ () => measureSpring({fps: 30, threshold: null}),+ /threshold must be a number, got null of type object/+ );+});++test('Should throw on invalid FPS', () => {+ expectToThrow(+ () => measureSpring({fps: 0}),+ /"fps" must be positive, but got 0./+ );+});diff --git a/packages/core/src/validation/validate-duration-in-frames.ts b/packages/core/src/validation/validate-duration-in-frames.tsnew file mode 100644index 0000000000..c51663eadd--- /dev/null+++ b/packages/core/src/validation/validate-duration-in-frames.ts@@ -0,0 +1,17 @@+export const validateDurationInFrames = (durationInFrames: number) => {+ if (typeof durationInFrames !== 'number') {+ throw new Error(+ `The "durationInFrames" of a composition must be a number, but you passed a ${typeof durationInFrames}`+ );+ }+ if (durationInFrames <= 0) {+ throw new TypeError(+ `The "durationInFrames" of a composition must be positive, but got ${durationInFrames}.`+ );+ }+ if (durationInFrames % 1 !== 0) {+ throw new TypeError(+ `The "durationInFrames" of a composition must be an integer, but got ${durationInFrames}.`+ );+ }+};diff --git a/packages/core/src/validation/validate-fps.ts b/packages/core/src/validation/validate-fps.tsnew file mode 100644index 0000000000..4f17bb1f09--- /dev/null+++ b/packages/core/src/validation/validate-fps.ts@@ -0,0 +1,14 @@+export const validateFps = (fps: number) => {+ if (typeof fps !== 'number') {+ throw new Error(`"fps" must be a number, but you passed a ${typeof fps}`);+ }+ if (!Number.isFinite(fps)) {+ throw new Error(`"fps" must be a finite, but you passed ${fps}`);+ }+ if (isNaN(fps)) {+ throw new Error(`"fps" must not be NaN, but got ${fps}`);+ }+ if (fps <= 0) {+ throw new TypeError(`"fps" must be positive, but got ${fps}.`);+ }+};