Skip to content

Commit

Permalink
Unit tests: mock matchMedia to enforce prefers-reduce-motion (#65438)
Browse files Browse the repository at this point in the history
* Add global matchMedia mock for prefers-reduced-motion

* Update DotTip snapshots

---

Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
3 people authored Sep 18, 2024
1 parent be80309 commit f67e39b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports[`DotTip should render correctly 1`] = `
data-wp-c16t="true"
data-wp-component="Popover"
role="dialog"
style="position: absolute; top: 0px; left: 0px; opacity: 1; transform: translateX(0px) translateY(0px) translateX(0em) scale(1) translateZ(0); transform-origin: 0% 50% 0;"
style="position: absolute; top: 0px; left: 0px; transform: none;"
tabindex="-1"
>
<div
Expand Down
5 changes: 4 additions & 1 deletion test/unit/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ module.exports = {
'<rootDir>/test/unit/config/global-mocks.js',
'<rootDir>/test/unit/config/gutenberg-env.js',
],
setupFilesAfterEnv: [ '<rootDir>/test/unit/config/testing-library.js' ],
setupFilesAfterEnv: [
'<rootDir>/test/unit/config/testing-library.js',
'<rootDir>/test/unit/mocks/match-media.js',
],
testEnvironmentOptions: {
url: 'http://localhost/',
},
Expand Down
22 changes: 22 additions & 0 deletions test/unit/mocks/match-media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Mock `matchMedia` so that all animations are skipped,
// since js-dom does not fully support CSS animations.
// Example: https://github.com/jsdom/jsdom/issues/3239
const originalMatchMedia = window.matchMedia;
const mockedMatchMedia = jest.fn( ( query ) => {
if ( /prefers-reduced-motion/.test( query ) ) {
return {
...originalMatchMedia( query ),
matches: true,
};
}

return originalMatchMedia( query );
} );

beforeAll( () => {
window.matchMedia = jest.fn( mockedMatchMedia );
} );

afterAll( () => {
window.matchMedia = originalMatchMedia;
} );

0 comments on commit f67e39b

Please sign in to comment.