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

Remove ReactTestUtils from ReactJSXRuntime #28337

Merged
merged 1 commit into from
Feb 27, 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
60 changes: 43 additions & 17 deletions packages/react/src/__tests__/ReactJSXRuntime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
let React;
let ReactDOM;
let ReactDOMClient;
let ReactTestUtils;
let JSXRuntime;
let JSXDEVRuntime;
let act;
Expand All @@ -29,7 +28,6 @@ describe('ReactJSXRuntime', () => {
JSXDEVRuntime = require('react/jsx-dev-runtime');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactTestUtils = require('react-dom/test-utils');
act = require('internal-test-utils').act;
});

Expand Down Expand Up @@ -72,26 +70,41 @@ describe('ReactJSXRuntime', () => {
expect(container.firstChild.textContent).toBe('persimmon');
});

it('should normalize props with default values', () => {
it('should normalize props with default values', async () => {
class Component extends React.Component {
render() {
return JSXRuntime.jsx('span', {children: this.props.prop});
}
}
Component.defaultProps = {prop: 'testKey'};

const instance = ReactTestUtils.renderIntoDocument(
JSXRuntime.jsx(Component, {}),
);
let container = document.createElement('div');
let root = ReactDOMClient.createRoot(container);
let instance;
await act(() => {
root.render(
JSXRuntime.jsx(Component, {ref: current => (instance = current)}),
);
});

expect(instance.props.prop).toBe('testKey');

const inst2 = ReactTestUtils.renderIntoDocument(
JSXRuntime.jsx(Component, {prop: null}),
);
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
let inst2;
await act(() => {
root.render(
JSXRuntime.jsx(Component, {
prop: null,
ref: current => (inst2 = current),
}),
);
});

expect(inst2.props.prop).toBe(null);
});

it('throws when changing a prop (in dev) after element creation', () => {
it('throws when changing a prop (in dev) after element creation', async () => {
class Outer extends React.Component {
render() {
const el = JSXRuntime.jsx('div', {className: 'moo'});
Expand All @@ -109,9 +122,13 @@ describe('ReactJSXRuntime', () => {
return el;
}
}
const outer = ReactTestUtils.renderIntoDocument(
JSXRuntime.jsx(Outer, {color: 'orange'}),
);
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(JSXRuntime.jsx(Outer, {color: 'orange'}));
});

const outer = container.firstChild;
if (__DEV__) {
expect(ReactDOM.findDOMNode(outer).className).toBe('moo');
} else {
Expand Down Expand Up @@ -151,15 +168,24 @@ describe('ReactJSXRuntime', () => {
}
});

it('does not warn for NaN props', () => {
it('does not warn for NaN props', async () => {
class Test extends React.Component {
render() {
return JSXRuntime.jsx('div', {});
}
}
const test = ReactTestUtils.renderIntoDocument(
JSXRuntime.jsx(Test, {value: +undefined}),
);
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
let test;
await act(() => {
root.render(
JSXRuntime.jsx(Test, {
value: +undefined,
ref: current => (test = current),
}),
);
});

expect(test.props.value).toBeNaN();
});

Expand Down