Skip to content

Commit

Permalink
Compose: Refactor useMediaQuery tests to RTL (#44912)
Browse files Browse the repository at this point in the history
* Compose: Refactor useMediaQuery tests to RTL

* Remove duplicate test
  • Loading branch information
Mamaduka authored Oct 13, 2022
1 parent 9dc7f9d commit fc081cc
Showing 1 changed file with 33 additions and 53 deletions.
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 () => {
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

0 comments on commit fc081cc

Please sign in to comment.