@@ -29,34 +29,40 @@ const injectStyles = (options: MountOptions) => () => {
29
29
/**
30
30
* Mount a React component in a blank document; register it as an alias
31
31
* To access: use an alias or original component reference
32
- * @function cy.mount
33
- * @param {React.ReactElement } jsx - component to mount
34
- * @param {string } [Component] - alias to use later
35
- * @example
32
+ * @function mount
33
+ * @param {React.ReactElement } jsx - component to mount
34
+ * @param {MountOptions } [options] - options, like alias, styles
35
+ * @see https://github.com/bahmutov/cypress-react-unit-test
36
+ * @see https://glebbahmutov.com/blog/my-vision-for-component-tests/
37
+ * @example
36
38
```
37
- import Hello from './hello.jsx'
38
- // mount and access by alias
39
- cy.mount(<Hello />, 'Hello')
40
- // using default alias
41
- cy.get('@Component')
42
- // using original component
43
- cy.get(Hello )
39
+ import Hello from './hello.jsx'
40
+ import { mount} from 'cypress-react-unit-test'
41
+ it('works', () => {
42
+ mount(<Hello onClick={cy.stub()} />)
43
+ // use Cypress commands
44
+ cy.contains('Hello').click()
45
+ } )
44
46
```
45
47
**/
46
48
export const mount = ( jsx : React . ReactElement , options : MountOptions = { } ) => {
47
49
checkMountModeEnabled ( )
48
50
49
51
// Get the display name property via the component constructor
50
52
// @ts -ignore FIXME
51
- const displayName = getDisplayName ( jsx . type , options . alias )
53
+ const componentName = getDisplayName ( jsx . type , options . alias )
54
+ const displayName = options . alias || componentName
55
+ const message = options . alias
56
+ ? `<${ componentName } ... /> as "${ options . alias } "`
57
+ : `<${ componentName } ... />`
52
58
let logInstance : Cypress . Log
53
59
54
60
return cy
55
61
. then ( ( ) => {
56
62
if ( options . log !== false ) {
57
63
logInstance = Cypress . log ( {
58
64
name : 'mount' ,
59
- message : [ `< ${ displayName } ... />` ] ,
65
+ message : [ message ] ,
60
66
} )
61
67
}
62
68
} )
@@ -85,7 +91,11 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
85
91
}
86
92
87
93
const reactComponent = React . createElement ( React . Fragment , props , jsx )
88
- const CypressTestComponent = reactDomToUse . render ( reactComponent , el )
94
+ // since we always surround the component with a fragment
95
+ // let's get back the original component
96
+ // @ts -ignore
97
+ const userComponent = reactComponent . props . children
98
+ reactDomToUse . render ( reactComponent , el )
89
99
90
100
if ( logInstance ) {
91
101
const logConsoleProps = {
@@ -109,8 +119,8 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
109
119
110
120
return (
111
121
cy
112
- . wrap ( CypressTestComponent , { log : false } )
113
- . as ( options . alias || displayName )
122
+ . wrap ( userComponent , { log : false } )
123
+ . as ( displayName )
114
124
// by waiting, we give the component's hook a chance to run
115
125
// https://github.com/bahmutov/cypress-react-unit-test/issues/200
116
126
. wait ( 1 , { log : false } )
@@ -129,7 +139,7 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
129
139
}
130
140
131
141
/**
132
- * Removes any mounted component
142
+ * Removes the mounted component
133
143
* @see https://github.com/bahmutov/cypress-react-unit-test/tree/master/cypress/component/basic/unmount
134
144
* @example
135
145
```
0 commit comments