Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert ReactElement-test from renderIntoDocument #28161

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});