12
12
let React = require ( 'react' ) ;
13
13
let ReactDOMClient = require ( 'react-dom/client' ) ;
14
14
let ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
15
- let ReactTestUtils = require ( 'react-dom/test-utils' ) ;
16
15
let act = require ( 'internal-test-utils' ) . act ;
17
16
18
17
// This is testing if string refs are deleted from `instance.refs`
@@ -26,7 +25,6 @@ describe('reactiverefs', () => {
26
25
React = require ( 'react' ) ;
27
26
ReactDOMClient = require ( 'react-dom/client' ) ;
28
27
ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
29
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
30
28
act = require ( 'internal-test-utils' ) . act ;
31
29
} ) ;
32
30
@@ -73,10 +71,7 @@ describe('reactiverefs', () => {
73
71
}
74
72
75
73
const expectClickLogsLengthToBe = function ( instance , length ) {
76
- const clickLogs = ReactTestUtils . scryRenderedDOMComponentsWithClass (
77
- instance ,
78
- 'clickLogDiv' ,
79
- ) ;
74
+ const clickLogs = instance . container . querySelectorAll ( '.clickLogDiv' ) ;
80
75
expect ( clickLogs . length ) . toBe ( length ) ;
81
76
expect ( Object . keys ( instance . refs . myCounter . refs ) . length ) . toBe ( length ) ;
82
77
} ;
@@ -101,13 +96,14 @@ describe('reactiverefs', () => {
101
96
* into a different parent.
102
97
*/
103
98
class TestRefsComponent extends React . Component {
99
+ container = null ;
104
100
doReset = ( ) => {
105
101
this . refs . myCounter . triggerReset ( ) ;
106
102
} ;
107
103
108
104
render ( ) {
109
105
return (
110
- < div >
106
+ < div ref = { current => ( this . container = current ) } >
111
107
< div ref = "resetDiv" onClick = { this . doReset } >
112
108
Reset Me By Clicking This.
113
109
</ div >
@@ -170,10 +166,8 @@ describe('reactiverefs', () => {
170
166
*/
171
167
it ( 'Should increase refs with an increase in divs' , async ( ) => {
172
168
const testRefsComponent = await renderTestRefsComponent ( ) ;
173
- const clickIncrementer = ReactTestUtils . findRenderedDOMComponentWithClass (
174
- testRefsComponent ,
175
- 'clickIncrementer' ,
176
- ) ;
169
+ const clickIncrementer =
170
+ testRefsComponent . container . querySelector ( '.clickIncrementer' ) ;
177
171
178
172
expectClickLogsLengthToBe ( testRefsComponent , 1 ) ;
179
173
@@ -202,7 +196,7 @@ describe('reactiverefs', () => {
202
196
203
197
if ( ! ReactFeatureFlags . disableModulePatternComponents ) {
204
198
describe ( 'factory components' , ( ) => {
205
- it ( 'Should correctly get the ref' , ( ) => {
199
+ it ( 'Should correctly get the ref' , async ( ) => {
206
200
function Comp ( ) {
207
201
return {
208
202
elemRef : React . createRef ( ) ,
@@ -213,9 +207,14 @@ if (!ReactFeatureFlags.disableModulePatternComponents) {
213
207
}
214
208
215
209
let inst ;
216
- expect (
217
- ( ) => ( inst = ReactTestUtils . renderIntoDocument ( < Comp /> ) ) ,
218
- ) . toErrorDev (
210
+ await expect ( async ( ) => {
211
+ const container = document . createElement ( 'div' ) ;
212
+ const root = ReactDOMClient . createRoot ( container ) ;
213
+
214
+ await act ( ( ) => {
215
+ root . render ( < Comp ref = { current => ( inst = current ) } /> ) ;
216
+ } ) ;
217
+ } ) . toErrorDev (
219
218
'Warning: The <Comp /> component appears to be a function component that returns a class instance. ' +
220
219
'Change Comp to a class that extends React.Component instead. ' +
221
220
"If you can't use a class try assigning the prototype on the function as a workaround. " +
@@ -237,10 +236,10 @@ describe('ref swapping', () => {
237
236
React = require ( 'react' ) ;
238
237
ReactDOMClient = require ( 'react-dom/client' ) ;
239
238
ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
240
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
241
239
act = require ( 'internal-test-utils' ) . act ;
242
240
243
241
RefHopsAround = class extends React . Component {
242
+ container = null ;
244
243
state = { count : 0 } ;
245
244
hopRef = React . createRef ( ) ;
246
245
divOneRef = React . createRef ( ) ;
@@ -260,7 +259,7 @@ describe('ref swapping', () => {
260
259
* points to the correct divs.
261
260
*/
262
261
return (
263
- < div >
262
+ < div ref = { current => ( this . container = current ) } >
264
263
< div
265
264
className = "first"
266
265
ref = { count % 3 === 0 ? this . hopRef : this . divOneRef }
@@ -279,32 +278,33 @@ describe('ref swapping', () => {
279
278
} ;
280
279
} ) ;
281
280
282
- it ( 'Allow refs to hop around children correctly' , ( ) => {
283
- const refHopsAround = ReactTestUtils . renderIntoDocument ( < RefHopsAround /> ) ;
281
+ it ( 'Allow refs to hop around children correctly' , async ( ) => {
282
+ const container = document . createElement ( 'div' ) ;
283
+ const root = ReactDOMClient . createRoot ( container ) ;
284
284
285
- const firstDiv = ReactTestUtils . findRenderedDOMComponentWithClass (
286
- refHopsAround ,
287
- 'first' ,
288
- ) ;
289
- const secondDiv = ReactTestUtils . findRenderedDOMComponentWithClass (
290
- refHopsAround ,
291
- 'second' ,
292
- ) ;
293
- const thirdDiv = ReactTestUtils . findRenderedDOMComponentWithClass (
294
- refHopsAround ,
295
- 'third' ,
296
- ) ;
285
+ let refHopsAround ;
286
+ await act ( ( ) => {
287
+ root . render ( < RefHopsAround ref = { current => ( refHopsAround = current ) } /> ) ;
288
+ } ) ;
289
+
290
+ const firstDiv = refHopsAround . container . querySelector ( '.first' ) ;
291
+ const secondDiv = refHopsAround . container . querySelector ( '.second' ) ;
292
+ const thirdDiv = refHopsAround . container . querySelector ( '.third' ) ;
297
293
298
294
expect ( refHopsAround . hopRef . current ) . toEqual ( firstDiv ) ;
299
295
expect ( refHopsAround . divTwoRef . current ) . toEqual ( secondDiv ) ;
300
296
expect ( refHopsAround . divThreeRef . current ) . toEqual ( thirdDiv ) ;
301
297
302
- refHopsAround . moveRef ( ) ;
298
+ await act ( ( ) => {
299
+ refHopsAround . moveRef ( ) ;
300
+ } ) ;
303
301
expect ( refHopsAround . divOneRef . current ) . toEqual ( firstDiv ) ;
304
302
expect ( refHopsAround . hopRef . current ) . toEqual ( secondDiv ) ;
305
303
expect ( refHopsAround . divThreeRef . current ) . toEqual ( thirdDiv ) ;
306
304
307
- refHopsAround . moveRef ( ) ;
305
+ await act ( ( ) => {
306
+ refHopsAround . moveRef ( ) ;
307
+ } ) ;
308
308
expect ( refHopsAround . divOneRef . current ) . toEqual ( firstDiv ) ;
309
309
expect ( refHopsAround . divTwoRef . current ) . toEqual ( secondDiv ) ;
310
310
expect ( refHopsAround . hopRef . current ) . toEqual ( thirdDiv ) ;
@@ -313,7 +313,9 @@ describe('ref swapping', () => {
313
313
* Make sure that after the third, we're back to where we started and the
314
314
* refs are completely restored.
315
315
*/
316
- refHopsAround . moveRef ( ) ;
316
+ await act ( ( ) => {
317
+ refHopsAround . moveRef ( ) ;
318
+ } ) ;
317
319
expect ( refHopsAround . hopRef . current ) . toEqual ( firstDiv ) ;
318
320
expect ( refHopsAround . divTwoRef . current ) . toEqual ( secondDiv ) ;
319
321
expect ( refHopsAround . divThreeRef . current ) . toEqual ( thirdDiv ) ;
@@ -364,15 +366,20 @@ describe('ref swapping', () => {
364
366
expect ( refCalled ) . toBe ( 1 ) ;
365
367
} ) ;
366
368
367
- it ( 'coerces numbers to strings' , ( ) => {
369
+ it ( 'coerces numbers to strings' , async ( ) => {
368
370
class A extends React . Component {
369
371
render ( ) {
370
372
return < div ref = { 1 } /> ;
371
373
}
372
374
}
373
375
let a ;
374
- expect ( ( ) => {
375
- a = ReactTestUtils . renderIntoDocument ( < A /> ) ;
376
+ await expect ( async ( ) => {
377
+ const container = document . createElement ( 'div' ) ;
378
+ const root = ReactDOMClient . createRoot ( container ) ;
379
+
380
+ await act ( ( ) => {
381
+ root . render ( < A ref = { current => ( a = current ) } /> ) ;
382
+ } ) ;
376
383
} ) . toErrorDev ( [
377
384
'Warning: Component "A" contains the string ref "1". ' +
378
385
'Support for string refs will be removed in a future major release. ' +
0 commit comments