react: dispatchEvent on input/textarea is ignored
Do you want to request a feature or report a bug? bug
What is the current behavior?
The dispatchEvent method has no effect on input/textarea elements.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/84v837e9/). v. 15.5.4: https://jsfiddle.net/c8tp5mqf/ (working) v. 15.6.1: https://jsfiddle.net/6bv1581z/ (not working)
What is the expected behavior?
The dispatchEvent method results in an event being handled.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? I’m using the latest Chrome browser. This has worked in 15.5.x, stopped working in 15.6.0 and is still not working in 15.6.1.
Usecase for this: some old tests that I’d happily remove, but have to support for now.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 31 (9 by maintainers)
Commits related to this issue
- Added AdminCredentials component & fixed TextInput component clear state Implemented the AdminCredentials component and copied its ErrorResponse component styling to the Login component, which approp... — committed to gmattie/Ballot-Box by gmattie 4 years ago
- Fixed multiplier changing if grade is changed Coursemology uses React and simply changing the value attribute of the multiplier input will not change Coursemology's state. As such, every time an onCh... — committed to purfectliterature/gradeassist by purfectliterature 4 years ago
Just leaving a solution for future reference (checked in Edge 15, IE 11, FF 53, Chrome 59):
Use it like so:
I’m not sure why I missed this or why this is, but it looks like there actually is a value setter on the prototype but there isn’t always one on the element. So here’s my adjusted version that works:
Good luck friends. Stay safe out there!
Found another issue talking about this that proposes an alternative solution:
https://github.com/facebook/react/issues/11488#issuecomment-347775628
Usage:
Is there a similar workaround if its a contentEditable div?
Yes, i also left that bit off because its a bit hard to explain, but React tracks manual DOMNode.value changes as well, so when you do
input.value ='foo'and then dispatch a change event React sees two discreet moments there, the first change and then the change via the event, so when your input event fires the value is still'foo'and React says “I already know about that that value so this must be a duplicate”It shouldn’t, if you are using something like Selenium, which fires “real” events when you ask it too, vs “fake” ones we can fire in the DOM. Comparative tools like Cypress or nightmare, etc should use workarounds that mimic the real browsers behavior so if something breaks (as in this case) it was more of a bug in Cypress 😃
I just tried the
_valueTrackersolution and it does trigger theonChangehandler. But I get caret position jump.Here is my use case:
// I need to replace it with a "." (point)onKeyDowndoesevent.preventDefault()onKeyDownideally would fire a KeyBoard event that behaves exactly like if the user had typed “.” (point), so that I don’t get caret jumping issues.Is this by any means possible?
Because if I’m going to deal with caret jumping anyway, I can always call my
onChangefunction directly from myonKeyDown.What I did so far:
the code next is worked for me.
I’ll need some time to process the use case in cypress (esp. the part about the setters - don’t really know what the spec says about this), so I’m not strongly opinionated for now.
In any case, the
simulatedthing seems to be working for now and the code that’s using it waits for a rewrite, so that might be turned into a non-issue for me.I’m just wondering - won’t this affect the ability to make integration tests for React-based apps using the DOM api?
Sorry if that’s all answered in the linked thread, I have yet to connect the dots.
hi. I know has been updated but i have this problem.
On Tue, Feb 13, 2024 at 2:48 AM Peter Briggs @.***> wrote:
Oh, kinda forgot about this issue, but now that I’m here I thought I’d mention that the
changeevent in DOM Testing Library’sfireEventhas this built-in 😃Yes this is expected and unavoidable to some extent. React dedupe change and input events so they don’t fire too often, in this case even tho you intentionally want to trigger the event react is swallowing it because it the input value has not changed. you can use the SimulateNative helper to get the behavior your looking for. Tho that will break again in v16