-
Notifications
You must be signed in to change notification settings - Fork 457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fireEvent.click() performs click on disabled input field #92
Comments
fireEvent.click()
performs click on disabled input field
Interesting. I wonder what the browser does and if this may be a bug in jsdom 🤔 |
Can you try to run your code in the browser and see if dispatching a click event on a disabled button in the browser will call the click handler or not? |
Here is the code that I was trying. After spending some time now, I'm unclear about where the problem is. |
Huh, weird. In addition, in codesandbox it's running in the browser rather than using jsdom... Hmm... |
* docs: update examples to be more atomic * docs: add SavePointSam to contributors list
If anyone can come up with what's going on here and a solution to the problem then please write here and we can re-open, but I'm going to go ahead and close this for now. |
I got the same problem.
I tried to run my code in the browser, the event handler is not called. |
Can you please make a codesandbox of that? |
@kentcdodds Thanks for your quickly response. |
I have just pushed the code on the repo. Please take a few minutes to check it out. The same code on the Codesanbox, but it's failed. |
@ngtan Updated your codesandbox https://codesandbox.io/s/naughty-smoke-ckqh6?fontsize=14 Removed the index.js and just used a pure test.js so it doesn't try to locate and render to a DOM element that doesn't exist. The test shows the problem exists and disabled inputs can and will receive a click event! |
I have encountered a form of this and almost thought it was a bug in jsdom, but it was here all along 😇 The trick here is that real user interaction clicks are required to always go into the user interaction task source first, and then it gets completely discarded if it turns out the target is a The problem, though, is that there's no "user interaction task source" in a headless library like JSDOM. Perhaps it's something that can be done at the JSDOM itself ignores it, but an option is to have |
Thanks for your investigation @Jessidhia! I'm hesitant to do much more in Another option is to just encode this logic in |
Leaning towards that because of all the other focus handling and stuff that's enabled there |
I'd like to hear more about why people want to write a text that clicks on a disabled input. What are you trying to test in this case? Is it not enough to simply assert that the input is disabled and trust the browser to do its job with that? I kinda think that trying to click it and asserting that the handler isn't called is basically testing the browser, not your code 🤔 |
I suppose the disabled state could be considered an implementation detail - for example you could just remap the click handler to a no-op function, and unless there are significant accessibility issues with that, it's a valid approach. |
I believe there are. And if you remap the click handler to a no-op function then you actually do want the click event to call it so you can test that :) |
I'll check if this works if you use testing-library in a real browser. It should work as expected though. We can probably close this issue and fix it upstream. |
Confirmed that a browser ignores This must be handled by the environment. If you want to follow progress on this bug subscribe to jsdom/jsdom#2665. |
I have a case where I want to prevent rage-clicking on a button that causes side-effects. Wouldn't this be a case of "The more your tests resemble the way your software is used, the more confidence they can give you.", too? |
dom-testing-library
version: 3.4.0react
version: -NA-node
version: 9.11.1npm
(oryarn
) version: -NA-Relevant code or config:
fireEvent.click()
on disabled elements.What you did:
I was expecting
fireEvent.click()
not to have dispatchedclick
event ondisabled
elements.What happened:
fireEvent
is ignoring thedisabled
attribute on the element.Reproduction:
The following test case
events.js
is failing.Problem description:
Currently, the
fireEvent
is ignoring thedisabled
attribute in elements.Suggested solution:
fireEvent
can check whether the element hasdisabled
attribute and skip dispatching the event.However I'm not sure whether to ignore all MouseEvents (like Chrome/Safari) or just ignore
click
events.The text was updated successfully, but these errors were encountered: