question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

event.currentTarget not working when using mount

See original GitHub issue

Current behavior

currentTarget not working when using mount

const wrapper = mount(<input type="text" onChange={(e)=> console.log(e.currentTarget.value)}/>)

wrapper.simulate('change', {currentTarget: {
  value: 'Hello'
}});

currentTarget working when using shallow

const wrapper = shallow(<input type="text" onChange={(e)=> console.log(e.currentTarget.value)}/>)

wrapper.simulate('change', {currentTarget: {
  value: 'Hello'
}});
Hello

Expected behavior

const wrapper = mount(<input type="text" onChange={(e)=> console.log(e.currentTarget.value)}/>)

wrapper.simulate('change', {currentTarget: {
  value: 'Hello'
}});
Hello

Your environment

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.1.15
react 16.6.3
react-dom 16.6.3
react-test-renderer 16.6.3
adapter (below) 1.0.3

Adapter

  • enzyme-adapter-react-16

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
ljharbcommented, Dec 14, 2018

I would suggest avoiding .simulate - it doesn’t faithfully simulate anything. Instead, manually invoke the desired prop - ie, wrapper.prop('onChange')(fakeEvent).

3reactions
wyqydsyqcommented, Dec 30, 2019

@steveHornseyDeveloper triggering the onChange prop is the proper way to do it; if you’re not using something like webdriver to make an actual click, then you’re not getting any value out of implicitly calling onChange by using simulate.

Perhaps if triggering the desired handler from the mounted component’s props is the “proper” way to do it, then this fact should be documented somewhere?

Searching Google for “enzyme trigger event” gives results where every link on the first page tells you to use .simulate(), the first two of which are links to the official Enzyme docs.

Further, all the examples of triggering events in Enzyme’s official docs suggest to use .simulate().

Even just mentioning what you’ve said in this issue thread under the common gotchas of this function would be a lot clearer than users having to dig through threads to find this information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

event.currentTarget.value returning underfined javascript
At the moment whenever I click on a button it returns undefined even though there is text in it (the buttons start off...
Read more >
Event.currentTarget - Web APIs | MDN
The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM.
Read more >
LWC : Events - When to use event.target.value & event.detail ...
currentTarget will give you the reference of the element which fired the event and event.detail will give you the information of data in ......
Read more >
React - how to capture only parent's onClick event and not ...
Since you are binding the handler to th you can use the currentTarget property. ... For some reason the accepted solution did not...
Read more >
currentTarget Event Property - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found