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 can't be specified in TestUtils.Simulate

See original GitHub issue

So 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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:23

github_iconTop GitHub Comments

7reactions
Joshua-rosecommented, May 6, 2019

If you are working with Typescript then I recommend using the following actions. (combination of the comments written below)

const inputElement = input.getDOMNode() as HTMLInputElement;
inputElement.value = "another_new_value";
input.update();
input.simulate("change");

https://github.com/airbnb/enzyme/issues/218#issuecomment-388481390 https://github.com/airbnb/enzyme/issues/1850#issuecomment-456791477

5reactions
MastroLinduscommented, Oct 24, 2016

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

Read more comments on GitHub >

github_iconTop 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 >

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