Skip to content

Commit

Permalink
Remove ReactTestUtils from ReactComponentLifeCycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Silbermann committed Feb 18, 2024
1 parent c1fd2a9 commit 256c42f
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ let act;
let React;
let ReactDOM;
let ReactDOMClient;
let ReactTestUtils;
let PropTypes;

const clone = function (o) {
Expand Down Expand Up @@ -97,7 +96,6 @@ describe('ReactComponentLifeCycle', () => {
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactTestUtils = require('react-dom/test-utils');
PropTypes = require('prop-types');
});

Expand Down Expand Up @@ -189,7 +187,7 @@ describe('ReactComponentLifeCycle', () => {

// You could assign state here, but not access members of it, unless you
// had provided a getInitialState method.
it('throws when accessing state in componentWillMount', () => {
it('throws when accessing state in componentWillMount', async () => {
class StatefulComponent extends React.Component {
UNSAFE_componentWillMount() {
void this.state.yada;
Expand All @@ -200,10 +198,13 @@ describe('ReactComponentLifeCycle', () => {
}
}

let instance = <StatefulComponent />;
expect(function () {
instance = ReactTestUtils.renderIntoDocument(instance);
}).toThrow();
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await expect(
act(() => {
root.render(<StatefulComponent />);
}),
).rejects.toThrow();
});

it('should allow update state inside of componentWillMount', () => {
Expand All @@ -217,9 +218,13 @@ describe('ReactComponentLifeCycle', () => {
}
}

let instance = <StatefulComponent />;
expect(function () {
instance = ReactTestUtils.renderIntoDocument(instance);
expect(async function () {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);

await act(() => {
root.render(<StatefulComponent />);
});
}).not.toThrow();
});

Expand Down Expand Up @@ -557,7 +562,7 @@ describe('ReactComponentLifeCycle', () => {
});
});

it('should allow state updates in componentDidMount', () => {
it('should allow state updates in componentDidMount', async () => {
/**
* calls setState in an componentDidMount.
*/
Expand All @@ -575,13 +580,19 @@ describe('ReactComponentLifeCycle', () => {
}
}

let instance = (
<SetStateInComponentDidMount
valueToUseInitially="hello"
valueToUseInOnDOMReady="goodbye"
/>
);
instance = ReactTestUtils.renderIntoDocument(instance);
let instance;
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<SetStateInComponentDidMount
ref={current => (instance = current)}
valueToUseInitially="hello"
valueToUseInOnDOMReady="goodbye"
/>,
);
});

expect(instance.state.stateField).toBe('goodbye');
});

Expand Down

0 comments on commit 256c42f

Please sign in to comment.