event.currentTarget not working when using mount
See original GitHub issueCurrent 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:
- Created 5 years ago
- Reactions:4
- Comments:11 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I would suggest avoiding
.simulate
- it doesn’t faithfully simulate anything. Instead, manually invoke the desired prop - ie,wrapper.prop('onChange')(fakeEvent)
.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.