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: handle cases when Animation.persist() does not exist #33282

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕵🏾‍♀️ visual regressions to review in the fluentuiv9 Visual Regression Report

Avatar Converged 2 screenshots
Image Name Diff(in Pixels) Image Type
Avatar Converged.badgeMask.chromium.png 6 Changed
Avatar Converged.Badge Mask RTL.chromium.png 2 Changed
Drawer 2 screenshots
Image Name Diff(in Pixels) Image Type
Drawer.Full Overlay Dark Mode.chromium.png 2561 Changed
Drawer.overlay drawer full.chromium.png 3320 Changed

"type": "patch",
"comment": "fix: handle case when Animation.persist() does not exist",
"packageName": "@fluentui/react-motion",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ function createElementMock() {
}

describe('createMotionComponent', () => {
let hasAnimation: boolean;
beforeEach(() => {
if (!global.Animation) {
hasAnimation = false;
global.Animation = {
// @ts-expect-error mock
prototype: {
persist: jest.fn(),
},
};
} else {
hasAnimation = true;
}
});

afterEach(() => {
if (!hasAnimation) {
// @ts-expect-error mock
delete global.Animation;
}
});
it('creates a motion and plays it', () => {
const TestAtom = createMotionComponent(motion);
const { animateMock, ElementMock } = createElementMock();
Expand All @@ -52,6 +73,24 @@ describe('createMotionComponent', () => {
});
});

it('creates a motion and plays it (without .persist())', () => {
// @ts-expect-error mock
delete global.Animation.prototype.persist;
const TestAtom = createMotionComponent(motion);
const { animateMock, ElementMock } = createElementMock();

render(
<TestAtom>
<ElementMock />
</TestAtom>,
);

expect(animateMock).toHaveBeenCalledWith(motion.keyframes, {
duration: 1,
fill: 'forwards',
});
});

it('supports functions as motion definitions', () => {
const fnMotion = jest.fn().mockImplementation(() => motion);
const TestAtom = createMotionComponent(fnMotion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ function createElementMock() {
}

describe('createPresenceComponent', () => {
let hasAnimation: boolean;
beforeEach(() => {
if (!global.Animation) {
hasAnimation = false;
global.Animation = {
// @ts-expect-error mock
prototype: {
persist: jest.fn(),
},
};
} else {
hasAnimation = true;
}
});

afterEach(() => {
if (!hasAnimation) {
// @ts-expect-error mock
delete global.Animation;
}
});

describe('appear', () => {
it('does not animate by default', () => {
const TestPresence = createPresenceComponent(motion);
Expand Down Expand Up @@ -68,6 +90,24 @@ describe('createPresenceComponent', () => {
expect(animateMock).toHaveBeenCalledWith(enterKeyframes, options);
});

it('animates when is "true" (without .persist())', () => {
// @ts-expect-error mock
delete window.Animation.prototype.persist;
const TestPresence = createPresenceComponent(motion);
const { animateMock, ElementMock } = createElementMock();

render(
<TestPresence appear visible>
<ElementMock />
</TestPresence>,
);

expect(animateMock).toHaveBeenCalledWith(enterKeyframes, {
...options,
duration: 1,
});
});

it('finishes motion when wrapped in motion behaviour context with skip behaviour', async () => {
const TestPresence = createPresenceComponent(motion);
const { finishMock, ElementMock } = createElementMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import * as React from 'react';
import type { AnimationHandle, AtomMotion } from '../types';

function useAnimateAtomsInSupportedEnvironment() {
// eslint-disable-next-line @nx/workspace-no-restricted-globals
const SUPPORTS_PERSIST = typeof window !== 'undefined' && typeof window.Animation?.prototype.persist === 'function';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in love with having this here. Ideally it would live in global scope because SUPPORTS_PERSIST will be constant for any given environment.

I moved it here because I could not find a good way to test it with Jest.


return React.useCallback(
(
element: HTMLElement,
Expand All @@ -19,10 +22,10 @@ function useAnimateAtomsInSupportedEnvironment() {
fill: 'forwards',

...params,
...(isReducedMotion && { duration: 1 }),
...((isReducedMotion || !SUPPORTS_PERSIST) && { duration: 1 }),
});

animation.persist();
SUPPORTS_PERSIST && animation.persist();

return animation;
});
Expand Down
Loading