Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 219b271

Browse files
committed
chore: copy jsdoc for mount
1 parent 54700c5 commit 219b271

File tree

3 files changed

+73
-31
lines changed

3 files changed

+73
-31
lines changed

cypress/integration/hello-world-inline-spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference types="cypress" />
2+
/// <reference types="../support" />
13
import React from 'react'
24
const HelloWorld = () => <p>Hello World!</p>
35
describe('HelloWorld component', () => {

cypress/support/commands.js

+48-31
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,31 @@ import { stylesCache, setXMLHttpRequest, setAlert } from '../../lib'
2929
@function cy.injectReactDOM
3030
**/
3131
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>
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

5351
cy.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
}

cypress/support/index.d.ts

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// I hope to get types and docs from functions imported from ./commands one day
2+
// but for now have to document methods in both places
3+
// like this: import {mount} from './commands'
4+
15
declare namespace Cypress {
26
interface Cypress {
37
stylesCache: any
@@ -10,6 +14,25 @@ declare namespace Cypress {
1014
interface Chainable<Subject = any> {
1115
injectReactDOM: () => Chainable<void>
1216
copyComponentStyles: (component: Symbol) => Chainable<void>
17+
/**
18+
* Mount a React component in a blank document; register it as an alias
19+
* To access: use an alias or original component reference
20+
* @function cy.mount
21+
* @param {Object} jsx - component to mount
22+
* @param {string} [Component] - alias to use later
23+
* @example
24+
```
25+
import Hello from './hello.jsx'
26+
// mount and access by alias
27+
cy.mount(<Hello />, 'Hello')
28+
// using default alias
29+
cy.get('@Component')
30+
// using specified alias
31+
cy.get('@Hello').its('state').should(...)
32+
// using original component
33+
cy.get(Hello)
34+
```
35+
**/
1336
mount: (component: Symbol, alias?: string) => Chainable<void>
1437
get<S = any>(alias: string | symbol | Function, options?: Partial<Loggable & Timeoutable>): Chainable<any>
1538
}

0 commit comments

Comments
 (0)