I'm trying to test a component that use useRouter from next/router hook to get the route, but I'm getting the following error even mock RouterContext:
Error: Uncaught [TypeError: (0 , _router.useRouter) is not a function]
my application use redux and it's also being mocked with react-redux Provider and mockStore.
This is my util code to render my component:
const getCustomWrapper =
(router, initialState) => {
const Wrapper = ({ children }) => {
const store = mockStore({
...initialStateModel,
...initialState
})
return (
<Provider store={store}>
<RouterContext.Provider value={{ ...mockRouter, ...router }}>
{children}
</RouterContext.Provider>
</Provider>
)
}
Wrapper.propTypes = {
children: PropTypes.node
}
return Wrapper
}
const customRender = async (ui, { options, router, initialState } = {}) => {
const Wrapper = getCustomWrapper(router, initialState)
return render(<Wrapper>{ui}</Wrapper>, options)
}
And my tests are smth like that:
const createComponent = (props = defaultProp, initialState = defaultInitialState) => {
customRender(<MyComponet {...props} />, { initialState })
}
it('should render my text', async () => {
createComponent()
const myText = screen.getByText('my text')
expect(myText).toBeInTheDocument()
})
Versions
next: 10.1.2,
react-redux: 7.2.5,
react: 16.13.1
updates it's not a option right now =/
thx in advance