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

Commit aac981e

Browse files
committed
fix: update alias and jsdoc, closes #283
1 parent 0fca723 commit aac981e

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Look at the examples in [cypress/component](cypress/component) folder. Here is t
111111
<!-- prettier-ignore-start -->
112112
Spec | Description
113113
--- | ---
114+
[alias](cypress/component/basic/alias) | Retrieve mounted component by its name or alias
114115
[alert-spec.js](cypress/component/basic/alert-spec.js) | Component tries to use `window.alert`
115116
[counter-set-state](cypress/component/basic/counter-set-state) | Counter component that uses `this.state`
116117
[counter-use-hooks](cypress/component/basic/counter-use-hooks) | Counter component that uses `useState` hook
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# alias
2+
3+
You can retrieve the created component using default constructor function name or an alias. See [alias-spec.js](alias-spec.js) for examples.
4+
5+
![Alias tests](images/alias.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
5+
describe('Alias', () => {
6+
it('returns component by its name', () => {
7+
const Greeting = () => <div>Hello!</div>
8+
mount(<Greeting />)
9+
// get the component instance by name "Greeting"
10+
cy.get('@Greeting')
11+
.its('props')
12+
.should('be.empty')
13+
// the component was constructed from the function Greeting
14+
cy.get('@Greeting')
15+
.its('type')
16+
.should('equal', Greeting)
17+
})
18+
19+
it('returns component by given display name', () => {
20+
const GreetingCard = props => <div>Hello {props.name}!</div>
21+
mount(<GreetingCard name="World" />, { alias: 'Hello' })
22+
cy.get('@Hello')
23+
.its('props')
24+
.should('deep.equal', {
25+
name: 'World',
26+
})
27+
})
28+
})
329 KB
Loading

lib/index.ts

+27-17
Original file line numberDiff line numberDiff line change
@@ -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
**/
4648
export 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

Comments
 (0)