event.currentTarget can't be specified in TestUtils.Simulate
See original GitHub issueSo I have an element that has a listener that uses currentTarget for some stuff and while I was doing some test for it I saw that when using Simulate to trigger an event no matter that you specify the currentTarget it will always be the original element.
There is a dumb example here: http://jsbin.com/sazovikiga/1/edit?js,console,output
If you click in the h1 it will log the target tagName and the currentTarget tagName after doing:
Simulate.keyUp(element, {target: {tagName: 'TARGET'}, currentTarget: {tagName: 'CURRENT'}});
So i was expecting that in the listener if I checked the currentTarget.tagName i would get “CURRENT” but I get the actual element while for target I got the (I think right value of) “TARGET” name instead of the element.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:23
Top Results From Across the Web
How to set HTML5 file type input with ReactTestUtils
I'm trying to figure out how to test this functionality using ReactTestUtils.Simulate but cant figure out how to set the files that should...
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 >Simulate Browser Events in React with React Testing Library
This enables us to assert that event handlers are called and assert correct arguments. We simulate browser events because, often, we don't want ......
Read more >Reactjs – Testing for mouse wheel events – iTecNote
reactjsreactjs-testutils. I've set up a simple function to handle mouse wheel events on a menu component I built. The component works fine, I'm...
Read more >react-addons-test-utils | Yarn - Package Manager
Important: This documentation covers modern versions of Yarn. For 1.x docs, see classic.yarnpkg.com. Yarn.
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
If you are working with Typescript then I recommend using the following actions. (combination of the comments written below)
https://github.com/airbnb/enzyme/issues/218#issuecomment-388481390 https://github.com/airbnb/enzyme/issues/1850#issuecomment-456791477
Thank you for the explanation, it makes a lot of sense.
Unfortunately I am actually using Enzyme (that as far as I know underlying uses the testUtils Simulate) to write the tests and as far as I know it doesn’t allow to mutate the actual dom node.
Even if Simulate doesn’t really change the DOM state of inputs, it still allows to pass custom data to the event.target object with Simulate.change(node, {target:{value:“whatever”}}). Wouldn’t be possible to allow simulate to do the same to currentTarget? That at least would allow to at least test the event handlers, since now it is just impossible in my scenario.
(Ps: I am using currentTarget instead of target in my event handlers mainly because I am using typescript and if I know that I am interested in the events on a html input I can at compile time check event.currentTarget.value in a typesafe manner, since target by definition is dynamic and cannot be known at compiletime. )