This repository was 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
feat: add wrapper option to mountHook function #536
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -60,21 +60,39 @@ function TestHook({ callback, onError, children }: TestHookProps) { | |
return null | ||
} | ||
|
||
type MountHookOptions = { | ||
wrapper?: React.ReactElement | ||
} | ||
|
||
/** | ||
* Mounts a React hook function in a test component for testing. | ||
* | ||
* @see https://github.com/bahmutov/cypress-react-unit-test#advanced-examples | ||
*/ | ||
export const mountHook = (hookFn: (...args: any[]) => any) => { | ||
export const mountHook = ( | ||
hookFn: (...args: any[]) => any, | ||
options: MountHookOptions = {}, | ||
) => { | ||
const { result, setValue, setError } = resultContainer() | ||
|
||
return mount( | ||
React.createElement(TestHook, { | ||
callback: hookFn, | ||
onError: setError, | ||
children: setValue, | ||
}), | ||
).then(() => { | ||
const testElement = React.createElement(TestHook, { | ||
callback: hookFn, | ||
onError: setError, | ||
children: setValue, | ||
key: Math.random().toString(), | ||
}) | ||
|
||
let mountElement: any = testElement | ||
if (options.wrapper) { | ||
// what's the proper type? I don't even care anymore | ||
// because types for React seem to be a mess | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not true. React types are better than Vue, and may be better than Angular ! |
||
// @ts-ignore | ||
mountElement = React.createElement(options.wrapper, { | ||
children: [testElement], | ||
}) | ||
} | ||
|
||
return mount(mountElement).then(() => { | ||
cy.wrap(result) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any
=>React.Element