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

Commit 65b54ba

Browse files
committed
feat: working components, close #3
1 parent 06e003b commit 65b54ba

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

cypress.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"viewportWidth": 300,
3-
"viewportHeight": 100
3+
"viewportHeight": 100,
4+
"port": 3456
45
}

cypress/integration/counter-spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,36 @@ describe('Counter', () => {
1212
.click()
1313
.contains('count: 2')
1414
})
15+
16+
it('counts clicks 2', () => {
17+
mount(<Counter />)
18+
cy.contains('count: 0')
19+
.click()
20+
.contains('count: 1')
21+
.click()
22+
.contains('count: 2')
23+
})
24+
})
25+
26+
describe('Counter mounted before each test', () => {
27+
beforeEach(() => {
28+
mount(<Counter />)
29+
})
30+
31+
it('goes to 3', () => {
32+
cy.contains('count: 0')
33+
.click()
34+
.click()
35+
.click()
36+
.contains('count: 3')
37+
})
38+
39+
it('has count in state', () => {
40+
cy.contains('count: 0')
41+
.click()
42+
.click()
43+
.click()
44+
Cypress.component().its('state')
45+
.should('deep.equal', {count: 3})
46+
})
1547
})

cypress/integration/hello-x-spec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ describe('HelloX component', () => {
1111
})
1212

1313
describe('HelloState component', () => {
14-
it('works', () => {
14+
it('changes state', () => {
1515
mount(<HelloState />)
16-
cy.contains('Hello Spider-man!').then(() => {
17-
Cypress.component.setState({ name: 'React' })
18-
})
16+
cy.contains('Hello Spider-man!')
17+
Cypress.component().invoke('setState', {name: 'React'})
1918
cy.contains('Hello React!')
2019
})
2120
})

lib/index.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { render } from 'react-dom'
2-
31
// having weak reference to styles prevents garbage collection
42
// and "losing" styles when the next test starts
53
const stylesCache = new Map()
@@ -39,13 +37,27 @@ const copyStyles = component => {
3937

4038
/* eslint-env mocha */
4139
export const mount = jsx => {
42-
const html = '<body><div id="app"></div></body>'
40+
// include React and ReactDOM from CDN to force DOM to register all DOM event listeners
41+
// otherwise the component will NOT be able to dispatch any events
42+
// when it runs the second time
43+
// https://github.com/bahmutov/cypress-react-unit-test/issues/3
44+
const html = `<body>
45+
<div id="app"></div>
46+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.development.js"></script>
47+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.development.js"></script>
48+
</body>`
4349

4450
const document = cy.state('document')
4551
document.write(html)
4652
document.close()
4753

48-
Cypress.component = render(jsx, document.getElementById('app'))
54+
cy.window({ log: false})
55+
.its('ReactDOM.render')
56+
.then(render => {
57+
Cypress._component = render(jsx, document.getElementById('app'))
58+
Cypress.component = () =>
59+
cy.then(() => Cypress._component)
60+
})
4961

5062
copyStyles(jsx)
5163
}

0 commit comments

Comments
 (0)