Griffin M
Griffin M

Reputation: 461

Testing react redirect

I have a react component which can result in one of two redirects being rendered, such as:

render () {
  if(condition) {
    return <Redirect to={'page1'} />
  } else {
    return <Redirect to={'page2'} />
  }
}

In the enzyme test, I have found out how to test that a redirect is being rendered:

expect(page.find('Redirect').length).toEqual(1)

However I am not sure how to configure the test so that I can test which of the redirects is being rendered. According to the Enzyme find docs it is not possible to use find to locate a tag along with an attribute, which leads me to believe I am going about this in the wrong way.

Upvotes: 3

Views: 2679

Answers (1)

Steve Vaughan
Steve Vaughan

Reputation: 2189

You should be able to do the following:

expect(page.containsMatchingElement(<Redirect to="page1" />)).toEqual(true)

Which would allow you to assert that the correct redirect is rendered under the correct circumstance

Alternatively, if there are more props being added to the component when it is mounted, you could always do something like:

page.find('Redirect').debug()

And match the attribute manually with RegExp.

Upvotes: 4

Related Questions