JavaDeveloper
JavaDeveloper

Reputation: 5660

How to test window.location.assign using react wrapper?

I have a react component which navigates to a different pages based on the value of somecondition. For example:

if (somecondition) {
 window.location.assign('some link')}
}

How would I test this somecondition using enzyme wrapper ?

Upvotes: 1

Views: 662

Answers (1)

user3142695
user3142695

Reputation: 17332

Using enzyme and jest, your test could look like this:

test('should redirect', () => {
  // SETUP
  window.location.assign = jest.fn()

  // EXECUTE
  const wrapper = shallow(<Component {...props} />)
  wrapper.instance().callYourFunction()

  // VERIFY
  expect(window.location.assign).toHaveBeenCalled()
  window.location.assign.mockClear()
})

Upvotes: 2

Related Questions