Skip to content

Commit

Permalink
Remove ReactTestUtils from ReactJSXRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Silbermann committed Feb 14, 2024
1 parent adadb81 commit 4eeac55
Showing 1 changed file with 43 additions and 17 deletions.
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

0 comments on commit 4eeac55

Please sign in to comment.