-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(motion): Add unit tests for Fade component (#32945)
- Loading branch information
1 parent
7467ac0
commit 345452c
Showing
3 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
...react-components/react-motion-components-preview/library/src/components/Fade/Fade.test.ts
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
...eact-components/react-motion-components-preview/library/src/components/Fade/Fade.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as React from 'react'; | ||
import { Fade, FadeExaggerated, FadeSnappy } from './Fade'; | ||
import { render } from '@testing-library/react'; | ||
import { motionTokens } from '@fluentui/react-motion'; | ||
import { mockAnimation } from '../../testing/testUtils'; | ||
|
||
describe('Fade motion component', () => { | ||
let originalAnimate: typeof HTMLElement.prototype.animate; | ||
let animateSpy: jest.SpyInstance; | ||
const testElement = <div data-testid="fade-content">Test</div>; | ||
|
||
// JSDOM does not support the Web Animations API, so create a mock animate() before spying on it | ||
beforeAll(() => { | ||
originalAnimate = HTMLElement.prototype.animate; | ||
HTMLElement.prototype.animate = () => mockAnimation(); | ||
}); | ||
|
||
beforeEach(() => { | ||
animateSpy = jest.spyOn(HTMLElement.prototype, 'animate'); | ||
}); | ||
|
||
afterEach(() => { | ||
animateSpy.mockRestore(); | ||
}); | ||
|
||
afterAll(() => { | ||
HTMLElement.prototype.animate = originalAnimate; | ||
}); | ||
|
||
it('should render Fade with correct opacity keyframes, duration and easing (visible=false -> true -> false)', () => { | ||
const { rerender } = render(<Fade visible={false}>{testElement}</Fade>); | ||
|
||
// Testing fade in motion | ||
rerender(<Fade visible={true}>{testElement}</Fade>); | ||
expect(animateSpy).toHaveBeenCalledWith( | ||
[{ opacity: 0 }, { opacity: 1 }], | ||
expect.objectContaining({ duration: motionTokens.durationNormal, easing: motionTokens.curveEasyEase }), | ||
); | ||
|
||
// Testing fade out motion | ||
rerender(<Fade visible={false}>{testElement}</Fade>); | ||
expect(animateSpy).toHaveBeenCalledWith( | ||
[{ opacity: 1 }, { opacity: 0 }], | ||
expect.objectContaining({ duration: motionTokens.durationNormal, easing: motionTokens.curveEasyEase }), | ||
); | ||
}); | ||
|
||
it('should render Snappy variant of Fade component with correct opacity keyframes, duration and easing', () => { | ||
const { rerender } = render(<FadeSnappy visible={false}>{testElement}</FadeSnappy>); | ||
|
||
rerender(<FadeSnappy visible={true}>{testElement}</FadeSnappy>); | ||
|
||
expect(animateSpy).toHaveBeenCalledWith( | ||
[{ opacity: 0 }, { opacity: 1 }], | ||
expect.objectContaining({ duration: motionTokens.durationFast, easing: motionTokens.curveEasyEase }), | ||
); | ||
}); | ||
|
||
it('should render Exaggerated variant of Fade component with correct opacity keyframes, duration and easing', () => { | ||
const { rerender } = render(<FadeExaggerated visible={false}>{testElement}</FadeExaggerated>); | ||
|
||
rerender(<FadeExaggerated visible={true}>{testElement}</FadeExaggerated>); | ||
|
||
expect(animateSpy).toHaveBeenCalledWith( | ||
[{ opacity: 0 }, { opacity: 1 }], | ||
expect.objectContaining({ duration: motionTokens.durationGentle, easing: motionTokens.curveEasyEase }), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters