@@ -29,33 +29,31 @@ import { stylesCache, setXMLHttpRequest, setAlert } from '../../lib'
2929 @function cy.injectReactDOM
3030**/
3131Cypress . Commands . add ( 'injectReactDOM' , ( ) => {
32- return cy
33- . log ( 'Injecting ReactDOM for Unit Testing' )
34- . then ( ( ) => {
35- // Generate inline script tags for UMD modules
36- const scripts = Cypress . modules
37- . map ( module => `<script>${ module . source } </script>` )
38- . join ( '' )
39- // include React and ReactDOM to force DOM to register all DOM event listeners
40- // otherwise the component will NOT be able to dispatch any events
41- // when it runs the second time
42- // https://github.com/bahmutov/cypress-react-unit-test/issues/3
43- var html = `<body>
32+ return cy . log ( 'Injecting ReactDOM for Unit Testing' ) . then ( ( ) => {
33+ // Generate inline script tags for UMD modules
34+ const scripts = Cypress . modules
35+ . map ( module => `<script>${ module . source } </script>` )
36+ . join ( '' )
37+ // include React and ReactDOM to force DOM to register all DOM event listeners
38+ // otherwise the component will NOT be able to dispatch any events
39+ // when it runs the second time
40+ // https://github.com/bahmutov/cypress-react-unit-test/issues/3
41+ var html = `<body>
4442 <div id="cypress-jsdom"></div>
4543 ${ scripts }
4644 </body>`
47- const document = cy . state ( 'document' )
48- document . write ( html )
49- document . close ( )
50- } )
45+ const document = cy . state ( 'document' )
46+ document . write ( html )
47+ document . close ( )
48+ } )
5149} )
5250
5351cy . stylesCache = stylesCache
5452/** Caches styles from previously compiled components for reuse
5553 @function cy.copyComponentStyles
5654 @param {Object } component
5755**/
58- Cypress . Commands . add ( 'copyComponentStyles' , ( component ) => {
56+ Cypress . Commands . add ( 'copyComponentStyles' , component => {
5957 // need to find same component when component is recompiled
6058 // by the JSX preprocessor. Thus have to use something else,
6159 // like component name
@@ -84,32 +82,48 @@ Cypress.Commands.add('copyComponentStyles', (component) => {
8482 styles . forEach ( function ( style ) {
8583 head . appendChild ( style )
8684 } )
87- return
8885} )
8986
90- /** Mount a React component in a blank document; register it as an alias
91- To access: use an alias, e.g.cy.get('@Component').its('state').should(...)
92- @function cy.mount
93- @param {Object } jsx
94- @param {string } [Component] alias
95- **/
96- Cypress . Commands . add ( 'mount' , ( jsx , alias ) => {
97- // Get the displayname property via the component constructor
87+ /**
88+ * Mount a React component in a blank document; register it as an alias
89+ * To access: use an alias or original component reference
90+ * @function cy.mount
91+ * @param {Object } jsx - component to mount
92+ * @param {string } [Component] - alias to use later
93+ * @example
94+ ```
95+ import Hello from './hello.jsx'
96+ // mount and access by alias
97+ cy.mount(<Hello />, 'Hello')
98+ // using default alias
99+ cy.get('@Component')
100+ // using specified alias
101+ cy.get('@Hello').its('state').should(...)
102+ // using original component
103+ cy.get(Hello)
104+ ```
105+ **/
106+ export const mount = ( jsx , alias ) => {
107+ // Get the display name property via the component constructor
98108 const displayname = alias || jsx . type . prototype . constructor . name
99- cy
100- . injectReactDOM ( )
109+ cy . injectReactDOM ( )
101110 . log ( `ReactDOM.render(<${ displayname } ... />)` , jsx . props )
102111 . window ( { log : false } )
103112 . then ( setXMLHttpRequest )
104113 . then ( setAlert )
105114 . then ( win => {
106115 const { ReactDOM } = win
107116 const document = cy . state ( 'document' )
108- const component = ReactDOM . render ( jsx , document . getElementById ( 'cypress-jsdom' ) )
117+ const component = ReactDOM . render (
118+ jsx ,
119+ document . getElementById ( 'cypress-jsdom' )
120+ )
109121 cy . wrap ( component , { log : false } ) . as ( displayname )
110122 } )
111123 cy . copyComponentStyles ( jsx )
112- } )
124+ }
125+
126+ Cypress . Commands . add ( 'mount' , mount )
113127
114128/** Get one or more DOM elements by selector or alias.
115129 Features extended support for JSX and React.Component
@@ -124,7 +138,10 @@ Cypress.Commands.overwrite('get', (originalFn, selector, options) => {
124138 switch ( typeof selector ) {
125139 case 'object' :
126140 // If attempting to use JSX as a selector, reference the displayname
127- if ( selector . $$typeof && selector . $$typeof . toString ( ) . startsWith ( 'Symbol(react' ) ) {
141+ if (
142+ selector . $$typeof &&
143+ selector . $$typeof . toString ( ) . startsWith ( 'Symbol(react' )
144+ ) {
128145 const displayname = selector . type . prototype . constructor . name
129146 return originalFn ( `@${ displayname } ` , options )
130147 }
0 commit comments