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

fix(react18): unmount component with react18 API #23204

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 23 additions & 31 deletions npm/react/src/createMount.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as React from 'react'
import ReactDOM from 'react-dom'
import getDisplayName from './getDisplayName'
import {
injectStylesBeforeElement,
getContainerEl,
ROOT_SELECTOR,
setupHooks,
} from '@cypress/mount-utils'
import type { InternalMountOptions, InternalUnmountOptions, MountOptions, MountReturn, UnmountArgs } from './types'
import type { InternalMountOptions, MountOptions, MountReturn, UnmountArgs } from './types'

/**
* Inject custom style text or CSS file or 3rd party style resources
Expand All @@ -20,7 +19,7 @@ const injectStyles = (options: MountOptions) => {
}
}

export let lastMountedReactDom: (typeof ReactDOM) | undefined
export let mountCleanup: InternalMountOptions['cleanup']

/**
* Create an `mount` function. Performs all the non-React-version specific
Expand All @@ -42,6 +41,8 @@ export const makeMountFn = (
throw Error('internalMountOptions must be provided with `render` and `reactDom` parameters')
}

mountCleanup = internalMountOptions.cleanup

// Get the display name property via the component constructor
// @ts-ignore FIXME
const componentName = getDisplayName(jsx.type, options.alias)
Expand All @@ -58,8 +59,6 @@ export const makeMountFn = (
.then(() => {
const reactDomToUse = internalMountOptions.reactDom

lastMountedReactDom = reactDomToUse

const el = getContainerEl()

if (!el) {
Expand Down Expand Up @@ -136,40 +135,33 @@ export const makeMountFn = (
* This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters,
* or people writing adapters for third-party, custom adapters.
*/
export const makeUnmountFn = (options: UnmountArgs, internalUnmountOptions: InternalUnmountOptions) => {
export const makeUnmountFn = (options: UnmountArgs) => {
return cy.then(() => {
return cy.get(ROOT_SELECTOR, { log: false }).then(($el) => {
if (lastMountedReactDom) {
internalUnmountOptions.unmount($el[0])
const wasUnmounted = internalUnmountOptions.unmount($el[0])

if (wasUnmounted && options.log) {
Cypress.log({
name: 'unmount',
type: 'parent',
message: [options.boundComponentMessage ?? 'Unmounted component'],
consoleProps: () => {
return {
description: 'Unmounts React component',
parent: $el[0],
home: 'https://github.com/cypress-io/cypress',
}
},
})
}
}
})
const wasUnmounted = mountCleanup()
rockindahizzy marked this conversation as resolved.
Show resolved Hide resolved

if (wasUnmounted && options.log) {
Cypress.log({
name: 'unmount',
type: 'parent',
message: [options.boundComponentMessage ?? 'Unmounted component'],
consoleProps: () => {
return {
description: 'Unmounts React component',
parent: getContainerEl().parentNode,
home: 'https://github.com/cypress-io/cypress',
}
},
})
}
})
}

// Cleanup before each run
// NOTE: we cannot use unmount here because
// we are not in the context of a test
const preMountCleanup = () => {
const el = getContainerEl()

if (el && lastMountedReactDom) {
lastMountedReactDom.unmountComponentAtNode(el)
if (mountCleanup) {
mountCleanup()
}
}

Expand Down
30 changes: 20 additions & 10 deletions npm/react/src/mount.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import { getContainerEl } from '@cypress/mount-utils'
import React from 'react'
import ReactDOM from 'react-dom'
import {
makeMountFn,
makeUnmountFn,
lastMountedReactDom,
} from './createMount'
import type {
MountOptions,
InternalMountOptions,
InternalUnmountOptionsReact,
} from './types'

let lastReactDom: any
rockindahizzy marked this conversation as resolved.
Show resolved Hide resolved

const cleanup = () => {
if (lastReactDom) {
const root = getContainerEl()

lastReactDom.unmountComponentAtNode(root)

return true
}

return false
}

export function mount (jsx: React.ReactNode, options: MountOptions = {}, rerenderKey?: string) {
const internalOptions: InternalMountOptions = {
reactDom: ReactDOM,
render: (reactComponent: ReturnType<typeof React.createElement>, el: HTMLElement, reactDomToUse: typeof ReactDOM) => {
return (reactDomToUse || ReactDOM).render(reactComponent, el)
lastReactDom = (reactDomToUse || ReactDOM)

return lastReactDom.render(reactComponent, el)
},
unmount,
cleanup,
}

return makeMountFn('mount', jsx, { ReactDom: ReactDOM, ...options }, rerenderKey, internalOptions)
}

export function unmount (options = { log: true }) {
const internalOptions: InternalUnmountOptionsReact = {
unmount: (el) => {
return (lastMountedReactDom || ReactDOM).unmountComponentAtNode(el)
},
}

return makeUnmountFn(options, internalOptions)
return makeUnmountFn(options)
}
13 changes: 1 addition & 12 deletions npm/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ export interface UnmountArgs {
boundComponentMessage?: string
}

export interface InternalUnmountOptionsReact {
unmount: (el: HTMLElement) => boolean
}

export interface InternalUnmountOptionsReact18 {
unmount: () => boolean
}

export type InternalUnmountOptions =
InternalUnmountOptionsReact
| InternalUnmountOptionsReact18

export type MountOptions = Partial<StyleOptions & MountReactComponentOptions>

export interface MountReactComponentOptions {
Expand All @@ -43,6 +31,7 @@ export interface InternalMountOptions {
reactDomToUse: typeof import('react-dom')
) => void
unmount: (options: UnmountArgs) => void
cleanup: () => boolean

// globalThis.Cypress.Chainable<MountReturn>
}
Expand Down
23 changes: 12 additions & 11 deletions npm/react18/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ import {
import type {
MountOptions,
InternalMountOptions,
InternalUnmountOptionsReact18,
UnmountArgs,
} from '@cypress/react'

let root: any

const cleanup = () => {
if (root) {
root.unmount()

return true
}

return false
}

export function mount (jsx: React.ReactNode, options: MountOptions = {}, rerenderKey?: string) {
const internalOptions: InternalMountOptions = {
reactDom: ReactDOM,
Expand All @@ -23,20 +32,12 @@ export function mount (jsx: React.ReactNode, options: MountOptions = {}, rerende
return root.render(reactComponent)
},
unmount,
cleanup,
}

return makeMountFn('mount', jsx, { ReactDom: ReactDOM, ...options }, rerenderKey, internalOptions)
}

export function unmount (options: UnmountArgs = { log: true }) {
const internalOptions: InternalUnmountOptionsReact18 = {
// type is ReturnType<typeof ReactDOM.createRoot>
unmount: (): boolean => {
root.unmount()

return true
},
}

return makeUnmountFn(options, internalOptions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file below this (_snapshots__) has a surprisingly huge diff with a lot of red. Is this right? It seems to be missing the "summary" part of the CLI output.

return makeUnmountFn(options)
}
Loading