In my component that I want to test I use useLocation
hook.
In the component I have:
function Test() {
const history = useHistory();
const location = useLocation();
const query = new URLSearchParams(location.search).get('value');
return <div > Welcome < /div>
}
To test the component I wrote this test:
jest.mock('react-router-dom', () => ({
useLocation: jest.fn().mockReturnValue({
pathname: '',
search: 'value',
hash: '',
state: null,
key: '5nvxpbdafa',
}),
}));
jest.mock('react-router-dom', () => ({
useHistory: () => jest.fn().mockReturnValue({
push: jest.fn(),
}),
}));
describe(' page test', () => {
test('should render', () => {
render(<Test />);
const title = screen.getByText(/welcome/i);
expect(title).toBeInTheDocument();
});
})
Trying this I get TypeError: (0 , _reactRouterDom.useLocation) is not a function
. Why do I get this error? How to write the test correctly to avoid the error?