From c43629ee8b81678d8e03695c21baa64444db28f0 Mon Sep 17 00:00:00 2001 From: Coltin Kifer Date: Sun, 8 Sep 2024 18:16:37 +0000 Subject: [PATCH] test: basic animate group tests, trigger onEnter and onExit --- src/AnimateGroup.jsx | 2 +- src/AnimateGroupChild.jsx | 1 + test/AnimateGroup.spec.tsx | 24 +++++++++ test/AnimateGroupChild.spec.tsx | 86 +++++++++++++++++++++++++++++++-- test/configUpdate.spec.ts | 7 ++- 5 files changed, 111 insertions(+), 9 deletions(-) diff --git a/src/AnimateGroup.jsx b/src/AnimateGroup.jsx index 9b56424..16074bb 100644 --- a/src/AnimateGroup.jsx +++ b/src/AnimateGroup.jsx @@ -13,7 +13,7 @@ function AnimateGroup(props) { appearOptions={appear} enterOptions={enter} leaveOptions={leave} - key={`child-${index}`} // eslint-disable-line + key={`child-${index}`} // eslint-disable-line > {child} diff --git a/src/AnimateGroupChild.jsx b/src/AnimateGroupChild.jsx index 25e64ca..705109f 100644 --- a/src/AnimateGroupChild.jsx +++ b/src/AnimateGroupChild.jsx @@ -66,6 +66,7 @@ class AnimateGroupChild extends Component { } render() { + // eslint-disable-next-line @typescript-eslint/no-unused-vars const { children, appearOptions, enterOptions, leaveOptions, ...props } = this.props; return ( diff --git a/test/AnimateGroup.spec.tsx b/test/AnimateGroup.spec.tsx index 528f326..cf01f9e 100644 --- a/test/AnimateGroup.spec.tsx +++ b/test/AnimateGroup.spec.tsx @@ -6,4 +6,28 @@ describe('AnimateGroup', () => { it('Should render AnimateGroup', () => { render(); }); + + it('Should render children wrapped in the default component span', () => { + const { container } = render( + +
+
+
, + ); + + const isSpan = container.firstChild instanceof HTMLSpanElement; + expect(isSpan).toBe(true); + }); + + it('Should render children wrapped in provided component type div', () => { + const { container } = render( + +
+
+
, + ); + + const isDiv = container.firstChild instanceof HTMLDivElement; + expect(isDiv).toBe(true); + }); }); diff --git a/test/AnimateGroupChild.spec.tsx b/test/AnimateGroupChild.spec.tsx index d46bb5b..39d567b 100644 --- a/test/AnimateGroupChild.spec.tsx +++ b/test/AnimateGroupChild.spec.tsx @@ -1,13 +1,91 @@ -import React from 'react'; -import { render } from '@testing-library/react'; +import React, { useState } from 'react'; +import { act, render, screen } from '@testing-library/react'; import AnimateGroupChild from '../src/AnimateGroupChild'; describe('AnimateGroupChild', () => { - it('Should render AnimateGroupChild', () => { - render( + it('Should render AnimateGroupChild and its children', () => { + const { container } = render(
, ); + + const isDiv = container.firstChild instanceof HTMLDivElement; + expect(isDiv).toBe(true); + }); + + it('Should render AnimateGroupChild with basic options set', () => { + const appearOptions = { duration: 0.5, steps: [] }; + const leaveOptions = { duration: 0.5, steps: [{ duration: 0.2 }] }; + const enterOptions = { duration: 0.5, steps: [{ duration: 0.2 }, { duration: 0.4 }] }; + const { container } = render( + +
+ , + ); + }); + + const Example = ({ enterOptions, inPropDefault }) => { + const [inProp, setInProp] = useState(inPropDefault); + + const appearOptions = { duration: 0.5, steps: [] }; + const leaveOptions = { duration: 0.5, steps: [{ duration: 0.2, style: { opacity: 0 } }] }; + + return ( + <> + + +
+ + + ); + }; + + it('Should render AnimateGroupChild and trigger enter state with custom onAnimationEnd', () => { + const enterOptions = { + duration: 0.5, + steps: [ + { duration: 0.2, style: { opacity: 10 } }, + { duration: 0.4, style: { opacity: 10 } }, + ], + onAnimationEnd: vi.fn(), + }; + + render(); + // enter state triggered + act(() => { + screen.getByText('click me').click(); + }); + }); + + it('Should render AnimateGroupChild and trigger enter state without custom onAnimationEnd', () => { + const enterOptions = { + duration: 0.5, + steps: [ + { duration: 0.2, style: { opacity: 10 } }, + { duration: 0.4, style: { opacity: 10 } }, + ], + }; + render(); + // enter state triggered + act(() => { + screen.getByText('click me').click(); + }); + }); + + it('Should render AnimateGroupChild and trigger exit state', () => { + const enterOptions = { duration: 0.5, steps: [{ duration: 0.2, style: { opacity: 0 } }, { duration: 0.4 }] }; + render(); + // enter state triggered + act(() => { + screen.getByText('click me').click(); + }); }); }); diff --git a/test/configUpdate.spec.ts b/test/configUpdate.spec.ts index 5151d71..5a1b87b 100644 --- a/test/configUpdate.spec.ts +++ b/test/configUpdate.spec.ts @@ -1,7 +1,5 @@ -import * as util from '../src/util'; import animationFunction, { alpha } from '../src/configUpdate'; import { configEasing } from '../src/easing'; -import { waitFor } from '@testing-library/react'; let requestAnimationFrameMock; let cancelAnimationFrameMock; @@ -65,10 +63,10 @@ describe('animationFunction', () => { }); it('should use timingUpdate if easing.isStepper is false', () => { - easing = vi.fn(() => vi.fn()); + easing = configEasing('spring'); const from = { x: 0, y: 0 }; const to = { x: 100, y: 100 }; - const duration = 1; + const duration = 20; const startAnimation = animationFunction(from, to, easing, duration, render); startAnimation(); @@ -102,6 +100,7 @@ describe('animationFunction', () => { startAnimation(); vi.advanceTimersToNextFrame(); + vi.runAllTimers(); expect(render).toHaveBeenCalledWith({ x: from.x,