Skip to content

Commit

Permalink
convert ReactElement-test from renderIntoDocument (#28161)
Browse files Browse the repository at this point in the history
## Summary

refactors ReactElement-test to use `createRoot` instead of
`renderIntoDocument`, which uses `ReactDOM.render` under the hood

## How did you test this change?
`yarn test ReactElement`
  • Loading branch information
noahlemen authored and gaearon committed Feb 3, 2024
1 parent 3af70fe commit 87e735a
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions packages/react/src/__tests__/ReactElement-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ let act;

let React;
let ReactDOMClient;
let ReactTestUtils;

describe('ReactElement', () => {
let ComponentClass;
Expand All @@ -25,7 +24,6 @@ describe('ReactElement', () => {

React = require('react');
ReactDOMClient = require('react-dom/client');
ReactTestUtils = require('react-dom/test-utils');
// NOTE: We're explicitly not using JSX here. This is intended to test
// classic JS without JSX.
ComponentClass = class extends React.Component {
Expand Down Expand Up @@ -223,19 +221,21 @@ describe('ReactElement', () => {
expect(element.props).toEqual({foo: '56'});
});

it('preserves the owner on the element', () => {
it('preserves the owner on the element', async () => {
let element;
let instance;

class Wrapper extends React.Component {
componentDidMount() {
instance = this;
}
render() {
element = React.createElement(ComponentClass);
return element;
}
}

const instance = ReactTestUtils.renderIntoDocument(
React.createElement(Wrapper),
);
const root = ReactDOMClient.createRoot(document.createElement('div'));
await act(() => root.render(React.createElement(Wrapper)));
expect(element._owner.stateNode).toBe(instance);
});

Expand Down Expand Up @@ -327,23 +327,28 @@ describe('ReactElement', () => {

// NOTE: We're explicitly not using JSX here. This is intended to test
// classic JS without JSX.
it('should normalize props with default values', () => {
it('should normalize props with default values', async () => {
let instance;
class Component extends React.Component {
componentDidMount() {
instance = this;
}
render() {
return React.createElement('span', null, this.props.prop);
}
}
Component.defaultProps = {prop: 'testKey'};

const instance = ReactTestUtils.renderIntoDocument(
React.createElement(Component),
);
const root = ReactDOMClient.createRoot(document.createElement('div'));
await act(() => {
root.render(React.createElement(Component));
});
expect(instance.props.prop).toBe('testKey');

const inst2 = ReactTestUtils.renderIntoDocument(
React.createElement(Component, {prop: null}),
);
expect(inst2.props.prop).toBe(null);
await act(() => {
root.render(React.createElement(Component, {prop: null}));
});
expect(instance.props.prop).toBe(null);
});

it('throws when changing a prop (in dev) after element creation', async () => {
Expand Down Expand Up @@ -410,13 +415,20 @@ describe('ReactElement', () => {
}
});

it('does not warn for NaN props', () => {
it('does not warn for NaN props', async () => {
let test;
class Test extends React.Component {
componentDidMount() {
test = this;
}
render() {
return <div />;
}
}
const test = ReactTestUtils.renderIntoDocument(<Test value={+undefined} />);
const root = ReactDOMClient.createRoot(document.createElement('div'));
await act(() => {
root.render(<Test value={+undefined} />);
});
expect(test.props.value).toBeNaN();
});
});

0 comments on commit 87e735a

Please sign in to comment.