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

Commit ef0704a

Browse files
committed
feat: spy on window.alert close #6
1 parent 10c0eb3 commit ef0704a

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
- [ ] some DOM events are not working when running all tests at once [#4](https://github.com/bahmutov/cypress-react-unit-test/issues/4)
1414
- [x] cannot mock server XHR for injected components [#5](https://github.com/bahmutov/cypress-react-unit-test/issues/5)
15-
- [ ] cannot spy on `window.alert` [#6](https://github.com/bahmutov/cypress-react-unit-test/issues/6)
15+
- [x] cannot spy on `window.alert` [#6](https://github.com/bahmutov/cypress-react-unit-test/issues/6)
1616

1717
## Install
1818

@@ -116,6 +116,7 @@ All components are in [src](src) folder. All tests are in [cypress/integration](
116116
* [stateless-spec.js](cypress/integration/stateless-spec.js) shows testing a stateless component from [stateless.jsx](src/stateless.jsx)
117117
* [transpiled-spec.js](cypress/integration/stateless-spec.js) shows testing a component with class properties syntax from [transpiled.jsx](src/stateless.jsx)
118118
* [users-spec.js](cypress/integration/users-spec.js) shows how to observe XHR requests, mock server responses for component [users.jsx](src/users.jsx)
119+
* [alert-spec.js](cypress/integration/alert-spec.js) shows how to spy on `window.alert` calls from your component [stateless-alert.jsx](src/stateless-alert.jsx)
119120

120121
## Large examples
121122

cypress/integration/alert-spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('Stateless alert', () => {
77
beforeEach(() => {
88
const spy = cy.spy().as('alert')
99
cy.on('window:alert', spy)
10-
mount(<HelloWorld name="Alert" />)
10+
mount(<HelloWorld name='Alert' />)
1111
})
1212

1313
it('shows link', () => {
@@ -18,6 +18,6 @@ describe('Stateless alert', () => {
1818
cy.contains('Say Hi').click()
1919
// spy on window:alert is never called
2020
// https://github.com/bahmutov/cypress-react-unit-test/issues/6
21-
// cy.get('@alert').should('have.been.calledOnce')
21+
cy.get('@alert').should('have.been.calledOnce')
2222
})
2323
})

lib/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ function setXMLHttpRequest (w) {
4343
return w
4444
}
4545

46+
function setAlert (w) {
47+
window.alert = w.alert
48+
return w
49+
}
50+
4651
/* eslint-env mocha */
4752
export const mount = jsx => {
4853
// include React and ReactDOM from CDN to force DOM to register all DOM event listeners
@@ -61,6 +66,7 @@ export const mount = jsx => {
6166

6267
cy.window({log: false})
6368
.then(setXMLHttpRequest)
69+
.then(setAlert)
6470
.its('ReactDOM.render')
6571
.then(render => {
6672
Cypress._component = render(jsx, document.getElementById('app'))

0 commit comments

Comments
 (0)