-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[Rating] Fix Rating returns NaN when using user event #45054
base: master
Are you sure you want to change the base?
Conversation
cdf633e
to
b7804d9
Compare
Netlify deploy previewhttps://deploy-preview-45054--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
630b67d
to
3a90194
Compare
3a90194
to
0cf5f9f
Compare
e40d308
to
ce7b44b
Compare
8102bb3
to
6a76a96
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Thanks for the fix and welcome to MUI contributors.
18b5a43
to
da88eea
Compare
if (event.clientX < left || event.clientX > right) { | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this logic needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The userEvent.click
emits a mousemove
event with clientX = 0
, while in Karma, the Rating
's bounding rect has left = 8
. If we don't use this logic, the newHover
would be 1 regardless of where we click. This also not align with real-world scenario, where if you not hovering over the Rating
, it should not trigger the onMouseMove
handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you not hovering over the Rating, it should not trigger the onMouseMove handler.
I didn't understand. Doesn't mousemove
trigger over the rating container when the user clicks the rating? Shouldn't we simulate it in the test for browsers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been trying to find a way to correctly simulate a mousemove
event with the correct event.clientX
but no success so far. Currently, user-event
doesn't seem to support that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am reluctant to have this logic just for a test. Maybe simulate a mousemove
using fireEvent.mouseMove
above user.click
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DiegoAndai if we mock the getBoundingClientRect
, the clientX
from MouseMove
event will still be 0.
Hence, when actually setting the value in handleChange
:
const handleChange = (event) => {
let newValue = event.target.value === '' ? null : parseFloat(event.target.value);
// Give mouse priority over keyboard
// Fix https://github.com/mui/material-ui/issues/22827
if (hover !== -1) {
newValue = hover;
}
setValueState(newValue);
if (onChange) {
onChange(event, newValue);
}
};
The hover
state variable will always be 1 while the newValue
will have the actual value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood.
Again, we shouldn't modify the component to handle a user event limitation. If we can't find a way around it, I prefer to document it and let users know not to use the user event for this instead of modifying the component.
Does this make sense @NooBat?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DiegoAndai From what I understand we should remove this
if (event.clientX < left || event.clientX > right) {
return;
}
and keep this in, correct?
if (!containerWidth) {
// Workaround for test scenario using userEvent since jsdom defaults getBoundingClientRect to 0
// Fix https://github.com/mui/material-ui/issues/38828
return;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I think we shouldn't have this:
if (!containerWidth) {
// Workaround for test scenario using userEvent since jsdom defaults getBoundingClientRect to 0
// Fix https://github.com/mui/material-ui/issues/38828
return;
}
Only to fix a userEvent limitation. If there's no other way around it, I would keep the component as is and add a warning in the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DiegoAndai So we just skip the suggestions from Oliver in #38828?
He did said:
- Consider special handling in the logic if getBoundingClientRect is not available, effectively for tests, we have a couple of those already in the codebase.
feb8d8b
to
a883b85
Compare
8e9bb99
to
f127e2e
Compare
c491e99
to
17fa68b
Compare
90aa943
to
1022460
Compare
b4b4168
to
e53642a
Compare
adfc156
to
fede27a
Compare
fede27a
to
bedd6cc
Compare
Closes #38828