import * as React from 'react' import ReactDOM from 'react-dom' import ReactDOMServer from 'react-dom/server' import {fireEvent, render, screen, configure} from '../' const isReact18 = React.version.startsWith('18.') const isReact19 = React.version.startsWith('19.') const testGateReact18 = isReact18 ? test : test.skip const testGateReact19 = isReact19 ? test : test.skip describe('render API', () => { let originalConfig beforeEach(() => { // Grab the existing configuration so we can restore // it at the end of the test configure(existingConfig => { originalConfig = existingConfig // Don't change the existing config return {} }) }) afterEach(() => { configure(originalConfig) }) test('renders div into document', () => { const ref = React.createRef() const {container} = render(
) expect(container.firstChild).toBe(ref.current) }) test('works great with react portals', () => { class MyPortal extends React.Component { constructor(...args) { super(...args) this.portalNode = document.createElement('div') this.portalNode.dataset.testid = 'my-portal' } componentDidMount() { document.body.appendChild(this.portalNode) } componentWillUnmount() { this.portalNode.parentNode.removeChild(this.portalNode) } render() { return ReactDOM.createPortal( , this.portalNode, ) } } function Greet({greeting, subject}) { return (
{greeting} {subject}
) } const {unmount} = render() expect(screen.getByText('Hello World')).toBeInTheDocument() const portalNode = screen.getByTestId('my-portal') expect(portalNode).toBeInTheDocument() unmount() expect(portalNode).not.toBeInTheDocument() }) test('returns baseElement which defaults to document.body', () => { const {baseElement} = render(
) expect(baseElement).toBe(document.body) }) test('supports fragments', () => { class Test extends React.Component { render() { return (
DocumentFragment is pretty cool!
) } } const {asFragment} = render() expect(asFragment()).toMatchSnapshot() }) test('renders options.wrapper around node', () => { const WrapperComponent = ({children}) => (
{children}
) const {container} = render(
, { wrapper: WrapperComponent, }) expect(screen.getByTestId('wrapper')).toBeInTheDocument() expect(container.firstChild).toMatchInlineSnapshot(`
`) }) test('renders options.wrapper around node when reactStrictMode is true', () => { configure({reactStrictMode: true}) const WrapperComponent = ({children}) => (
{children}
) const {container} = render(
, { wrapper: WrapperComponent, }) expect(screen.getByTestId('wrapper')).toBeInTheDocument() expect(container.firstChild).toMatchInlineSnapshot(`
`) }) test('renders twice when reactStrictMode is true', () => { configure({reactStrictMode: true}) const spy = jest.fn() function Component() { spy() return null } render() expect(spy).toHaveBeenCalledTimes(2) }) test('flushes useEffect cleanup functions sync on unmount()', () => { const spy = jest.fn() function Component() { React.useEffect(() => spy, []) return null } const {unmount} = render() expect(spy).toHaveBeenCalledTimes(0) unmount() expect(spy).toHaveBeenCalledTimes(1) }) test('can be called multiple times on the same container', () => { const container = document.createElement('div') const {unmount} = render(, {container}) expect(container).toContainHTML('') render(, {container}) expect(container).toContainHTML('') unmount() expect(container).toBeEmptyDOMElement() }) test('hydrate will make the UI interactive', () => { function App() { const [clicked, handleClick] = React.useReducer(n => n + 1, 0) return ( ) } const ui = const container = document.createElement('div') document.body.appendChild(container) container.innerHTML = ReactDOMServer.renderToString(ui) expect(container).toHaveTextContent('clicked:0') render(ui, {container, hydrate: true}) fireEvent.click(container.querySelector('button')) expect(container).toHaveTextContent('clicked:1') }) test('hydrate can have a wrapper', () => { const wrapperComponentMountEffect = jest.fn() function WrapperComponent({children}) { React.useEffect(() => { wrapperComponentMountEffect() }) return children } const ui =
const container = document.createElement('div') document.body.appendChild(container) container.innerHTML = ReactDOMServer.renderToString(ui) render(ui, {container, hydrate: true, wrapper: WrapperComponent}) expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(1) }) testGateReact18('legacyRoot uses legacy ReactDOM.render', () => { expect(() => { render(
, {legacyRoot: true}) }).toErrorDev( [ "Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot", ], {withoutStack: true}, ) }) testGateReact19('legacyRoot throws', () => { expect(() => { render(
, {legacyRoot: true}) }).toThrowErrorMatchingInlineSnapshot( `\`legacyRoot: true\` is not supported in this version of React. If your app runs React 19 or later, you should remove this flag. If your app runs React 18 or earlier, visit https://react.dev/blog/2022/03/08/react-18-upgrade-guide for upgrade instructions.`, ) }) testGateReact18('legacyRoot uses legacy ReactDOM.hydrate', () => { const ui =
const container = document.createElement('div') container.innerHTML = ReactDOMServer.renderToString(ui) expect(() => { render(ui, {container, hydrate: true, legacyRoot: true}) }).toErrorDev( [ "Warning: ReactDOM.hydrate is no longer supported in React 18. Use hydrateRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot", ], {withoutStack: true}, ) }) testGateReact19('legacyRoot throws even with hydrate', () => { const ui =
const container = document.createElement('div') container.innerHTML = ReactDOMServer.renderToString(ui) expect(() => { render(ui, {container, hydrate: true, legacyRoot: true}) }).toThrowErrorMatchingInlineSnapshot( `\`legacyRoot: true\` is not supported in this version of React. If your app runs React 19 or later, you should remove this flag. If your app runs React 18 or earlier, visit https://react.dev/blog/2022/03/08/react-18-upgrade-guide for upgrade instructions.`, ) }) test('reactStrictMode in renderOptions has precedence over config when rendering', () => { const wrapperComponentMountEffect = jest.fn() function WrapperComponent({children}) { React.useEffect(() => { wrapperComponentMountEffect() }) return children } const ui =
configure({reactStrictMode: false}) render(ui, {wrapper: WrapperComponent, reactStrictMode: true}) expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(2) }) test('reactStrictMode in config is used when renderOptions does not specify reactStrictMode', () => { const wrapperComponentMountEffect = jest.fn() function WrapperComponent({children}) { React.useEffect(() => { wrapperComponentMountEffect() }) return children } const ui =
configure({reactStrictMode: true}) render(ui, {wrapper: WrapperComponent}) expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(2) }) })