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

Compose: Refactor useMediaQuery tests to RTL #44912

Merged
merged 2 commits into from
Oct 13, 2022
Merged
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
86 changes: 33 additions & 53 deletions packages/compose/src/hooks/use-media-query/test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { create, act } from 'react-test-renderer';
import { render, act } from '@testing-library/react';

/**
* Internal dependencies
Expand Down Expand Up @@ -33,36 +33,21 @@ describe( 'useMediaQuery', () => {
return `useMediaQuery: ${ queryResult }`;
};

it( 'should return true when query matches on the first render', async () => {
global.matchMedia.mockReturnValue( {
addListener,
removeListener,
matches: true,
} );

const root = create( <TestComponent query="(min-width: 782px)" /> );

expect( root.toJSON() ).toBe( 'useMediaQuery: true' );
} );

it( 'should return true when query matches', async () => {
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
global.matchMedia.mockReturnValue( {
addListener,
removeListener,
matches: true,
} );

let root;
const { container, unmount } = render(
<TestComponent query="(min-width: 782px)" />
);

await act( async () => {
root = create( <TestComponent query="(min-width: 782px)" /> );
} );
expect( container ).toHaveTextContent( 'useMediaQuery: true' );

expect( root.toJSON() ).toBe( 'useMediaQuery: true' );
unmount();

await act( async () => {
root.unmount();
} );
expect( removeListener ).toHaveBeenCalled();
} );

Expand Down Expand Up @@ -90,24 +75,23 @@ describe( 'useMediaQuery', () => {
matches: false,
} );

let root, updateMatchFunction;
await act( async () => {
root = create( <TestComponent query="(min-width: 782px)" /> );
} );
expect( root.toJSON() ).toBe( 'useMediaQuery: true' );
const { container, unmount } = render(
<TestComponent query="(min-width: 782px)" />
);

expect( container ).toHaveTextContent( 'useMediaQuery: true' );

let updateMatchFunction;
await act( async () => {
updateMatchFunction = addListener.mock.calls[ 0 ][ 0 ];
updateMatchFunction();
} );
expect( root.toJSON() ).toBe( 'useMediaQuery: false' );

await act( async () => {
root.unmount();
} );
expect( removeListener.mock.calls ).toEqual( [
[ updateMatchFunction ],
] );
expect( container ).toHaveTextContent( 'useMediaQuery: false' );

unmount();

expect( removeListener ).toHaveBeenCalledWith( updateMatchFunction );
} );

it( 'should return false when the query does not matches', async () => {
Expand All @@ -116,15 +100,15 @@ describe( 'useMediaQuery', () => {
removeListener,
matches: false,
} );
let root;
await act( async () => {
root = create( <TestComponent query="(min-width: 782px)" /> );
} );
expect( root.toJSON() ).toBe( 'useMediaQuery: false' );

await act( async () => {
root.unmount();
} );
const { container, unmount } = render(
<TestComponent query="(min-width: 782px)" />
);

expect( container ).toHaveTextContent( 'useMediaQuery: false' );

unmount();

expect( removeListener ).toHaveBeenCalled();
} );

Expand All @@ -134,21 +118,17 @@ describe( 'useMediaQuery', () => {
removeListener,
matches: false,
} );
let root;
await act( async () => {
root = create( <TestComponent /> );
} );

const { container, rerender, unmount } = render( <TestComponent /> );

// Query will be case to a boolean to simplify the return type.
expect( root.toJSON() ).toBe( 'useMediaQuery: false' );
expect( container ).toHaveTextContent( 'useMediaQuery: false' );

await act( async () => {
root.update( <TestComponent query={ false } /> );
} );
expect( root.toJSON() ).toBe( 'useMediaQuery: false' );
rerender( <TestComponent query={ false } /> );

await act( async () => {
root.unmount();
} );
expect( container ).toHaveTextContent( 'useMediaQuery: false' );

unmount();
expect( global.matchMedia ).not.toHaveBeenCalled();
expect( addListener ).not.toHaveBeenCalled();
expect( removeListener ).not.toHaveBeenCalled();
Expand Down