@@ -14,7 +14,6 @@ let act;
14
14
let PropTypes ;
15
15
let React ;
16
16
let ReactDOMClient ;
17
- let ReactTestUtils ;
18
17
let createReactClass ;
19
18
20
19
describe ( 'create-react-class-integration' , ( ) => {
@@ -24,7 +23,6 @@ describe('create-react-class-integration', () => {
24
23
PropTypes = require ( 'prop-types' ) ;
25
24
React = require ( 'react' ) ;
26
25
ReactDOMClient = require ( 'react-dom/client' ) ;
27
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
28
26
createReactClass = require ( 'create-react-class/factory' ) (
29
27
React . Component ,
30
28
React . isValidElement ,
@@ -231,7 +229,7 @@ describe('create-react-class-integration', () => {
231
229
] ) ;
232
230
} ) ;
233
231
234
- it ( 'should support statics' , ( ) => {
232
+ it ( 'should support statics' , async ( ) => {
235
233
const Component = createReactClass ( {
236
234
statics : {
237
235
abc : 'def' ,
@@ -247,8 +245,13 @@ describe('create-react-class-integration', () => {
247
245
return < span /> ;
248
246
} ,
249
247
} ) ;
250
- let instance = < Component /> ;
251
- instance = ReactTestUtils . renderIntoDocument ( instance ) ;
248
+ const container = document . createElement ( 'div' ) ;
249
+ const root = ReactDOMClient . createRoot ( container ) ;
250
+ let instance ;
251
+ await act ( ( ) => {
252
+ root . render ( < Component ref = { current => ( instance = current ) } /> ) ;
253
+ } ) ;
254
+
252
255
expect ( instance . constructor . abc ) . toBe ( 'def' ) ;
253
256
expect ( Component . abc ) . toBe ( 'def' ) ;
254
257
expect ( instance . constructor . def ) . toBe ( 0 ) ;
@@ -261,7 +264,7 @@ describe('create-react-class-integration', () => {
261
264
expect ( Component . pqr ( ) ) . toBe ( Component ) ;
262
265
} ) ;
263
266
264
- it ( 'should work with object getInitialState() return values' , ( ) => {
267
+ it ( 'should work with object getInitialState() return values' , async ( ) => {
265
268
const Component = createReactClass ( {
266
269
getInitialState : function ( ) {
267
270
return {
@@ -272,12 +275,17 @@ describe('create-react-class-integration', () => {
272
275
return < span /> ;
273
276
} ,
274
277
} ) ;
275
- let instance = < Component /> ;
276
- instance = ReactTestUtils . renderIntoDocument ( instance ) ;
278
+ const container = document . createElement ( 'div' ) ;
279
+ const root = ReactDOMClient . createRoot ( container ) ;
280
+ let instance ;
281
+ await act ( ( ) => {
282
+ root . render ( < Component ref = { current => ( instance = current ) } /> ) ;
283
+ } ) ;
284
+
277
285
expect ( instance . state . occupation ) . toEqual ( 'clown' ) ;
278
286
} ) ;
279
287
280
- it ( 'should work with getDerivedStateFromProps() return values' , ( ) => {
288
+ it ( 'should work with getDerivedStateFromProps() return values' , async ( ) => {
281
289
const Component = createReactClass ( {
282
290
getInitialState ( ) {
283
291
return { } ;
@@ -289,8 +297,12 @@ describe('create-react-class-integration', () => {
289
297
Component . getDerivedStateFromProps = ( ) => {
290
298
return { occupation : 'clown' } ;
291
299
} ;
292
- let instance = < Component /> ;
293
- instance = ReactTestUtils . renderIntoDocument ( instance ) ;
300
+ let instance ;
301
+ const container = document . createElement ( 'div' ) ;
302
+ const root = ReactDOMClient . createRoot ( container ) ;
303
+ await act ( ( ) => {
304
+ root . render ( < Component ref = { current => ( instance = current ) } /> ) ;
305
+ } ) ;
294
306
expect ( instance . state . occupation ) . toEqual ( 'clown' ) ;
295
307
} ) ;
296
308
@@ -328,8 +340,9 @@ describe('create-react-class-integration', () => {
328
340
expect ( container . firstChild . className ) . toBe ( 'foo' ) ;
329
341
} ) ;
330
342
331
- it ( 'should throw with non-object getInitialState() return values' , ( ) => {
332
- [ [ 'an array' ] , 'a string' , 1234 ] . forEach ( function ( state ) {
343
+ it ( 'should throw with non-object getInitialState() return values' , async ( ) => {
344
+ // eslint-disable-next-line no-for-of-loops/no-for-of-loops
345
+ for ( const state of [ [ 'an array' ] , 'a string' , 1234 ] ) {
333
346
const Component = createReactClass ( {
334
347
getInitialState : function ( ) {
335
348
return state ;
@@ -338,16 +351,19 @@ describe('create-react-class-integration', () => {
338
351
return < span /> ;
339
352
} ,
340
353
} ) ;
341
- let instance = < Component /> ;
342
- expect ( function ( ) {
343
- instance = ReactTestUtils . renderIntoDocument ( instance ) ;
344
- } ) . toThrowError (
354
+ const container = document . createElement ( 'div' ) ;
355
+ const root = ReactDOMClient . createRoot ( container ) ;
356
+ await expect (
357
+ act ( ( ) => {
358
+ root . render ( < Component /> ) ;
359
+ } ) ,
360
+ ) . rejects . toThrowError (
345
361
'Component.getInitialState(): must return an object or null' ,
346
362
) ;
347
- } ) ;
363
+ }
348
364
} ) ;
349
365
350
- it ( 'should work with a null getInitialState() return value' , ( ) => {
366
+ it ( 'should work with a null getInitialState() return value' , async ( ) => {
351
367
const Component = createReactClass ( {
352
368
getInitialState : function ( ) {
353
369
return null ;
@@ -356,9 +372,13 @@ describe('create-react-class-integration', () => {
356
372
return < span /> ;
357
373
} ,
358
374
} ) ;
359
- expect ( ( ) =>
360
- ReactTestUtils . renderIntoDocument ( < Component /> ) ,
361
- ) . not . toThrow ( ) ;
375
+ const container = document . createElement ( 'div' ) ;
376
+ const root = ReactDOMClient . createRoot ( container ) ;
377
+ await expect (
378
+ act ( ( ) => {
379
+ root . render ( < Component /> ) ;
380
+ } ) ,
381
+ ) . resolves . not . toThrow ( ) ;
362
382
} ) ;
363
383
364
384
it ( 'should throw when using legacy factories' , ( ) => {
@@ -375,7 +395,7 @@ describe('create-react-class-integration', () => {
375
395
) ;
376
396
} ) ;
377
397
378
- it ( 'replaceState and callback works' , ( ) => {
398
+ it ( 'replaceState and callback works' , async ( ) => {
379
399
const ops = [ ] ;
380
400
const Component = createReactClass ( {
381
401
getInitialState ( ) {
@@ -387,10 +407,19 @@ describe('create-react-class-integration', () => {
387
407
} ,
388
408
} ) ;
389
409
390
- const instance = ReactTestUtils . renderIntoDocument ( < Component /> ) ;
391
- instance . replaceState ( { step : 1 } , ( ) => {
392
- ops . push ( 'Callback: ' + instance . state . step ) ;
410
+ const container = document . createElement ( 'div' ) ;
411
+ const root = ReactDOMClient . createRoot ( container ) ;
412
+ let instance ;
413
+ await act ( ( ) => {
414
+ root . render ( < Component ref = { current => ( instance = current ) } /> ) ;
415
+ } ) ;
416
+
417
+ await act ( ( ) => {
418
+ instance . replaceState ( { step : 1 } , ( ) => {
419
+ ops . push ( 'Callback: ' + instance . state . step ) ;
420
+ } ) ;
393
421
} ) ;
422
+
394
423
expect ( ops ) . toEqual ( [ 'Render: 0' , 'Render: 1' , 'Callback: 1' ] ) ;
395
424
} ) ;
396
425
0 commit comments