4

I am writing unit tests with Jest and Enzyme. I have a button on a 404 page which should redirect a user to the websites homepage. I want to write a test to make sure this button is redirecting a user to the homepage regardless of the URL. In other words the test should pass if the button click takes a user to the homepage and should not rely on the href.

So far I have the code to click my button which should reroute the user.

it('redirects when HOMEPAGE button is clicked', () => {
    const wrapper = mount(<PageNotFound />);
    const link = wrapper.find('#page-not-found-link').first();
    link.simulate('click');
    expect()
})

When the button is clicked we should be routed to <HomePage /> so I have tried adding in:

it('redirects when HOMEPAGE button is clicked', () => {
    const wrapper = mount(<PageNotFound />);
    const newComp = mount (<HomePage />);
    const link = wrapper.find('#page-not-found-link').first();
    const buttonClick = link.simulate('click');
    expect(buttonClick).toEqual(newComp)
})

It seems like I need to compare <HomePage /> with the result of clicking the button but I haven't been able to find a working solution yet.

tdammon
  • 412
  • 3
  • 18
  • I don't recommend that you unit test this functionality. It should be safe to assume that a browser will conform to conventions and that clicking an anchor will redirect to the provided url in the href. To test more than that starts to become more of an integration or end to end test. – dethstrobe Oct 10 '19 at 16:54

0 Answers0