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

Commit 779cbfa

Browse files
committed
fix: better mount command log, closes #271
1 parent deb4bc6 commit 779cbfa

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

cypress/component/advanced/tutorial/square-spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ class Square extends React.Component {
1313

1414
render() {
1515
return (
16-
<button className="square" onClick={() => this.setState({ value: 'X' })}>
16+
<button
17+
className="square"
18+
onClick={() => this.setState({ value: this.props.value })}
19+
>
1720
{this.state.value}
1821
</button>
1922
)
2023
}
2124
}
2225

2326
describe('Square', () => {
24-
it('changes value on click', () => {
27+
it.only('changes value on click', () => {
2528
const selector = 'button.square'
26-
mount(<Square />)
29+
mount(<Square value="X" />)
2730
// initially button is blank
2831
cy.get(selector).should('have.text', '')
2932
// but it changes text on click

lib/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
5656
if (options.log !== false) {
5757
logInstance = Cypress.log({
5858
name: 'mount',
59-
message: [`ReactDOM.render(<${displayName} ... />)`],
59+
message: [`<${displayName} ... />`],
6060
})
6161
}
6262
})
@@ -84,15 +84,22 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
8484
key,
8585
}
8686

87-
const CypressTestComponent = reactDomToUse.render(
88-
React.createElement(React.Fragment, props, jsx),
89-
el,
90-
)
87+
const reactComponent = React.createElement(React.Fragment, props, jsx)
88+
const CypressTestComponent = reactDomToUse.render(reactComponent, el)
9189

92-
const logConsoleProps = {
93-
props: jsx.props,
94-
}
9590
if (logInstance) {
91+
const logConsoleProps = {
92+
props: jsx.props,
93+
description: 'Mounts React component',
94+
home: 'https://github.com/bahmutov/cypress-react-unit-test',
95+
}
96+
const componentElement = el.children[0]
97+
98+
if (componentElement) {
99+
// @ts-ignore
100+
logConsoleProps.yielded = reactDomToUse.findDOMNode(componentElement)
101+
}
102+
96103
logInstance.set('consoleProps', () => logConsoleProps)
97104

98105
if (el.children.length) {

0 commit comments

Comments
 (0)