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

Commit 1b8feb5

Browse files
committed
feat: use Cypress.component() to get the mounted component
1 parent 65b54ba commit 1b8feb5

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ describe('HelloState component', () => {
1414
// mount the component under test
1515
mount(<HelloState />)
1616
// start testing!
17-
cy.contains('Hello Spider-man!').then(() => {
18-
// mounted component is at Cypress.component
19-
Cypress.component.setState({ name: 'React' })
17+
cy.contains('Hello Spider-man!')
18+
// mounted component is returned from Cypress.component()
19+
Cypress.component().invoke('setState', {name: 'React'})
20+
Cypress.component().its('state').should('deep.equal', {
21+
name: 'React'
2022
})
23+
// check if GUI has rerendered
2124
cy.contains('Hello React!')
2225
})
2326
})
@@ -32,4 +35,5 @@ All components are in [src](src) folder. All tests are in [cypress/integration](
3235
* [hello-world-spec.js](cypress/integration/hello-world-spec.js) - testing the simplest React component from [hello-world.jsx](src/hello-world.jsx)
3336
* [hello-x-spec.js](cypress/integration/hello-x-spec.js) - testing React component with props and state [hello-x.jsx](src/hello-x.jsx)
3437
* [counter-spec.js](cypress/integration/counter-spec.js) clicks on the component and confirms the result
38+
* [stateless-spec.js](cypress/integration/stateless-spec.js) shows testing a stateless component from [stateless.jsx](src/stateless.jsx)
3539
* separate repo [bahmutov/calculator](https://github.com/bahmutov/calculator) tests multiple components

cypress/integration/hello-x-spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ describe('HelloState component', () => {
1515
mount(<HelloState />)
1616
cy.contains('Hello Spider-man!')
1717
Cypress.component().invoke('setState', {name: 'React'})
18+
Cypress.component().its('state').should('deep.equal', {
19+
name: 'React'
20+
})
1821
cy.contains('Hello React!')
1922
})
2023
})

cypress/integration/stateless-spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import HelloWorld from '../../src/stateless.jsx'
2+
import React from 'react'
3+
import { mount } from '../../lib'
4+
5+
/* eslint-env mocha */
6+
describe('Stateless component', () => {
7+
beforeEach(() => {
8+
// pass spy and save it under an alias
9+
// so we can easily get it later with cy.get('@greeting')
10+
const spy = cy.spy().as('greeting')
11+
mount(<HelloWorld name="Test Aficionado" click={spy} />)
12+
})
13+
14+
it('shows link', () => {
15+
cy.contains('a', 'Say Hi')
16+
})
17+
18+
it('alerts with name', () => {
19+
cy.contains('Say Hi').click()
20+
cy.get('@greeting').should('be.calledWith', 'Hi Test Aficionado')
21+
})
22+
})

src/stateless.jsx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
3+
// example stateless component from
4+
// https://hackernoon.com/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc
5+
const HelloWorld = ({name, click}) => {
6+
7+
return (
8+
<div>
9+
<a href="#"
10+
onClick={click(`Hi ${name}`)}>Say Hi</a>
11+
</div>
12+
)
13+
}
14+
15+
export default HelloWorld

0 commit comments

Comments
 (0)