Skip to content

🎥 Make videos programmatically with React

write tests

Jonny Burgera7739aef9528
diff --git a/packages/core/src/sequencing/index.tsx b/packages/core/src/sequencing/index.tsx
index 967715e154..3a1a15c264 100644
--- a/packages/core/src/sequencing/index.tsx
+++ b/packages/core/src/sequencing/index.tsx
@@ -167,7 +167,7 @@ export const Sequence: React.FC<SequenceProps> = ({
return (
<SequenceContext.Provider value={contextValue}>
- {layout === 'absolute-fill' ? (
+ {content === null ? null : layout === 'absolute-fill' ? (
<div
style={{
position: 'absolute',
diff --git a/packages/core/src/stagger/index.tsx b/packages/core/src/stagger/index.tsx
index 445b459aa8..3dea62062d 100644
--- a/packages/core/src/stagger/index.tsx
+++ b/packages/core/src/stagger/index.tsx
@@ -5,6 +5,7 @@ import {validateDurationInFrames} from '../validation/validate-duration-in-frame
type StaggerChildProps = PropsWithChildren<
{
durationInFrames: number;
+ offset?: number;
} & Pick<SequenceProps, 'layout' | 'name'>
>;
@@ -16,13 +17,21 @@ const StaggerChild = ({children}: StaggerChildProps) => {
const Stagger: FC = ({children}) => {
const childrenValue = useMemo(() => {
let startFrame = 0;
- return Children.map(children, (child) => {
- const castedChild = (child as unknown) as {
- props: StaggerChildProps;
- type: typeof StaggerChild;
- };
- if (!castedChild || !castedChild.props.children) {
- throw new TypeError(`The StaggerChild component must have children.`);
+ return Children.map(children, (child, i) => {
+ const castedChild = (child as unknown) as
+ | {
+ props: StaggerChildProps;
+ type: typeof StaggerChild;
+ }
+ | string;
+ if (typeof castedChild === 'string') {
+ if (castedChild.trim() === '') {
+ return null;
+ }
+
+ throw new TypeError(
+ `The <Stagger> component only accepts a list of <StaggerChild /> components as it's children, but you passed a string "${castedChild}"`
+ );
}
if (castedChild.type !== StaggerChild) {
@@ -31,6 +40,14 @@ const Stagger: FC = ({children}) => {
);
}
+ const debugInfo = `index = ${i}, duration = ${castedChild.props.durationInFrames}`;
+
+ if (!castedChild || !castedChild.props.children) {
+ throw new TypeError(
+ `A <StaggerChild /> component (${debugInfo}) doesn't have any children.`
+ );
+ }
+
const durationInFramesProp = castedChild.props.durationInFrames;
const {
durationInFrames,
@@ -38,8 +55,27 @@ const Stagger: FC = ({children}) => {
...passedProps
} = castedChild.props;
validateDurationInFrames(durationInFramesProp, `<StaggerChild />`);
- const currentStartFrame = startFrame;
- startFrame += durationInFramesProp;
+ const offset = castedChild.props.offset ?? 0;
+ if (Number.isNaN(offset)) {
+ throw new TypeError(
+ `The "offset" property of a <StaggerChild /> must not be NaN, but got NaN (${debugInfo}).`
+ );
+ }
+
+ if (!Number.isFinite(offset)) {
+ throw new TypeError(
+ `The "offset" property of a <StaggerChild /> must be finite, but got ${offset} (${debugInfo}).`
+ );
+ }
+
+ if (offset % 1 !== 0) {
+ throw new TypeError(
+ `The "offset" property of a <StaggerChild /> must be finite, but got ${offset} (${debugInfo}).`
+ );
+ }
+
+ const currentStartFrame = startFrame + offset;
+ startFrame += durationInFramesProp + offset;
return (
<Sequence
from={currentStartFrame}
diff --git a/packages/core/src/test/composition-validation.test.tsx b/packages/core/src/test/composition-validation.test.tsx
index 3e0b7809ce..fd2a8add4d 100644
--- a/packages/core/src/test/composition-validation.test.tsx
+++ b/packages/core/src/test/composition-validation.test.tsx
@@ -174,7 +174,7 @@ describe('Composition-validation render should throw with invalid props', () =>
/>
</RemotionRoot>
),
- /The "durationInFrames" of a composition must be positive, but got -100./
+ /The "durationInFrames" prop of a <Composition \/> component must be positive, but got -100./
);
});
test('It should throw if durationInFrames=0 of a composition is boundary off-point', () => {
@@ -192,7 +192,7 @@ describe('Composition-validation render should throw with invalid props', () =>
/>
</RemotionRoot>
),
- /The "durationInFrames" of a composition must be positive, but got 0./
+ /The "durationInFrames" prop of a <Composition \/> component must be positive, but got 0./
);
});
test('It should throw if durationInFrames of a composition is not an integer', () => {
@@ -210,7 +210,7 @@ describe('Composition-validation render should throw with invalid props', () =>
/>
</RemotionRoot>
),
- /The "durationInFrames" of a composition must be an integer, but got 0.11./
+ /The "durationInFrames" prop of a <Composition \/> component must be an integer, but got 0.11./
);
});
test('It should throw if durationInFrames of a composition is not a number', () => {
@@ -229,7 +229,7 @@ describe('Composition-validation render should throw with invalid props', () =>
/>
</RemotionRoot>
),
- /The "durationInFrames" of a composition must be a number, but you passed a value of type string/
+ /The "durationInFrames" prop of a <Composition \/> component must be a number, but you passed a value of type string/
);
});
});
diff --git a/packages/core/src/test/stagger.test.tsx b/packages/core/src/test/stagger.test.tsx
index 6c3a6185af..65142c67d6 100644
--- a/packages/core/src/test/stagger.test.tsx
+++ b/packages/core/src/test/stagger.test.tsx
@@ -1,8 +1,260 @@
-test.todo('Basic stagger test');
-test.todo('Should not allow foreign elements');
-test.todo('Should allow layout prop');
-test.todo('Should not allow additional props');
-test.todo('Should throw if invalid or no duration provided');
-test.todo('Should allow whitespace');
-test.todo('Handle empty StaggerChild');
-test.todo('Should allow overlap prop');
+/* eslint-disable react/jsx-no-constructed-context-values */
+import {render} from '@testing-library/react';
+import React from 'react';
+import {Stagger, StaggerChild} from '../stagger';
+import {TimelineContext} from '../timeline-position-state';
+import {useCurrentFrame} from '../use-frame';
+import {expectToThrow} from './expect-to-throw';
+
+const First = () => {
+ const frame = useCurrentFrame();
+ return <div>{'first ' + frame}</div>;
+};
+
+const Second = () => {
+ const frame = useCurrentFrame();
+ return <div>{'second ' + frame}</div>;
+};
+
+const Third = () => {
+ const frame = useCurrentFrame();
+ return <div>{'third ' + frame}</div>;
+};
+
+const renderForFrame = (frame: number, markup: React.ReactNode) => {
+ return render(
+ <TimelineContext.Provider
+ value={{
+ rootId: '',
+ frame,
+ playing: false,
+ }}
+ >
+ {markup}
+ </TimelineContext.Provider>
+ );
+};
+
+test('Basic stagger test', () => {
+ const {queryByText} = renderForFrame(
+ 10,
+ <Stagger>
+ <StaggerChild durationInFrames={5}>
+ <First />
+ </StaggerChild>
+ <StaggerChild durationInFrames={5}>
+ <Second />
+ </StaggerChild>
+ <StaggerChild durationInFrames={5}>
+ <Third />
+ </StaggerChild>
+ </Stagger>
+ );
+
+ expect(queryByText(/^third\s0$/g)).not.toBe(null);
+});
+test('Should not allow foreign elements', () => {
+ expectToThrow(() => {
+ render(
+ <Stagger>
+ <First />
+ </Stagger>
+ );
+ }, /only accepts a/);
+});
+test('Should allow layout prop', () => {
+ const {container} = renderForFrame(
+ 0,
+ <Stagger>
+ <StaggerChild durationInFrames={1}>
+ <First />
+ </StaggerChild>
+ </Stagger>
+ );
+ expect(container.outerHTML).toBe(
+ '<div><div style="position: absolute; display: flex; width: 100%; height: 100%; top: 0px; bottom: 0px; left: 0px; right: 0px;"><div>first 0</div></div></div>'
+ );
+
+ const {container: withoutLayoutContainer} = renderForFrame(
+ 0,
+ <Stagger>
+ <StaggerChild durationInFrames={1} layout="none">
+ <First />
+ </StaggerChild>
+ </Stagger>
+ );
+ expect(withoutLayoutContainer.outerHTML).toBe(
+ '<div><div>first 0</div></div>'
+ );
+});
+test('Should render nothing after the end', () => {
+ const {container} = renderForFrame(
+ 10,
+ <Stagger>
+ <StaggerChild durationInFrames={1}>
+ <First />
+ </StaggerChild>
+ </Stagger>
+ );
+ expect(container.outerHTML).toBe('<div></div>');
+});
+test('Should throw if invalid or no duration provided', () => {
+ expectToThrow(() => {
+ renderForFrame(
+ 10,
+ <Stagger>
+ <StaggerChild durationInFrames={NaN}>
+ <First />
+ </StaggerChild>
+ </Stagger>
+ );
+ }, /The "durationInFrames" prop of a <StaggerChild \/> component must be an integer, but got NaN./);
+ expectToThrow(() => {
+ renderForFrame(
+ 10,
+ <Stagger>
+ {/**
+ * @ts-expect-error */}
+ <StaggerChild>
+ <First />
+ </StaggerChild>
+ </Stagger>
+ );
+ }, /The "durationInFrames" prop of a <StaggerChild \/> component must be a number, but you passed a value of type undefined/);
+});
+test('Should allow whitespace', () => {
+ const {queryByText} = renderForFrame(
+ 11,
+ <Stagger>
+ <StaggerChild durationInFrames={10}>
+ <First />
+ </StaggerChild>{' '}
+ <StaggerChild durationInFrames={10}>
+ <Second />
+ </StaggerChild>
+ </Stagger>
+ );
+ expect(queryByText(/^second\s1$/g)).not.toBe(null);
+});
+test('Handle empty StaggerChild', () => {
+ expectToThrow(
+ () =>
+ renderForFrame(
+ 11,
+ <Stagger>
+ <StaggerChild durationInFrames={10}>
+ <First />
+ </StaggerChild>
+ <StaggerChild durationInFrames={10} />
+ </Stagger>
+ ),
+ /A <StaggerChild \/> component \(index = 1, duration = 10\) doesn't have any children\./
+ );
+});
+
+test('Should allow negative overlap prop', () => {
+ const {container} = renderForFrame(
+ 4,
+ <Stagger>
+ <StaggerChild durationInFrames={5} layout="none">
+ <First />
+ </StaggerChild>
+ <StaggerChild offset={-1} layout="none" durationInFrames={5}>
+ <Second />
+ </StaggerChild>
+ </Stagger>
+ );
+ expect(container.outerHTML).toBe(
+ '<div><div>first 4</div><div>second 0</div></div>'
+ );
+});
+
+test('Should allow positive overlap prop', () => {
+ const {container} = renderForFrame(
+ 5,
+ <Stagger>
+ <StaggerChild durationInFrames={5} layout="none">
+ <First />
+ </StaggerChild>
+ <StaggerChild offset={1} layout="none" durationInFrames={5}>
+ <Second />
+ </StaggerChild>
+ </Stagger>
+ );
+ expect(container.outerHTML).toBe('<div></div>');
+});
+
+test('Should disallow NaN as offset prop', () => {
+ expectToThrow(() => {
+ renderForFrame(
+ 9,
+ <Stagger>
+ <StaggerChild offset={NaN} layout="none" durationInFrames={5}>
+ <Second />
+ </StaggerChild>
+ </Stagger>
+ );
+ }, /The "offset" property of a <StaggerChild \/> must not be NaN, but got NaN \(index = 0, duration = 5\)\./);
+});
+
+test('Should disallow Infinity as offset prop', () => {
+ expectToThrow(() => {
+ renderForFrame(
+ 9,
+ <Stagger>
+ <StaggerChild offset={Infinity} layout="none" durationInFrames={5}>
+ <Second />
+ </StaggerChild>
+ </Stagger>
+ );
+ }, /The "offset" property of a <StaggerChild \/> must be finite, but got Infinity \(index = 0, duration = 5\)\./);
+});
+
+test('Should disallow non-integer numbers as offset prop', () => {
+ expectToThrow(() => {
+ renderForFrame(
+ 9,
+ <Stagger>
+ <StaggerChild offset={Math.PI} layout="none" durationInFrames={5}>
+ <Second />
+ </StaggerChild>
+ </Stagger>
+ );
+ }, /The "offset" property of a <StaggerChild \/> must be finite, but got 3.141592653589793 \(index = 0, duration = 5\)\./);
+});
+
+test('Should cascade negative offset props', () => {
+ const {container} = renderForFrame(
+ 9,
+ <Stagger>
+ <StaggerChild durationInFrames={5} layout="none">
+ <First />
+ </StaggerChild>
+ <StaggerChild offset={-1} layout="none" durationInFrames={5}>
+ <Second />
+ </StaggerChild>
+ <StaggerChild layout="none" durationInFrames={5}>
+ <Third />
+ </StaggerChild>
+ </Stagger>
+ );
+ expect(container.outerHTML).toBe('<div><div>third 0</div></div>');
+});
+
+test('Should cascade positive offset props', () => {
+ const {container} = renderForFrame(
+ 11,
+ <Stagger>
+ <StaggerChild durationInFrames={5} layout="none">
+ <First />
+ </StaggerChild>
+ <StaggerChild offset={1} layout="none" durationInFrames={5}>
+ <Second />
+ </StaggerChild>
+ <StaggerChild layout="none" durationInFrames={5}>
+ <Third />
+ </StaggerChild>
+ </Stagger>
+ );
+ expect(container.outerHTML).toBe('<div><div>third 0</div></div>');
+});