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

Commit 010441e

Browse files
committed
add mount hook demo test for #534
1 parent 6481d07 commit 010441e

File tree

8 files changed

+113
-0
lines changed

8 files changed

+113
-0
lines changed

cypress/component/advanced/hooks/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- [counter-with-hooks.spec.js](counter-with-hooks.spec.js) and [counter2-with-hooks.spec.js](counter2-with-hooks.spec.js) test React components that uses hooks
44
- [use-counter.spec.js](use-counter.spec.js) shows how to test a React hook using `mountHook` function
5+
- [custom-hook.mount-spec.js](custom-hook.mount-spec.js) manually creates a wrapper component around a custom hook that uses Redux provider
6+
- [custom-hook.mount-hook-spec.js](custom-hook.mount-hook-spec.js) is skipped for now
57

68
![Hook test](images/hook.png)
79

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const countReducer = function(state = 0, action) {
2+
switch (action.type) {
3+
case 'INCREMENT':
4+
return state + 1
5+
case 'DECREMENT':
6+
return state - 1
7+
default:
8+
return state
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useEffect } from 'react'
2+
import { useDispatch } from 'react-redux'
3+
4+
export function useCustomHook() {
5+
useDispatch()
6+
7+
useEffect(() => {
8+
console.log('hello world!')
9+
}, [])
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mountHook } from 'cypress-react-unit-test'
4+
const { useCustomHook } = require('./custom-hook')
5+
import { Provider } from 'react-redux'
6+
import store from './store'
7+
8+
describe('custom hook that needs redux provider', () => {
9+
it.skip('mounted with wrapper', () => {
10+
mountHook(() => useCustomHook()).then(result => {
11+
console.log(result)
12+
})
13+
})
14+
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
const { useCustomHook } = require('./custom-hook')
5+
import { Provider } from 'react-redux'
6+
import store from './store'
7+
8+
describe('custom hook that needs redux provider', () => {
9+
it('mounts if we make a test component around it', () => {
10+
const App = () => {
11+
useCustomHook()
12+
13+
return <></>
14+
}
15+
cy.spy(console, 'log').as('log')
16+
mount(
17+
<Provider store={store}>
18+
<App />
19+
</Provider>,
20+
)
21+
cy.get('@log').should('have.been.calledWith', 'hello world!')
22+
})
23+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createStore } from 'redux'
2+
import { countReducer } from './count-reducer'
3+
4+
const store = createStore(countReducer)
5+
6+
export default store

package-lock.json

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@
114114
"react-google-maps": "9.4.5",
115115
"react-i18next": "11.7.2",
116116
"react-loading-skeleton": "2.0.1",
117+
"react-redux": "7.2.2",
117118
"react-router": "6.0.0-alpha.1",
118119
"react-router-dom": "6.0.0-alpha.1",
119120
"react-scripts": "3.4.1",
121+
"redux": "4.0.5",
120122
"rollup-plugin-istanbul": "2.0.1",
121123
"semantic-release": "17.2.2",
122124
"standard": "14.3.3",

0 commit comments

Comments
 (0)