1
1
/**
2
2
* External dependencies
3
3
*/
4
- import { create , act } from 'react-test-renderer ' ;
4
+ import { render , act } from '@testing-library/react ' ;
5
5
6
6
/**
7
7
* Internal dependencies
@@ -33,36 +33,21 @@ describe( 'useMediaQuery', () => {
33
33
return `useMediaQuery: ${ queryResult } ` ;
34
34
} ;
35
35
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
-
48
36
it ( 'should return true when query matches' , async ( ) => {
49
37
global . matchMedia . mockReturnValue ( {
50
38
addListener,
51
39
removeListener,
52
40
matches : true ,
53
41
} ) ;
54
42
55
- let root ;
43
+ const { container, unmount } = render (
44
+ < TestComponent query = "(min-width: 782px)" />
45
+ ) ;
56
46
57
- await act ( async ( ) => {
58
- root = create ( < TestComponent query = "(min-width: 782px)" /> ) ;
59
- } ) ;
47
+ expect ( container ) . toHaveTextContent ( 'useMediaQuery: true' ) ;
60
48
61
- expect ( root . toJSON ( ) ) . toBe ( 'useMediaQuery: true' ) ;
49
+ unmount ( ) ;
62
50
63
- await act ( async ( ) => {
64
- root . unmount ( ) ;
65
- } ) ;
66
51
expect ( removeListener ) . toHaveBeenCalled ( ) ;
67
52
} ) ;
68
53
@@ -90,24 +75,23 @@ describe( 'useMediaQuery', () => {
90
75
matches : false ,
91
76
} ) ;
92
77
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' ) ;
98
83
84
+ let updateMatchFunction ;
99
85
await act ( async ( ) => {
100
86
updateMatchFunction = addListener . mock . calls [ 0 ] [ 0 ] ;
101
87
updateMatchFunction ( ) ;
102
88
} ) ;
103
- expect ( root . toJSON ( ) ) . toBe ( 'useMediaQuery: false' ) ;
104
89
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 ) ;
111
95
} ) ;
112
96
113
97
it ( 'should return false when the query does not matches' , async ( ) => {
@@ -116,15 +100,15 @@ describe( 'useMediaQuery', () => {
116
100
removeListener,
117
101
matches : false ,
118
102
} ) ;
119
- let root ;
120
- await act ( async ( ) => {
121
- root = create ( < TestComponent query = "(min-width: 782px)" /> ) ;
122
- } ) ;
123
- expect ( root . toJSON ( ) ) . toBe ( 'useMediaQuery: false' ) ;
124
103
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
+
128
112
expect ( removeListener ) . toHaveBeenCalled ( ) ;
129
113
} ) ;
130
114
@@ -134,21 +118,17 @@ describe( 'useMediaQuery', () => {
134
118
removeListener,
135
119
matches : false ,
136
120
} ) ;
137
- let root ;
138
- await act ( async ( ) => {
139
- root = create ( < TestComponent /> ) ;
140
- } ) ;
121
+
122
+ const { container, rerender, unmount } = render ( < TestComponent /> ) ;
123
+
141
124
// 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' ) ;
143
126
144
- await act ( async ( ) => {
145
- root . update ( < TestComponent query = { false } /> ) ;
146
- } ) ;
147
- expect ( root . toJSON ( ) ) . toBe ( 'useMediaQuery: false' ) ;
127
+ rerender ( < TestComponent query = { false } /> ) ;
148
128
149
- await act ( async ( ) => {
150
- root . unmount ( ) ;
151
- } ) ;
129
+ expect ( container ) . toHaveTextContent ( 'useMediaQuery: false' ) ;
130
+
131
+ unmount ( ) ;
152
132
expect ( global . matchMedia ) . not . toHaveBeenCalled ( ) ;
153
133
expect ( addListener ) . not . toHaveBeenCalled ( ) ;
154
134
expect ( removeListener ) . not . toHaveBeenCalled ( ) ;
0 commit comments