-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor useMediaQuery with useSyncExternalStore (#48973)
* Compose: Refactor useMediaQuery with useSyncExternalStore * Update tests * Return false for the server * Don't create MediaQueryList from scratch to get value * Use preferred syntax * Use the 'mock-match-media' package * Feedback
- Loading branch information
Showing
4 changed files
with
78 additions
and
107 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
110 changes: 24 additions & 86 deletions
110
packages/compose/src/hooks/use-media-query/test/index.js
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 |
---|---|---|
@@ -1,136 +1,74 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, act } from '@testing-library/react'; | ||
import { act, render } from '@testing-library/react'; | ||
import { matchMedia, setMedia, cleanup } from 'mock-match-media'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useMediaQuery from '../'; | ||
|
||
describe( 'useMediaQuery', () => { | ||
let addListener, removeListener; | ||
const TestComponent = ( { query } ) => { | ||
const queryResult = useMediaQuery( query ); | ||
return `useMediaQuery: ${ queryResult }`; | ||
}; | ||
|
||
describe( 'useMediaQuery', () => { | ||
beforeAll( () => { | ||
jest.spyOn( global, 'matchMedia' ); | ||
|
||
addListener = jest.fn(); | ||
removeListener = jest.fn(); | ||
window.matchMedia = matchMedia; | ||
} ); | ||
|
||
afterEach( () => { | ||
global.matchMedia.mockClear(); | ||
addListener.mockClear(); | ||
removeListener.mockClear(); | ||
beforeEach( () => { | ||
setMedia( { | ||
width: '960px', | ||
} ); | ||
} ); | ||
|
||
afterAll( () => { | ||
global.matchMedia.mockRestore(); | ||
afterEach( () => { | ||
cleanup(); | ||
} ); | ||
|
||
const TestComponent = ( { query } ) => { | ||
const queryResult = useMediaQuery( query ); | ||
return `useMediaQuery: ${ queryResult }`; | ||
}; | ||
|
||
it( 'should return true when query matches', async () => { | ||
global.matchMedia.mockReturnValue( { | ||
addListener, | ||
removeListener, | ||
matches: true, | ||
} ); | ||
|
||
const { container, unmount } = render( | ||
const { container } = render( | ||
<TestComponent query="(min-width: 782px)" /> | ||
); | ||
|
||
expect( container ).toHaveTextContent( 'useMediaQuery: true' ); | ||
|
||
unmount(); | ||
|
||
expect( removeListener ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should correctly update the value when the query evaluation matches', async () => { | ||
// First render. | ||
global.matchMedia.mockReturnValueOnce( { | ||
addListener, | ||
removeListener, | ||
matches: true, | ||
} ); | ||
// The query within useEffect. | ||
global.matchMedia.mockReturnValueOnce( { | ||
addListener, | ||
removeListener, | ||
matches: true, | ||
} ); | ||
global.matchMedia.mockReturnValueOnce( { | ||
addListener, | ||
removeListener, | ||
matches: true, | ||
} ); | ||
global.matchMedia.mockReturnValueOnce( { | ||
addListener, | ||
removeListener, | ||
matches: false, | ||
} ); | ||
|
||
const { container, unmount } = render( | ||
const { container } = render( | ||
<TestComponent query="(min-width: 782px)" /> | ||
); | ||
|
||
expect( container ).toHaveTextContent( 'useMediaQuery: true' ); | ||
|
||
let updateMatchFunction; | ||
await act( async () => { | ||
updateMatchFunction = addListener.mock.calls[ 0 ][ 0 ]; | ||
updateMatchFunction(); | ||
act( () => { | ||
setMedia( { | ||
width: '600px', | ||
} ); | ||
} ); | ||
|
||
expect( container ).toHaveTextContent( 'useMediaQuery: false' ); | ||
|
||
unmount(); | ||
|
||
expect( removeListener ).toHaveBeenCalledWith( updateMatchFunction ); | ||
} ); | ||
|
||
it( 'should return false when the query does not matches', async () => { | ||
global.matchMedia.mockReturnValue( { | ||
addListener, | ||
removeListener, | ||
matches: false, | ||
} ); | ||
|
||
const { container, unmount } = render( | ||
<TestComponent query="(min-width: 782px)" /> | ||
const { container } = render( | ||
<TestComponent query="(max-width: 782px)" /> | ||
); | ||
|
||
expect( container ).toHaveTextContent( 'useMediaQuery: false' ); | ||
|
||
unmount(); | ||
|
||
expect( removeListener ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should not call matchMedia if a query is not passed', async () => { | ||
global.matchMedia.mockReturnValue( { | ||
addListener, | ||
removeListener, | ||
matches: false, | ||
} ); | ||
|
||
const { container, rerender, unmount } = render( <TestComponent /> ); | ||
it( 'should return false when a query is not passed', async () => { | ||
const { container, rerender } = render( <TestComponent /> ); | ||
|
||
// Query will be case to a boolean to simplify the return type. | ||
expect( container ).toHaveTextContent( 'useMediaQuery: false' ); | ||
|
||
rerender( <TestComponent query={ false } /> ); | ||
|
||
expect( container ).toHaveTextContent( 'useMediaQuery: false' ); | ||
|
||
unmount(); | ||
expect( global.matchMedia ).not.toHaveBeenCalled(); | ||
expect( addListener ).not.toHaveBeenCalled(); | ||
expect( removeListener ).not.toHaveBeenCalled(); | ||
} ); | ||
} ); |
8ac3b00
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 8ac3b00.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4434495818
📝 Reported issues:
specs/editor/plugins/custom-post-types.test.js
/test/e2e/specs/site-editor/template-part.spec.js