I have a system providing a members only section. I’d like to test if, when an unauthorized user opens a URL from the member section, they get forwarded to the login page.
In my test I render the whole application. From there I’d like to trigger a location change. How would I do that?
test('Unauthorized, open member page', () = {
// run
render(<App />);
// TODO: change location to "/members/profile"
// check
expect(window.location.href).toBe('http://localhost/login');
});
2
Answers
A protected route would solve the problem in these cases, if user is not Authorized it will redirect it to login page.
you can nevigate to another page by import useHistory from react-router-dom