Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

NooBat
Copy link
Contributor

@NooBat NooBat commented Jan 18, 2025

Closes #38828

@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch from cdf633e to b7804d9 Compare January 18, 2025 06:26
@zannager zannager added the component: rating This is the name of the generic UI component, not the React module! label Jan 20, 2025
@zannager zannager requested a review from sai6855 January 20, 2025 16:16
@ZeeshanTamboli ZeeshanTamboli changed the title [fix] Fix Rating returns NaN when using user event [Rating] Fix Rating returns NaN when using user event Jan 23, 2025
@mui-bot
Copy link

mui-bot commented Jan 23, 2025

Netlify deploy preview

https://deploy-preview-45054--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 6eba2a4

@ZeeshanTamboli ZeeshanTamboli added the package: material-ui Specific to @mui/material label Jan 23, 2025
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch from 630b67d to 3a90194 Compare January 27, 2025 11:41
@NooBat NooBat requested a review from siriwatknp January 28, 2025 13:04
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch from 3a90194 to 0cf5f9f Compare January 30, 2025 03:39
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch from e40d308 to ce7b44b Compare January 30, 2025 11:12
@NooBat NooBat requested a review from ZeeshanTamboli January 30, 2025 11:33
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch 3 times, most recently from 8102bb3 to 6a76a96 Compare January 31, 2025 05:27
Copy link
Member

@siriwatknp siriwatknp left a 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.

@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch from 18b5a43 to da88eea Compare January 31, 2025 07:17
Comment on lines 401 to 431
if (event.clientX < left || event.clientX > right) {
return;
}
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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;
}

Copy link
Member

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.

Copy link
Contributor Author

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:

  1. Consider special handling in the logic if getBoundingClientRect is not available, effectively for tests, we have a couple of those already in the codebase.

@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch from feb8d8b to a883b85 Compare February 3, 2025 10:22
@NooBat NooBat requested a review from ZeeshanTamboli February 3, 2025 10:22
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch 2 times, most recently from 8e9bb99 to f127e2e Compare February 4, 2025 03:19
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch 5 times, most recently from c491e99 to 17fa68b Compare February 6, 2025 08:30
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch 4 times, most recently from 90aa943 to 1022460 Compare February 14, 2025 12:08
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch 3 times, most recently from b4b4168 to e53642a Compare February 21, 2025 11:11
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch 2 times, most recently from adfc156 to fede27a Compare February 25, 2025 13:25
@NooBat NooBat force-pushed the fix/rating-test-with-user-event branch from fede27a to bedd6cc Compare February 25, 2025 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: rating This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[docs][Rating] Testing the component with @testing-library/user-event results in NaN
6 participants