I use useNavigate
from react-router-dom
to handle navigation. I called a useNavigate
hook at the top level of the functional component like that: const navigate = useNavigate()
. Inside a handleClick()
function I use it to navigate to an another page of my website like that: navigate("/home")
. I tried it and it is working fine, if I click on the button it takes me to homepage (http://localhost/home). However I have problems unit testing this functionality. After simulating a click on that button, the url should change so I wrote the following expectation: expect(global.window.location.href).toContain('/home');
The test fails, the window location did not change. However when I tried to console log the window location in the afterEach()
method, it shows that after the test the global.window.location.href
equals to http://localhost/home
. So it is working just can`t verify that in my test....
Why it is only change after the test? What can I do to verify that url inside the test?