Consider the following input element in a React component:
<input onChange={() => console.log('onChange')} ... />
While testing the React component, I'm emulating user changing the input value:
input.value = newValue;
TestUtils.Simulate.change(input);
This causes 'onChange'
to be logged, as expected.
However, when the 'change'
event is dispatched directly (I'm using jsdom):
input.value = newValue;
input.dispatchEvent(new Event('change'));
the onChange
handler is not called.
Why?
My motivation to use dispatchEvent
rather than TestUtils.Simulate
is because TestUtils.Simulate
doesn't support event bubbling and my component's behavior relies on that. I wonder whether there is a way to test events without TestUtils.Simulate
?
value
? If I want to click a button, should that be added toconst input
orconst input.value
? And so I tried just setting it toconst input
, and got thatreactTriggerChange is undefined
, so I added an Import statement. Then gotCannot read property 'toLowerCase' of undefined
. Plus I have a ton of linting errors I would have to clean up to use this package :( . – Eliaseliason