enzyme icon indicating copy to clipboard operation
enzyme copied to clipboard

simulate('click') issue with react-router Link

Open mulleady1 opened this issue 10 years ago • 8 comments

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?

mulleady1 avatar Jul 27 '16 18:07 mulleady1

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?

blainekasten avatar Aug 01 '16 16:08 blainekasten

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 avatar Aug 01 '16 17:08 mulleady1

@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.

blainekasten avatar Aug 01 '16 18:08 blainekasten

Fair enough. Thanks for the feedback!

mulleady1 avatar Aug 01 '16 18:08 mulleady1

This is probably a common enough case to warrant an example in the docs.

mjhm avatar Jun 11 '17 12:06 mjhm

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?

Thanks for this. It really helped.

shridharkalagi avatar Jul 15 '19 09:07 shridharkalagi

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.

ctiller15 avatar Sep 05 '20 23:09 ctiller15

@mulleady1 thumbs up thanks for the post!

santaclauze avatar Sep 08 '20 21:09 santaclauze