@@ -29,34 +29,40 @@ const injectStyles = (options: MountOptions) => () => {
2929/**
3030 * Mount a React component in a blank document; register it as an alias
3131 * 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
3638 ```
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+ } )
4446 ```
4547 **/
4648export const mount = ( jsx : React . ReactElement , options : MountOptions = { } ) => {
4749 checkMountModeEnabled ( )
4850
4951 // Get the display name property via the component constructor
5052 // @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 } ... />`
5258 let logInstance : Cypress . Log
5359
5460 return cy
5561 . then ( ( ) => {
5662 if ( options . log !== false ) {
5763 logInstance = Cypress . log ( {
5864 name : 'mount' ,
59- message : [ `< ${ displayName } ... />` ] ,
65+ message : [ message ] ,
6066 } )
6167 }
6268 } )
@@ -85,7 +91,11 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
8591 }
8692
8793 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 )
8999
90100 if ( logInstance ) {
91101 const logConsoleProps = {
@@ -109,8 +119,8 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
109119
110120 return (
111121 cy
112- . wrap ( CypressTestComponent , { log : false } )
113- . as ( options . alias || displayName )
122+ . wrap ( userComponent , { log : false } )
123+ . as ( displayName )
114124 // by waiting, we give the component's hook a chance to run
115125 // https://github.com/bahmutov/cypress-react-unit-test/issues/200
116126 . wait ( 1 , { log : false } )
@@ -129,7 +139,7 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
129139}
130140
131141/**
132- * Removes any mounted component
142+ * Removes the mounted component
133143 * @see https://github.com/bahmutov/cypress-react-unit-test/tree/master/cypress/component/basic/unmount
134144 * @example
135145 ```
0 commit comments