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

Commit 8b97e63

Browse files
committed
chore: add unit test for alert spy for #6
1 parent 22d2e73 commit 8b97e63

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

cypress/integration/alert-spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import HelloWorld from '../../src/stateless-alert.jsx'
2+
import React from 'react'
3+
import { mount } from '../../lib'
4+
5+
/* eslint-env mocha */
6+
describe('Stateless alert', () => {
7+
beforeEach(() => {
8+
const spy = cy.spy().as('alert')
9+
cy.on('window:alert', spy)
10+
mount(<HelloWorld name="Alert" />)
11+
})
12+
13+
it('shows link', () => {
14+
cy.contains('a', 'Say Hi')
15+
})
16+
17+
it('alerts with name', () => {
18+
cy.contains('Say Hi').click()
19+
// spy on window:alert is never called
20+
// https://github.com/bahmutov/cypress-react-unit-test/issues/6
21+
// cy.get('@alert').should('have.been.calledOnce')
22+
})
23+
})

src/stateless-alert.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
6+
/* global alert */
7+
const HelloWorld = ({name, click}) => {
8+
const sayHi = () => {
9+
console.log('about to alert')
10+
alert(`Hi ${name}`)
11+
}
12+
13+
return (
14+
<div>
15+
<a href="#"
16+
onClick={sayHi}>Say Hi</a>
17+
</div>
18+
)
19+
}
20+
21+
export default HelloWorld

src/stateless.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ const HelloWorld = ({name, click}) => {
1212
)
1313
}
1414

15-
export default HelloWorld
15+
export default HelloWorld

0 commit comments

Comments
 (0)