@@ -29,33 +29,31 @@ import { stylesCache, setXMLHttpRequest, setAlert } from '../../lib'
29
29
@function cy.injectReactDOM
30
30
**/
31
31
Cypress . 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>
44
42
<div id="cypress-jsdom"></div>
45
43
${ scripts }
46
44
</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
+ } )
51
49
} )
52
50
53
51
cy . stylesCache = stylesCache
54
52
/** Caches styles from previously compiled components for reuse
55
53
@function cy.copyComponentStyles
56
54
@param {Object } component
57
55
**/
58
- Cypress . Commands . add ( 'copyComponentStyles' , ( component ) => {
56
+ Cypress . Commands . add ( 'copyComponentStyles' , component => {
59
57
// need to find same component when component is recompiled
60
58
// by the JSX preprocessor. Thus have to use something else,
61
59
// like component name
@@ -84,32 +82,48 @@ Cypress.Commands.add('copyComponentStyles', (component) => {
84
82
styles . forEach ( function ( style ) {
85
83
head . appendChild ( style )
86
84
} )
87
- return
88
85
} )
89
86
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
98
108
const displayname = alias || jsx . type . prototype . constructor . name
99
- cy
100
- . injectReactDOM ( )
109
+ cy . injectReactDOM ( )
101
110
. log ( `ReactDOM.render(<${ displayname } ... />)` , jsx . props )
102
111
. window ( { log : false } )
103
112
. then ( setXMLHttpRequest )
104
113
. then ( setAlert )
105
114
. then ( win => {
106
115
const { ReactDOM } = win
107
116
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
+ )
109
121
cy . wrap ( component , { log : false } ) . as ( displayname )
110
122
} )
111
123
cy . copyComponentStyles ( jsx )
112
- } )
124
+ }
125
+
126
+ Cypress . Commands . add ( 'mount' , mount )
113
127
114
128
/** Get one or more DOM elements by selector or alias.
115
129
Features extended support for JSX and React.Component
@@ -124,7 +138,10 @@ Cypress.Commands.overwrite('get', (originalFn, selector, options) => {
124
138
switch ( typeof selector ) {
125
139
case 'object' :
126
140
// 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
+ ) {
128
145
const displayname = selector . type . prototype . constructor . name
129
146
return originalFn ( `@${ displayname } ` , options )
130
147
}
0 commit comments