This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add wrapper option to mountHook function (#536)
- Loading branch information
Showing
10 changed files
with
157 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const countReducer = function(state = 0, action) { | ||
switch (action.type) { | ||
case 'INCREMENT': | ||
return state + 1 | ||
case 'DECREMENT': | ||
return state - 1 | ||
default: | ||
return state | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useEffect } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
|
||
export function useCustomHook() { | ||
useDispatch() | ||
|
||
useEffect(() => { | ||
console.log('hello world!') | ||
}, []) | ||
} |
20 changes: 20 additions & 0 deletions
20
cypress/component/advanced/hooks/custom-hook.mount-hook-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// <reference types="cypress" /> | ||
import React from 'react' | ||
import { mountHook } from 'cypress-react-unit-test' | ||
const { useCustomHook } = require('./custom-hook') | ||
import { Provider } from 'react-redux' | ||
import store from './store' | ||
|
||
describe('custom hook that needs redux provider', () => { | ||
it('mounted with wrapper', () => { | ||
const wrapper = ({ children }) => ( | ||
<Provider store={store}>{children}</Provider> | ||
) | ||
|
||
cy.spy(console, 'log').as('log') | ||
mountHook(() => useCustomHook(), { wrapper }) | ||
|
||
// make sure the custom hook calls "useEffect" | ||
cy.get('@log').should('have.been.calledWith', 'hello world!') | ||
}) | ||
}) |
23 changes: 23 additions & 0 deletions
23
cypress/component/advanced/hooks/custom-hook.mount-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/// <reference types="cypress" /> | ||
import React from 'react' | ||
import { mount } from 'cypress-react-unit-test' | ||
const { useCustomHook } = require('./custom-hook') | ||
import { Provider } from 'react-redux' | ||
import store from './store' | ||
|
||
describe('custom hook that needs redux provider', () => { | ||
it('mounts if we make a test component around it', () => { | ||
const App = () => { | ||
useCustomHook() | ||
|
||
return <></> | ||
} | ||
cy.spy(console, 'log').as('log') | ||
mount( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider>, | ||
) | ||
cy.get('@log').should('have.been.calledWith', 'hello world!') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createStore } from 'redux' | ||
import { countReducer } from './count-reducer' | ||
|
||
const store = createStore(countReducer) | ||
|
||
export default store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters