@@ -43,29 +43,41 @@ describe("useDimensions", () => {
43
43
const { result } = renderHelper ( ) ;
44
44
expect ( observe ) . not . toHaveBeenCalled ( ) ;
45
45
46
- result . current . observe ( ) ;
46
+ act ( ( ) => {
47
+ result . current . observe ( ) ;
48
+ } ) ;
47
49
expect ( observe ) . not . toHaveBeenCalled ( ) ;
48
50
} ) ;
49
51
50
52
it ( "should return workable observe method" , ( ) => {
51
53
const { result } = renderHelper ( ) ;
52
- result . current . observe ( target ) ;
54
+ act ( ( ) => {
55
+ result . current . observe ( target ) ;
56
+ } ) ;
53
57
expect ( disconnect ) . toHaveBeenCalledTimes ( 1 ) ;
54
58
expect ( observe ) . toHaveBeenCalledTimes ( 1 ) ;
55
59
56
- result . current . observe ( ) ;
60
+ act ( ( ) => {
61
+ result . current . observe ( ) ;
62
+ } ) ;
57
63
expect ( observe ) . toHaveBeenCalledTimes ( 2 ) ;
58
64
} ) ;
59
65
60
66
it ( "should not observe repeatedly" , ( ) => {
61
67
const { result } = renderHelper ( ) ;
62
- result . current . observe ( target ) ;
68
+ act ( ( ) => {
69
+ result . current . observe ( target ) ;
70
+ } ) ;
63
71
expect ( disconnect ) . toHaveBeenCalledTimes ( 1 ) ;
64
72
65
- result . current . observe ( target ) ;
73
+ act ( ( ) => {
74
+ result . current . observe ( target ) ;
75
+ } ) ;
66
76
expect ( disconnect ) . toHaveBeenCalledTimes ( 1 ) ;
67
77
68
- result . current . observe ( document . createElement ( "div" ) ) ;
78
+ act ( ( ) => {
79
+ result . current . observe ( document . createElement ( "div" ) ) ;
80
+ } ) ;
69
81
expect ( disconnect ) . toHaveBeenCalledTimes ( 2 ) ;
70
82
} ) ;
71
83
@@ -77,7 +89,9 @@ describe("useDimensions", () => {
77
89
78
90
it ( "should return width and height correctly" , ( ) => {
79
91
const { result } = renderHelper ( ) ;
80
- result . current . observe ( target ) ;
92
+ act ( ( ) => {
93
+ result . current . observe ( target ) ;
94
+ } ) ;
81
95
82
96
expect ( result . current . width ) . toBe ( 0 ) ;
83
97
expect ( result . current . height ) . toBe ( 0 ) ;
@@ -96,7 +110,9 @@ describe("useDimensions", () => {
96
110
it ( "should use border-box size" , ( ) => {
97
111
console . warn = jest . fn ( ) ;
98
112
let { result } = renderHelper ( { useBorderBoxSize : true } ) ;
99
- result . current . observe ( target ) ;
113
+ act ( ( ) => {
114
+ result . current . observe ( target ) ;
115
+ } ) ;
100
116
const contentBoxSize = { blockSize : 100 , inlineSize : 100 } ;
101
117
act ( ( ) => {
102
118
triggerObserverCb ( { contentBoxSize } ) ;
@@ -109,7 +125,9 @@ describe("useDimensions", () => {
109
125
110
126
console . warn = jest . fn ( ) ;
111
127
result = renderHelper ( { useBorderBoxSize : true } ) . result ;
112
- result . current . observe ( target ) ;
128
+ act ( ( ) => {
129
+ result . current . observe ( target ) ;
130
+ } ) ;
113
131
const borderBoxSize = { blockSize : 110 , inlineSize : 110 } ;
114
132
act ( ( ) => triggerObserverCb ( { contentBoxSize, borderBoxSize } ) ) ;
115
133
expect ( console . warn ) . not . toHaveBeenCalledWith ( borderBoxWarn ) ;
@@ -119,18 +137,24 @@ describe("useDimensions", () => {
119
137
120
138
it ( "should return currentBreakpoint correctly" , ( ) => {
121
139
let { result } = renderHelper ( ) ;
122
- result . current . observe ( target ) ;
140
+ act ( ( ) => {
141
+ result . current . observe ( target ) ;
142
+ } ) ;
123
143
expect ( result . current . currentBreakpoint ) . toBe ( "" ) ;
124
144
125
145
result = renderHelper ( { breakpoints : { T1 : 100 } } ) . result ;
126
- result . current . observe ( target ) ;
146
+ act ( ( ) => {
147
+ result . current . observe ( target ) ;
148
+ } ) ;
127
149
act ( ( ) => triggerObserverCb ( { contentRect : { width : 0 } } ) ) ;
128
150
expect ( result . current . currentBreakpoint ) . toBe ( "" ) ;
129
151
act ( ( ) => triggerObserverCb ( { contentRect : { width : 99 } } ) ) ;
130
152
expect ( result . current . currentBreakpoint ) . toBe ( "" ) ;
131
153
132
154
result = renderHelper ( { breakpoints : { T0 : 0 , T1 : 100 } } ) . result ;
133
- result . current . observe ( target ) ;
155
+ act ( ( ) => {
156
+ result . current . observe ( target ) ;
157
+ } ) ;
134
158
act ( ( ) => triggerObserverCb ( { contentRect : { width : 0 } } ) ) ;
135
159
expect ( result . current . currentBreakpoint ) . toBe ( "T0" ) ;
136
160
act ( ( ) => triggerObserverCb ( { contentRect : { width : 99 } } ) ) ;
@@ -144,7 +168,9 @@ describe("useDimensions", () => {
144
168
145
169
it ( "should return entry correctly" , ( ) => {
146
170
const { result } = renderHelper ( ) ;
147
- result . current . observe ( target ) ;
171
+ act ( ( ) => {
172
+ result . current . observe ( target ) ;
173
+ } ) ;
148
174
expect ( result . current . entry ) . toBeUndefined ( ) ;
149
175
150
176
const e = { contentRect : { width : 100 , height : 100 } } ;
@@ -161,7 +187,9 @@ describe("useDimensions", () => {
161
187
it ( "should trigger onResize without breakpoints" , ( ) => {
162
188
const onResize = jest . fn ( ) ;
163
189
const { result } = renderHelper ( { onResize } ) ;
164
- result . current . observe ( target ) ;
190
+ act ( ( ) => {
191
+ result . current . observe ( target ) ;
192
+ } ) ;
165
193
const contentRect = { width : 100 , height : 100 } ;
166
194
act ( ( ) => triggerObserverCb ( { contentRect } ) ) ;
167
195
expect ( onResize ) . toHaveBeenCalledWith ( {
@@ -180,7 +208,9 @@ describe("useDimensions", () => {
180
208
breakpoints : { T0 : 0 , T1 : 100 } ,
181
209
onResize,
182
210
} ) ;
183
- result . current . observe ( target ) ;
211
+ act ( ( ) => {
212
+ result . current . observe ( target ) ;
213
+ } ) ;
184
214
const contentRect = { width : 50 , height : 100 } ;
185
215
act ( ( ) => {
186
216
triggerObserverCb ( { contentRect } ) ;
@@ -202,7 +232,9 @@ describe("useDimensions", () => {
202
232
breakpoints : { T0 : 100 , T1 : 200 } ,
203
233
updateOnBreakpointChange : true ,
204
234
} ) ;
205
- result . current . observe ( target ) ;
235
+ act ( ( ) => {
236
+ result . current . observe ( target ) ;
237
+ } ) ;
206
238
act ( ( ) => triggerObserverCb ( { contentRect : { width : 50 } } ) ) ;
207
239
expect ( result . current . width ) . toBe ( 0 ) ;
208
240
act ( ( ) => triggerObserverCb ( { contentRect : { width : 100 } } ) ) ;
@@ -211,9 +243,11 @@ describe("useDimensions", () => {
211
243
212
244
it ( "should update state conditionally" , ( ) => {
213
245
const { result } = renderHelper ( {
214
- shouldUpdate : ( { width } ) => width > 300 ,
246
+ shouldUpdate : ( { width } ) => ( width ?? 0 ) > 300 ,
247
+ } ) ;
248
+ act ( ( ) => {
249
+ result . current . observe ( target ) ;
215
250
} ) ;
216
- result . current . observe ( target ) ;
217
251
act ( ( ) => triggerObserverCb ( { contentRect : { width : 100 } } ) ) ;
218
252
expect ( result . current . width ) . toBe ( 0 ) ;
219
253
act ( ( ) => triggerObserverCb ( { contentRect : { width : 400 } } ) ) ;
@@ -226,16 +260,20 @@ describe("useDimensions", () => {
226
260
let { result } = renderHelper ( {
227
261
updateOnBreakpointChange : true ,
228
262
} ) ;
229
- result . current . observe ( target ) ;
263
+ act ( ( ) => {
264
+ result . current . observe ( target ) ;
265
+ } ) ;
230
266
act ( ( ) => triggerObserverCb ( { contentRect : { width : 50 } } ) ) ;
231
267
expect ( result . current . width ) . toBe ( 50 ) ;
232
268
233
269
result = renderHelper ( {
234
270
breakpoints : { T0 : 100 , T1 : 200 } ,
235
271
updateOnBreakpointChange : true ,
236
- shouldUpdate : ( { width } ) => width > 300 ,
272
+ shouldUpdate : ( { width } ) => ( width ?? 0 ) > 300 ,
237
273
} ) . result ;
238
- result . current . observe ( target ) ;
274
+ act ( ( ) => {
275
+ result . current . observe ( target ) ;
276
+ } ) ;
239
277
act ( ( ) => triggerObserverCb ( { contentRect : { width : 100 } } ) ) ;
240
278
expect ( result . current . width ) . toBe ( 0 ) ;
241
279
act ( ( ) => triggerObserverCb ( { contentRect : { width : 400 } } ) ) ;
@@ -271,7 +309,9 @@ describe("useDimensions", () => {
271
309
// @ts -ignore
272
310
delete global . ResizeObserverEntry ;
273
311
const { result } = renderHelper ( { polyfill : mockResizeObserver } ) ;
274
- result . current . observe ( target ) ;
312
+ act ( ( ) => {
313
+ result . current . observe ( target ) ;
314
+ } ) ;
275
315
expect ( observe ) . toHaveBeenCalledTimes ( 1 ) ;
276
316
} ) ;
277
317
} ) ;
0 commit comments