Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(motion): improve Web Animations API detection in tests #32029

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
layershifter marked this conversation as resolved.
Show resolved Hide resolved
"type": "patch",
"comment": "fix: improve Web Animations API detection in tests",
"packageName": "@fluentui/react-motion",
"email": "olfedias@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import * as React from 'react';
import type { AtomMotion } from '../types';
import { createMotionComponent } from './createMotionComponent';

jest.mock('./createMotionComponent', () => {
// Add a mock for the `animate` method on the HTMLElement prototype as jsdom does not support it
Element.prototype.animate = jest.fn();

return jest.requireActual('./createMotionComponent');
});

const motion: AtomMotion = {
keyframes: [{ opacity: 0 }, { opacity: 1 }],
duration: 500,
Expand Down Expand Up @@ -42,10 +35,6 @@ function createElementMock() {
}

describe('createMotionComponent', () => {
it('has mock for .animate()', () => {
expect(Element.prototype.animate).toBeDefined();
});

it('creates a motion and plays it', () => {
const TestAtom = createMotionComponent(motion);
const { animateMock, ElementMock } = createElementMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createPresenceComponent } from './createPresenceComponent';
const keyframes = [{ opacity: 0 }, { opacity: 1 }];
const options = { duration: 500, fill: 'forwards' as const };

const TestAtom = createPresenceComponent({
const TestPresence = createPresenceComponent({
enter: { keyframes, ...options },
exit: { keyframes: keyframes.slice().reverse(), ...options },
});
Expand All @@ -23,9 +23,9 @@ const TestComponent: React.FC<{ appear?: boolean; finish?: () => void }> = props
return (
<>
<button onClick={() => setVisible(v => !v)}>Click me</button>
<TestAtom appear={appear} onMotionFinish={onMotionFinish} visible={visible} unmountOnExit>
<TestPresence appear={appear} onMotionFinish={onMotionFinish} visible={visible} unmountOnExit>
<div>Hello</div>
</TestAtom>
</TestPresence>
</>
);
};
Expand All @@ -37,7 +37,7 @@ const TestComponent: React.FC<{ appear?: boolean; finish?: () => void }> = props
//
// This test suite ensures that the component works correctly in tests using that environment.

describe('createPresenceComponent', () => {
describe('createPresenceComponent (jest)', () => {
it('does not support .animate()', () => {
expect(Element.prototype.animate).toBeUndefined();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* @jest-environment node
*/

// 👆 this is intentionally to test in SSR like environment

import * as React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';

import { createPresenceComponent } from './createPresenceComponent';

const keyframes = [{ opacity: 0 }, { opacity: 1 }];
const options = { duration: 500, fill: 'forwards' as const };

const TestPresence = createPresenceComponent({
enter: { keyframes, ...options },
exit: { keyframes: keyframes.slice().reverse(), ...options },
});

// Heads up!
//
// Unfortunately, jsdom (the optional environment for Jest) does not support the Web Animations API, which is used by
// createPresenceComponent() to animate elements.
//
// This test suite ensures that the component works correctly in tests using that environment.

describe('createPresenceComponent (node)', () => {
it('renders a child component when "visible" is "true"', () => {
const html = renderToStaticMarkup(
<TestPresence visible>
<div data-tid="child" />
</TestPresence>,
);

expect(html).toMatchInlineSnapshot(`"<div data-tid=\\"child\\"></div>"`);
});

it('does not render a child component when "visible" is "false" & "unmountOnExit"', () => {
const html = renderToStaticMarkup(
<TestPresence unmountOnExit visible={false}>
<div data-tid="child" />
</TestPresence>,
);

expect(html).toMatchInlineSnapshot(`""`);
});
});
Loading
Loading