simulate('click') issue with react-router Link
First off, thank you airbnb team for this awesome library!
Sorry if this was brought up already. I imagine someone has come across this issue before but my search yielded zero results.
I have react-router doing routing and rendering Link components for me. In my tests, I do things like:
const wrapper = mount(
<Router history={createMemoryHistory('/')}
<Route path="/" component={ComponentA}>
<IndexRoute component={ComponentB} />
<Route path="path/to/c" component={ComponentC} />
</Route>
</Router>
)
Then after I do a wrapper.find('a').first().simulate('click'), which I know is called on an <a> tag that comes from a Link component that links to ComponentC, I expect to find ComponentC in the new render output. However, this was not occurring.
I eventually discovered that the route change was not taking place because in Link's render function, there's a check for event.button === 0. This check fails with enzyme if you don't call simulate() correctly, with simulate('click', { button: 0 }).
I guess it's not a big deal to manually pass { button: 0 }, but doesn't this seem like a reasonable default?
Sorry if I misunderstood exactly what your aim here is, but it sounds like you got your test to work? and are you just asking that we could maybe default { button: 0 } for all simulate calls?
Thanks for the response. Sorry for being unclear in my original post. I wrote it primarily as an FYI for anyone having the same issue, and secondarily as a conversation starter for possibly adding { button: 0 } as a default for simulate('click') calls.
I figured the FYI portion was worth posting given how popular both Enzyme and React Router are. Just hoping to save somebody some time in the future!
@mulleady1 Thanks so much! It's definitely helpful to have backlog of issues that people can search. Hopefully it helps others!
secondarily as a conversation starter for possibly adding { button: 0 } as a default for simulate('click') calls.
We've had requests to default arguments to the simulated callback. Ultimately we always lean away from them as it is generally not safe for everyone's tests.
Fair enough. Thanks for the feedback!
This is probably a common enough case to warrant an example in the docs.
First off, thank you airbnb team for this awesome library!
Sorry if this was brought up already. I imagine someone has come across this issue before but my search yielded zero results.
I have react-router doing routing and rendering Link components for me. In my tests, I do things like:
const wrapper = mount( <Router history={createMemoryHistory('/')} <Route path="/" component={ComponentA}> <IndexRoute component={ComponentB} /> <Route path="path/to/c" component={ComponentC} /> </Route> </Router> )Then after I do a
wrapper.find('a').first().simulate('click'), which I know is called on an<a>tag that comes from a Link component that links to ComponentC, I expect to find ComponentC in the new render output. However, this was not occurring.I eventually discovered that the route change was not taking place because in Link's render function, there's a check for
event.button === 0. This check fails with enzyme if you don't call simulate() correctly, withsimulate('click', { button: 0 }).I guess it's not a big deal to manually pass
{ button: 0 }, but doesn't this seem like a reasonable default?
Thanks for this. It really helped.
Just want to add that this helped me out as well. It would definitely be worth noting in an obvious spot under the simulate examples.
@mulleady1 thumbs up thanks for the post!