-
Notifications
You must be signed in to change notification settings - Fork 1.1k
onChange is called when checkbox is disabled #275
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
Comments
Confirmed! This is pretty odd! I'm not sure what it could be. Anyone wanna dig into this one? Pretty good learning opportunity here :) |
short answerYou should use examplehttps://codesandbox.io/s/j22y6pl21y source/**
* SECTION: handle `click` event
*/
function shouldUseClickEvent(elem) {
// Use the `click` event to detect changes to checkbox and radio inputs.
// This approach works across all browsers, whereas `change` does not fire
// until `blur` in IE8.
var nodeName = elem.nodeName;
return nodeName && nodeName.toLowerCase() === 'input' && (elem.type === 'checkbox' || elem.type === 'radio');
} |
Thanks @puemos, but the fact is that the tests function differently from when you do this in an app. You definitely should be able to use onChange like this and fire a click event. Something else is up 🤔 |
I don't know if this is a problem, but either for Could it be a problem with the jsdom? |
@kentcdodds My feeling is there is a problem with firEvent of |
This is interesting. I created a vanilla JavaScript version of this: https://codesandbox.io/s/q70qj31m46 const app = document.getElementById('app')
app.innerHTML = ''
const input = document.createElement('input')
input.setAttribute('type', 'checkbox')
input.setAttribute('disabled', 'true')
let clickCalled = false
input.addEventListener('click', () => {
clickCalled = true
})
let changeCalled = false
input.addEventListener('change', () => {
changeCalled = true
})
let inputCalled = false
input.addEventListener('input', () => {
inputCalled = true
})
app.appendChild(input)
// this is basically what fireEvent.click does
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
button: 0,
})
input.dispatchEvent(event) The result is that the I'm guessing this has something to do with a strange specification or something. If someone wants to investigate this further then I'd welcome help there... I'm afraid I don't have time to dedicate to this problem at the moment. |
It looks like this may be a case where the spec was followed a little too closely. According to the Spec:
In the vanilla JS example, we can see that it is in fact not dispatching the click event, but dispatching the remaining events. My hypothesis is that browsers are doing some special handling for dispatching click events as opposed to other events in order to control ad clicking (I may be totally off basis here though I'm no expert). This is based entirely off of this method: Which has this line that immediately returns if it's a form element that is disabled: So yeah...my guess is that the right way to handle this is to make sure we are more closely simulating what the browser appears to be doing, and swallowing the click event if the element is disabled. |
But that would be a change for react right? Not react-testing-library |
I don't think so. Since React is just proxying the event, they don't do anything around calling My guess is that we need to do something here: https://github.com/kentcdodds/dom-testing-library/blob/master/src/events.js#L310 To account for the click event on ignored elements, and just swallow the event. This will mimic what the browser is doing under the hood (and I'm guessing the feature we would get if we could actually click a button rather than just dispatching the click event). Thoughts? |
I think it will make more sense when I see a pull request. Anyone willing to open that up? |
I'll work on it!
…On Tue, Feb 19, 2019, 2:37 PM Kent C. Dodds ***@***.***> wrote:
I think it will make more sense when I see a pull request. Anyone willing
to open that up?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#275 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADw5WQaWqaXZsNbMRT4HlCr3AM3Z_a7Uks5vPGCJgaJpZM4aPLjm>
.
|
This is handled by user-event. |
Hi 👋 Thanks for the great library! Think I may have found a bug but if not I would love to know what I'm doing wrong here.
react-testing-library
version: 5.4.4react
version: 16.7.0node
version: 10.15.0npm
(oryarn
) version: yarn 1.10.1Relevant code or config:
What you did:
I wrote a test to check that a checkbox
onChange
function would not be called if the checkbox is disabled.What happened:
The test failed saying that the
onChange
function had been called.Reproduction:
https://codesandbox.io/s/91684p354
Problem description:
The test should pass.
onChange
functions should not be called in the test if the checkbox is disabled.Suggested solution:
Not sure, haven't looked into the code yet.
The text was updated successfully, but these errors were encountered: