1
1
import * as React from 'react'
2
- import ReactDOM from 'react-dom'
3
2
import getDisplayName from './getDisplayName'
4
3
import {
5
4
injectStylesBeforeElement ,
6
5
getContainerEl ,
7
6
ROOT_SELECTOR ,
8
7
setupHooks ,
9
8
} from '@cypress/mount-utils'
10
- import type { InternalMountOptions , InternalUnmountOptions , MountOptions , MountReturn , UnmountArgs } from './types'
9
+ import type { InternalMountOptions , MountOptions , MountReturn , UnmountArgs } from './types'
11
10
12
11
/**
13
12
* Inject custom style text or CSS file or 3rd party style resources
@@ -20,7 +19,7 @@ const injectStyles = (options: MountOptions) => {
20
19
}
21
20
}
22
21
23
- export let lastMountedReactDom : ( typeof ReactDOM ) | undefined
22
+ let mountCleanup : InternalMountOptions [ 'cleanup' ]
24
23
25
24
/**
26
25
* Create an `mount` function. Performs all the non-React-version specific
@@ -42,6 +41,8 @@ export const makeMountFn = (
42
41
throw Error ( 'internalMountOptions must be provided with `render` and `reactDom` parameters' )
43
42
}
44
43
44
+ mountCleanup = internalMountOptions . cleanup
45
+
45
46
// Get the display name property via the component constructor
46
47
// @ts -ignore FIXME
47
48
const componentName = getDisplayName ( jsx . type , options . alias )
@@ -58,8 +59,6 @@ export const makeMountFn = (
58
59
. then ( ( ) => {
59
60
const reactDomToUse = internalMountOptions . reactDom
60
61
61
- lastMountedReactDom = reactDomToUse
62
-
63
62
const el = getContainerEl ( )
64
63
65
64
if ( ! el ) {
@@ -136,41 +135,32 @@ export const makeMountFn = (
136
135
* This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters,
137
136
* or people writing adapters for third-party, custom adapters.
138
137
*/
139
- export const makeUnmountFn = ( options : UnmountArgs , internalUnmountOptions : InternalUnmountOptions ) => {
138
+ export const makeUnmountFn = ( options : UnmountArgs ) => {
140
139
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
- } )
140
+ const wasUnmounted = mountCleanup ?.( )
141
+
142
+ if ( wasUnmounted && options . log ) {
143
+ Cypress . log ( {
144
+ name : 'unmount' ,
145
+ type : 'parent' ,
146
+ message : [ options . boundComponentMessage ?? 'Unmounted component' ] ,
147
+ consoleProps : ( ) => {
148
+ return {
149
+ description : 'Unmounts React component' ,
150
+ parent : getContainerEl ( ) . parentNode ,
151
+ home : 'https://github.com/cypress-io/cypress' ,
152
+ }
153
+ } ,
154
+ } )
155
+ }
162
156
} )
163
157
}
164
158
165
159
// Cleanup before each run
166
160
// NOTE: we cannot use unmount here because
167
161
// we are not in the context of a test
168
162
const preMountCleanup = ( ) => {
169
- const el = getContainerEl ( )
170
-
171
- if ( el && lastMountedReactDom ) {
172
- lastMountedReactDom . unmountComponentAtNode ( el )
173
- }
163
+ mountCleanup ?.( )
174
164
}
175
165
176
166
const _mount = ( jsx : React . ReactNode , options : MountOptions = { } ) => makeMountFn ( 'mount' , jsx , options )
0 commit comments