From fc081cc0e339a0bb759249fd58fdfd7a9133e5b0 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 13 Oct 2022 15:52:36 +0400 Subject: [PATCH] Compose: Refactor useMediaQuery tests to RTL (#44912) * Compose: Refactor useMediaQuery tests to RTL * Remove duplicate test --- .../src/hooks/use-media-query/test/index.js | 86 +++++++------------ 1 file changed, 33 insertions(+), 53 deletions(-) diff --git a/packages/compose/src/hooks/use-media-query/test/index.js b/packages/compose/src/hooks/use-media-query/test/index.js index 0e4abff448466f..f75ba22a7b3fa1 100644 --- a/packages/compose/src/hooks/use-media-query/test/index.js +++ b/packages/compose/src/hooks/use-media-query/test/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { create, act } from 'react-test-renderer'; +import { render, act } from '@testing-library/react'; /** * Internal dependencies @@ -33,18 +33,6 @@ 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( ); - - expect( root.toJSON() ).toBe( 'useMediaQuery: true' ); - } ); - it( 'should return true when query matches', async () => { global.matchMedia.mockReturnValue( { addListener, @@ -52,17 +40,14 @@ describe( 'useMediaQuery', () => { matches: true, } ); - let root; + const { container, unmount } = render( + + ); - await act( async () => { - root = create( ); - } ); + expect( container ).toHaveTextContent( 'useMediaQuery: true' ); - expect( root.toJSON() ).toBe( 'useMediaQuery: true' ); + unmount(); - await act( async () => { - root.unmount(); - } ); expect( removeListener ).toHaveBeenCalled(); } ); @@ -90,24 +75,23 @@ describe( 'useMediaQuery', () => { matches: false, } ); - let root, updateMatchFunction; - await act( async () => { - root = create( ); - } ); - expect( root.toJSON() ).toBe( 'useMediaQuery: true' ); + const { container, unmount } = render( + + ); + + 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 () => { @@ -116,15 +100,15 @@ describe( 'useMediaQuery', () => { removeListener, matches: false, } ); - let root; - await act( async () => { - root = create( ); - } ); - expect( root.toJSON() ).toBe( 'useMediaQuery: false' ); - await act( async () => { - root.unmount(); - } ); + const { container, unmount } = render( + + ); + + expect( container ).toHaveTextContent( 'useMediaQuery: false' ); + + unmount(); + expect( removeListener ).toHaveBeenCalled(); } ); @@ -134,21 +118,17 @@ describe( 'useMediaQuery', () => { removeListener, matches: false, } ); - let root; - await act( async () => { - root = create( ); - } ); + + const { container, rerender, unmount } = render( ); + // 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( ); - } ); - expect( root.toJSON() ).toBe( 'useMediaQuery: false' ); + rerender( ); - await act( async () => { - root.unmount(); - } ); + expect( container ).toHaveTextContent( 'useMediaQuery: false' ); + + unmount(); expect( global.matchMedia ).not.toHaveBeenCalled(); expect( addListener ).not.toHaveBeenCalled(); expect( removeListener ).not.toHaveBeenCalled();