|
| 1 | +import * as React from 'react' |
| 2 | +import ReactDOM from 'react-dom' |
| 3 | +import getDisplayName from './getDisplayName' |
| 4 | +import { |
| 5 | + injectStylesBeforeElement, |
| 6 | + getContainerEl, |
| 7 | + ROOT_SELECTOR, |
| 8 | + setupHooks, |
| 9 | +} from '@cypress/mount-utils' |
| 10 | +import type { InternalMountOptions, InternalUnmountOptions, MountOptions, MountReturn, UnmountArgs } from './types' |
| 11 | + |
| 12 | +/** |
| 13 | + * Inject custom style text or CSS file or 3rd party style resources |
| 14 | + */ |
| 15 | +const injectStyles = (options: MountOptions) => { |
| 16 | + return (): HTMLElement => { |
| 17 | + const el = getContainerEl() |
| 18 | + |
| 19 | + return injectStylesBeforeElement(options, document, el) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +export let lastMountedReactDom: (typeof ReactDOM) | undefined |
| 24 | + |
| 25 | +/** |
| 26 | + * Create an `mount` function. Performs all the non-React-version specific |
| 27 | + * behavior related to mounting. The React-version-specific code |
| 28 | + * is injected. This helps us to maintain a consistent public API |
| 29 | + * and handle breaking changes in React's rendering API. |
| 30 | + * |
| 31 | + * This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters, |
| 32 | + * or people writing adapters for third-party, custom adapters. |
| 33 | + */ |
| 34 | +export const makeMountFn = ( |
| 35 | + type: 'mount' | 'rerender', |
| 36 | + jsx: React.ReactNode, |
| 37 | + options: MountOptions = {}, |
| 38 | + rerenderKey?: string, |
| 39 | + internalMountOptions?: InternalMountOptions, |
| 40 | +): globalThis.Cypress.Chainable<MountReturn> => { |
| 41 | + if (!internalMountOptions) { |
| 42 | + throw Error('internalMountOptions must be provided with `render` and `reactDom` parameters') |
| 43 | + } |
| 44 | + |
| 45 | + // Get the display name property via the component constructor |
| 46 | + // @ts-ignore FIXME |
| 47 | + const componentName = getDisplayName(jsx.type, options.alias) |
| 48 | + const displayName = options.alias || componentName |
| 49 | + |
| 50 | + const jsxComponentName = `<${componentName} ... />` |
| 51 | + |
| 52 | + const message = options.alias |
| 53 | + ? `${jsxComponentName} as "${options.alias}"` |
| 54 | + : jsxComponentName |
| 55 | + |
| 56 | + return cy |
| 57 | + .then(injectStyles(options)) |
| 58 | + .then(() => { |
| 59 | + const reactDomToUse = internalMountOptions.reactDom |
| 60 | + |
| 61 | + lastMountedReactDom = reactDomToUse |
| 62 | + |
| 63 | + const el = getContainerEl() |
| 64 | + |
| 65 | + if (!el) { |
| 66 | + throw new Error( |
| 67 | + [ |
| 68 | + `[@cypress/react] 🔥 Hmm, cannot find root element to mount the component. Searched for ${ROOT_SELECTOR}`, |
| 69 | + ].join(' '), |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + const key = rerenderKey ?? |
| 74 | + // @ts-ignore provide unique key to the the wrapped component to make sure we are rerendering between tests |
| 75 | + (Cypress?.mocha?.getRunner()?.test?.title as string || '') + Math.random() |
| 76 | + const props = { |
| 77 | + key, |
| 78 | + } |
| 79 | + |
| 80 | + const reactComponent = React.createElement( |
| 81 | + options.strict ? React.StrictMode : React.Fragment, |
| 82 | + props, |
| 83 | + jsx, |
| 84 | + ) |
| 85 | + // since we always surround the component with a fragment |
| 86 | + // let's get back the original component |
| 87 | + const userComponent = (reactComponent.props as { |
| 88 | + key: string |
| 89 | + children: React.ReactNode |
| 90 | + }).children |
| 91 | + |
| 92 | + internalMountOptions.render(reactComponent, el, reactDomToUse) |
| 93 | + |
| 94 | + if (options.log !== false) { |
| 95 | + Cypress.log({ |
| 96 | + name: type, |
| 97 | + type: 'parent', |
| 98 | + message: [message], |
| 99 | + // @ts-ignore |
| 100 | + $el: (el.children.item(0) as unknown) as JQuery<HTMLElement>, |
| 101 | + consoleProps: () => { |
| 102 | + return { |
| 103 | + // @ts-ignore protect the use of jsx functional components use ReactNode |
| 104 | + props: jsx.props, |
| 105 | + description: type === 'mount' ? 'Mounts React component' : 'Rerenders mounted React component', |
| 106 | + home: 'https://github.com/cypress-io/cypress', |
| 107 | + } |
| 108 | + }, |
| 109 | + }).snapshot('mounted').end() |
| 110 | + } |
| 111 | + |
| 112 | + return ( |
| 113 | + // Separate alias and returned value. Alias returns the component only, and the thenable returns the additional functions |
| 114 | + cy.wrap<React.ReactNode>(userComponent, { log: false }) |
| 115 | + .as(displayName) |
| 116 | + .then(() => { |
| 117 | + return cy.wrap<MountReturn>({ |
| 118 | + component: userComponent, |
| 119 | + rerender: (newComponent) => makeMountFn('rerender', newComponent, options, key, internalMountOptions), |
| 120 | + unmount: internalMountOptions.unmount, |
| 121 | + }, { log: false }) |
| 122 | + }) |
| 123 | + // by waiting, we delaying test execution for the next tick of event loop |
| 124 | + // and letting hooks and component lifecycle methods to execute mount |
| 125 | + // https://github.com/bahmutov/cypress-react-unit-test/issues/200 |
| 126 | + .wait(0, { log: false }) |
| 127 | + ) |
| 128 | + // Bluebird types are terrible. I don't think the return type can be carried without this cast |
| 129 | + }) as unknown as globalThis.Cypress.Chainable<MountReturn> |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Create an `unmount` function. Performs all the non-React-version specific |
| 134 | + * behavior related to unmounting. |
| 135 | + * |
| 136 | + * This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters, |
| 137 | + * or people writing adapters for third-party, custom adapters. |
| 138 | + */ |
| 139 | +export const makeUnmountFn = (options: UnmountArgs, internalUnmountOptions: InternalUnmountOptions) => { |
| 140 | + return cy.then(() => { |
| 141 | + return cy.get(ROOT_SELECTOR, { log: false }).then(($el) => { |
| 142 | + if (lastMountedReactDom) { |
| 143 | + internalUnmountOptions.unmount($el[0]) |
| 144 | + const wasUnmounted = internalUnmountOptions.unmount($el[0]) |
| 145 | + |
| 146 | + if (wasUnmounted && options.log) { |
| 147 | + Cypress.log({ |
| 148 | + name: 'unmount', |
| 149 | + type: 'parent', |
| 150 | + message: [options.boundComponentMessage ?? 'Unmounted component'], |
| 151 | + consoleProps: () => { |
| 152 | + return { |
| 153 | + description: 'Unmounts React component', |
| 154 | + parent: $el[0], |
| 155 | + home: 'https://github.com/cypress-io/cypress', |
| 156 | + } |
| 157 | + }, |
| 158 | + }) |
| 159 | + } |
| 160 | + } |
| 161 | + }) |
| 162 | + }) |
| 163 | +} |
| 164 | + |
| 165 | +// Cleanup before each run |
| 166 | +// NOTE: we cannot use unmount here because |
| 167 | +// we are not in the context of a test |
| 168 | +const preMountCleanup = () => { |
| 169 | + const el = getContainerEl() |
| 170 | + |
| 171 | + if (el && lastMountedReactDom) { |
| 172 | + lastMountedReactDom.unmountComponentAtNode(el) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +const _mount = (jsx: React.ReactNode, options: MountOptions = {}) => makeMountFn('mount', jsx, options) |
| 177 | + |
| 178 | +export const createMount = (defaultOptions: MountOptions) => { |
| 179 | + return ( |
| 180 | + element: React.ReactElement, |
| 181 | + options?: MountOptions, |
| 182 | + ) => { |
| 183 | + return _mount(element, { ...defaultOptions, ...options }) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** @deprecated Should be removed in the next major version */ |
| 188 | +// TODO: Remove |
| 189 | +export default _mount |
| 190 | + |
| 191 | +export interface JSX extends Function { |
| 192 | + displayName: string |
| 193 | +} |
| 194 | + |
| 195 | +// Side effects from "import { mount } from '@cypress/<my-framework>'" are annoying, we should avoid doing this |
| 196 | +// by creating an explicit function/import that the user can register in their 'component.js' support file, |
| 197 | +// such as: |
| 198 | +// import 'cypress/<my-framework>/support' |
| 199 | +// or |
| 200 | +// import { registerCT } from 'cypress/<my-framework>' |
| 201 | +// registerCT() |
| 202 | +// Note: This would be a breaking change |
| 203 | + |
| 204 | +// it is required to unmount component in beforeEach hook in order to provide a clean state inside test |
| 205 | +// because `mount` can be called after some preparation that can side effect unmount |
| 206 | +// @see npm/react/cypress/component/advanced/set-timeout-example/loading-indicator-spec.js |
| 207 | +setupHooks(preMountCleanup) |
0 commit comments