| | import * as React from 'react' |
| | import ReactDOM from 'react-dom' |
| | import * as ReactDOMClient from 'react-dom/client' |
| | import { |
| | getQueriesForElement, |
| | prettyDOM, |
| | configure as configureDTL, |
| | } from '@testing-library/dom' |
| | import act, { |
| | getIsReactActEnvironment, |
| | setReactActEnvironment, |
| | } from './act-compat' |
| | import {fireEvent} from './fire-event' |
| | import {getConfig, configure} from './config' |
| |
|
| | function jestFakeTimersAreEnabled() { |
| | |
| | if (typeof jest !== 'undefined' && jest !== null) { |
| | return ( |
| | |
| | setTimeout._isMockFunction === true || |
| | |
| | Object.prototype.hasOwnProperty.call(setTimeout, 'clock') |
| | ) |
| | } |
| |
|
| | return false |
| | } |
| |
|
| | configureDTL({ |
| | unstable_advanceTimersWrapper: cb => { |
| | return act(cb) |
| | }, |
| | |
| | |
| | |
| | asyncWrapper: async cb => { |
| | const previousActEnvironment = getIsReactActEnvironment() |
| | setReactActEnvironment(false) |
| | try { |
| | const result = await cb() |
| | |
| | |
| | |
| | await new Promise(resolve => { |
| | setTimeout(() => { |
| | resolve() |
| | }, 0) |
| |
|
| | if (jestFakeTimersAreEnabled()) { |
| | jest.advanceTimersByTime(0) |
| | } |
| | }) |
| |
|
| | return result |
| | } finally { |
| | setReactActEnvironment(previousActEnvironment) |
| | } |
| | }, |
| | eventWrapper: cb => { |
| | let result |
| | act(() => { |
| | result = cb() |
| | }) |
| | return result |
| | }, |
| | }) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | const mountedContainers = new Set() |
| | |
| | |
| | |
| | const mountedRootEntries = [] |
| |
|
| | function strictModeIfNeeded(innerElement, reactStrictMode) { |
| | return reactStrictMode ?? getConfig().reactStrictMode |
| | ? React.createElement(React.StrictMode, null, innerElement) |
| | : innerElement |
| | } |
| |
|
| | function wrapUiIfNeeded(innerElement, wrapperComponent) { |
| | return wrapperComponent |
| | ? React.createElement(wrapperComponent, null, innerElement) |
| | : innerElement |
| | } |
| |
|
| | function createConcurrentRoot( |
| | container, |
| | { |
| | hydrate, |
| | onCaughtError, |
| | onRecoverableError, |
| | ui, |
| | wrapper: WrapperComponent, |
| | reactStrictMode, |
| | }, |
| | ) { |
| | let root |
| | if (hydrate) { |
| | act(() => { |
| | root = ReactDOMClient.hydrateRoot( |
| | container, |
| | strictModeIfNeeded( |
| | wrapUiIfNeeded(ui, WrapperComponent), |
| | reactStrictMode, |
| | ), |
| | {onCaughtError, onRecoverableError}, |
| | ) |
| | }) |
| | } else { |
| | root = ReactDOMClient.createRoot(container, { |
| | onCaughtError, |
| | onRecoverableError, |
| | }) |
| | } |
| |
|
| | return { |
| | hydrate() { |
| | |
| | if (!hydrate) { |
| | throw new Error( |
| | 'Attempted to hydrate a non-hydrateable root. This is a bug in `@testing-library/react`.', |
| | ) |
| | } |
| | |
| | }, |
| | render(element) { |
| | root.render(element) |
| | }, |
| | unmount() { |
| | root.unmount() |
| | }, |
| | } |
| | } |
| |
|
| | function createLegacyRoot(container) { |
| | return { |
| | hydrate(element) { |
| | ReactDOM.hydrate(element, container) |
| | }, |
| | render(element) { |
| | ReactDOM.render(element, container) |
| | }, |
| | unmount() { |
| | ReactDOM.unmountComponentAtNode(container) |
| | }, |
| | } |
| | } |
| |
|
| | function renderRoot( |
| | ui, |
| | { |
| | baseElement, |
| | container, |
| | hydrate, |
| | queries, |
| | root, |
| | wrapper: WrapperComponent, |
| | reactStrictMode, |
| | }, |
| | ) { |
| | act(() => { |
| | if (hydrate) { |
| | root.hydrate( |
| | strictModeIfNeeded( |
| | wrapUiIfNeeded(ui, WrapperComponent), |
| | reactStrictMode, |
| | ), |
| | container, |
| | ) |
| | } else { |
| | root.render( |
| | strictModeIfNeeded( |
| | wrapUiIfNeeded(ui, WrapperComponent), |
| | reactStrictMode, |
| | ), |
| | container, |
| | ) |
| | } |
| | }) |
| |
|
| | return { |
| | container, |
| | baseElement, |
| | debug: (el = baseElement, maxLength, options) => |
| | Array.isArray(el) |
| | ? |
| | el.forEach(e => console.log(prettyDOM(e, maxLength, options))) |
| | : |
| | console.log(prettyDOM(el, maxLength, options)), |
| | unmount: () => { |
| | act(() => { |
| | root.unmount() |
| | }) |
| | }, |
| | rerender: rerenderUi => { |
| | renderRoot(rerenderUi, { |
| | container, |
| | baseElement, |
| | root, |
| | wrapper: WrapperComponent, |
| | reactStrictMode, |
| | }) |
| | |
| | |
| | }, |
| | asFragment: () => { |
| | |
| | if (typeof document.createRange === 'function') { |
| | return document |
| | .createRange() |
| | .createContextualFragment(container.innerHTML) |
| | } else { |
| | const template = document.createElement('template') |
| | template.innerHTML = container.innerHTML |
| | return template.content |
| | } |
| | }, |
| | ...getQueriesForElement(baseElement, queries), |
| | } |
| | } |
| |
|
| | function render( |
| | ui, |
| | { |
| | container, |
| | baseElement = container, |
| | legacyRoot = false, |
| | onCaughtError, |
| | onUncaughtError, |
| | onRecoverableError, |
| | queries, |
| | hydrate = false, |
| | wrapper, |
| | reactStrictMode, |
| | } = {}, |
| | ) { |
| | if (onUncaughtError !== undefined) { |
| | throw new Error( |
| | 'onUncaughtError is not supported. The `render` call will already throw on uncaught errors.', |
| | ) |
| | } |
| | if (legacyRoot && typeof ReactDOM.render !== 'function') { |
| | const error = new Error( |
| | '`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.', |
| | ) |
| | Error.captureStackTrace(error, render) |
| | throw error |
| | } |
| |
|
| | if (!baseElement) { |
| | |
| | |
| | baseElement = document.body |
| | } |
| | if (!container) { |
| | container = baseElement.appendChild(document.createElement('div')) |
| | } |
| |
|
| | let root |
| | |
| | if (!mountedContainers.has(container)) { |
| | const createRootImpl = legacyRoot ? createLegacyRoot : createConcurrentRoot |
| | root = createRootImpl(container, { |
| | hydrate, |
| | onCaughtError, |
| | onRecoverableError, |
| | ui, |
| | wrapper, |
| | reactStrictMode, |
| | }) |
| |
|
| | mountedRootEntries.push({container, root}) |
| | |
| | |
| | |
| | mountedContainers.add(container) |
| | } else { |
| | mountedRootEntries.forEach(rootEntry => { |
| | |
| | |
| | |
| | if (rootEntry.container === container) { |
| | root = rootEntry.root |
| | } |
| | }) |
| | } |
| |
|
| | return renderRoot(ui, { |
| | container, |
| | baseElement, |
| | queries, |
| | hydrate, |
| | wrapper, |
| | root, |
| | reactStrictMode, |
| | }) |
| | } |
| |
|
| | function cleanup() { |
| | mountedRootEntries.forEach(({root, container}) => { |
| | act(() => { |
| | root.unmount() |
| | }) |
| | if (container.parentNode === document.body) { |
| | document.body.removeChild(container) |
| | } |
| | }) |
| | mountedRootEntries.length = 0 |
| | mountedContainers.clear() |
| | } |
| |
|
| | function renderHook(renderCallback, options = {}) { |
| | const {initialProps, ...renderOptions} = options |
| |
|
| | if (renderOptions.legacyRoot && typeof ReactDOM.render !== 'function') { |
| | const error = new Error( |
| | '`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.', |
| | ) |
| | Error.captureStackTrace(error, renderHook) |
| | throw error |
| | } |
| |
|
| | const result = React.createRef() |
| |
|
| | function TestComponent({renderCallbackProps}) { |
| | const pendingResult = renderCallback(renderCallbackProps) |
| |
|
| | React.useEffect(() => { |
| | result.current = pendingResult |
| | }) |
| |
|
| | return null |
| | } |
| |
|
| | const {rerender: baseRerender, unmount} = render( |
| | <TestComponent renderCallbackProps={initialProps} />, |
| | renderOptions, |
| | ) |
| |
|
| | function rerender(rerenderCallbackProps) { |
| | return baseRerender( |
| | <TestComponent renderCallbackProps={rerenderCallbackProps} />, |
| | ) |
| | } |
| |
|
| | return {result, rerender, unmount} |
| | } |
| |
|
| | |
| | export * from '@testing-library/dom' |
| | export {render, renderHook, cleanup, act, fireEvent, getConfig, configure} |
| |
|
| | |
| |
|