Skip to content

Commit fc081cc

Browse files
authored
Compose: Refactor useMediaQuery tests to RTL (#44912)
* Compose: Refactor useMediaQuery tests to RTL * Remove duplicate test
1 parent 9dc7f9d commit fc081cc

File tree

1 file changed

+33
-53
lines changed
  • packages/compose/src/hooks/use-media-query/test

1 file changed

+33
-53
lines changed

packages/compose/src/hooks/use-media-query/test/index.js

+33-53
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* External dependencies
33
*/
4-
import { create, act } from 'react-test-renderer';
4+
import { render, act } from '@testing-library/react';
55

66
/**
77
* Internal dependencies
@@ -33,36 +33,21 @@ describe( 'useMediaQuery', () => {
3333
return `useMediaQuery: ${ queryResult }`;
3434
};
3535

36-
it( 'should return true when query matches on the first render', async () => {
37-
global.matchMedia.mockReturnValue( {
38-
addListener,
39-
removeListener,
40-
matches: true,
41-
} );
42-
43-
const root = create( <TestComponent query="(min-width: 782px)" /> );
44-
45-
expect( root.toJSON() ).toBe( 'useMediaQuery: true' );
46-
} );
47-
4836
it( 'should return true when query matches', async () => {
4937
global.matchMedia.mockReturnValue( {
5038
addListener,
5139
removeListener,
5240
matches: true,
5341
} );
5442

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

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

61-
expect( root.toJSON() ).toBe( 'useMediaQuery: true' );
49+
unmount();
6250

63-
await act( async () => {
64-
root.unmount();
65-
} );
6651
expect( removeListener ).toHaveBeenCalled();
6752
} );
6853

@@ -90,24 +75,23 @@ describe( 'useMediaQuery', () => {
9075
matches: false,
9176
} );
9277

93-
let root, updateMatchFunction;
94-
await act( async () => {
95-
root = create( <TestComponent query="(min-width: 782px)" /> );
96-
} );
97-
expect( root.toJSON() ).toBe( 'useMediaQuery: true' );
78+
const { container, unmount } = render(
79+
<TestComponent query="(min-width: 782px)" />
80+
);
81+
82+
expect( container ).toHaveTextContent( 'useMediaQuery: true' );
9883

84+
let updateMatchFunction;
9985
await act( async () => {
10086
updateMatchFunction = addListener.mock.calls[ 0 ][ 0 ];
10187
updateMatchFunction();
10288
} );
103-
expect( root.toJSON() ).toBe( 'useMediaQuery: false' );
10489

105-
await act( async () => {
106-
root.unmount();
107-
} );
108-
expect( removeListener.mock.calls ).toEqual( [
109-
[ updateMatchFunction ],
110-
] );
90+
expect( container ).toHaveTextContent( 'useMediaQuery: false' );
91+
92+
unmount();
93+
94+
expect( removeListener ).toHaveBeenCalledWith( updateMatchFunction );
11195
} );
11296

11397
it( 'should return false when the query does not matches', async () => {
@@ -116,15 +100,15 @@ describe( 'useMediaQuery', () => {
116100
removeListener,
117101
matches: false,
118102
} );
119-
let root;
120-
await act( async () => {
121-
root = create( <TestComponent query="(min-width: 782px)" /> );
122-
} );
123-
expect( root.toJSON() ).toBe( 'useMediaQuery: false' );
124103

125-
await act( async () => {
126-
root.unmount();
127-
} );
104+
const { container, unmount } = render(
105+
<TestComponent query="(min-width: 782px)" />
106+
);
107+
108+
expect( container ).toHaveTextContent( 'useMediaQuery: false' );
109+
110+
unmount();
111+
128112
expect( removeListener ).toHaveBeenCalled();
129113
} );
130114

@@ -134,21 +118,17 @@ describe( 'useMediaQuery', () => {
134118
removeListener,
135119
matches: false,
136120
} );
137-
let root;
138-
await act( async () => {
139-
root = create( <TestComponent /> );
140-
} );
121+
122+
const { container, rerender, unmount } = render( <TestComponent /> );
123+
141124
// Query will be case to a boolean to simplify the return type.
142-
expect( root.toJSON() ).toBe( 'useMediaQuery: false' );
125+
expect( container ).toHaveTextContent( 'useMediaQuery: false' );
143126

144-
await act( async () => {
145-
root.update( <TestComponent query={ false } /> );
146-
} );
147-
expect( root.toJSON() ).toBe( 'useMediaQuery: false' );
127+
rerender( <TestComponent query={ false } /> );
148128

149-
await act( async () => {
150-
root.unmount();
151-
} );
129+
expect( container ).toHaveTextContent( 'useMediaQuery: false' );
130+
131+
unmount();
152132
expect( global.matchMedia ).not.toHaveBeenCalled();
153133
expect( addListener ).not.toHaveBeenCalled();
154134
expect( removeListener ).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)