Skip to content

Commit 84a1a0c

Browse files
eps1longaearon
authored andcommitted
Convert createReactClassIntegration to createRoot (#28196)
1 parent 348cc74 commit 84a1a0c

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

packages/react/src/__tests__/createReactClassIntegration-test.js

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ let act;
1414
let PropTypes;
1515
let React;
1616
let ReactDOMClient;
17-
let ReactTestUtils;
1817
let createReactClass;
1918

2019
describe('create-react-class-integration', () => {
@@ -24,7 +23,6 @@ describe('create-react-class-integration', () => {
2423
PropTypes = require('prop-types');
2524
React = require('react');
2625
ReactDOMClient = require('react-dom/client');
27-
ReactTestUtils = require('react-dom/test-utils');
2826
createReactClass = require('create-react-class/factory')(
2927
React.Component,
3028
React.isValidElement,
@@ -231,7 +229,7 @@ describe('create-react-class-integration', () => {
231229
]);
232230
});
233231

234-
it('should support statics', () => {
232+
it('should support statics', async () => {
235233
const Component = createReactClass({
236234
statics: {
237235
abc: 'def',
@@ -247,8 +245,13 @@ describe('create-react-class-integration', () => {
247245
return <span />;
248246
},
249247
});
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+
252255
expect(instance.constructor.abc).toBe('def');
253256
expect(Component.abc).toBe('def');
254257
expect(instance.constructor.def).toBe(0);
@@ -261,7 +264,7 @@ describe('create-react-class-integration', () => {
261264
expect(Component.pqr()).toBe(Component);
262265
});
263266

264-
it('should work with object getInitialState() return values', () => {
267+
it('should work with object getInitialState() return values', async () => {
265268
const Component = createReactClass({
266269
getInitialState: function () {
267270
return {
@@ -272,12 +275,17 @@ describe('create-react-class-integration', () => {
272275
return <span />;
273276
},
274277
});
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+
277285
expect(instance.state.occupation).toEqual('clown');
278286
});
279287

280-
it('should work with getDerivedStateFromProps() return values', () => {
288+
it('should work with getDerivedStateFromProps() return values', async () => {
281289
const Component = createReactClass({
282290
getInitialState() {
283291
return {};
@@ -289,8 +297,12 @@ describe('create-react-class-integration', () => {
289297
Component.getDerivedStateFromProps = () => {
290298
return {occupation: 'clown'};
291299
};
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+
});
294306
expect(instance.state.occupation).toEqual('clown');
295307
});
296308

@@ -328,8 +340,9 @@ describe('create-react-class-integration', () => {
328340
expect(container.firstChild.className).toBe('foo');
329341
});
330342

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]) {
333346
const Component = createReactClass({
334347
getInitialState: function () {
335348
return state;
@@ -338,16 +351,19 @@ describe('create-react-class-integration', () => {
338351
return <span />;
339352
},
340353
});
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(
345361
'Component.getInitialState(): must return an object or null',
346362
);
347-
});
363+
}
348364
});
349365

350-
it('should work with a null getInitialState() return value', () => {
366+
it('should work with a null getInitialState() return value', async () => {
351367
const Component = createReactClass({
352368
getInitialState: function () {
353369
return null;
@@ -356,9 +372,13 @@ describe('create-react-class-integration', () => {
356372
return <span />;
357373
},
358374
});
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();
362382
});
363383

364384
it('should throw when using legacy factories', () => {
@@ -375,7 +395,7 @@ describe('create-react-class-integration', () => {
375395
);
376396
});
377397

378-
it('replaceState and callback works', () => {
398+
it('replaceState and callback works', async () => {
379399
const ops = [];
380400
const Component = createReactClass({
381401
getInitialState() {
@@ -387,10 +407,19 @@ describe('create-react-class-integration', () => {
387407
},
388408
});
389409

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+
});
393421
});
422+
394423
expect(ops).toEqual(['Render: 0', 'Render: 1', 'Callback: 1']);
395424
});
396425

0 commit comments

Comments
 (0)