I have a custom hook, which has structure of:
const urlHook = () => {
const router = useRouter();
const read = () => {
return validate(router.query.param);
}
const write = (params) => {
router.push(
{
query: {
param: params,
},
},
undefined,
{shallow: true},
)
}
const validate = (params) => {}
}
I want to test this hook using react-hooks-testing-library
but I'm not sure how to setup for router.query.param
to read values that I want or how to check if function write()
will create correct url?
To mock entire hook -
jest.requireActual
:To mock history/routing state - MemoryRouter:
Helpful example with explanations: https://v5.reactrouter.com/web/guides/testing