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

[material-ui][SwitchBase] Fix spreading of handlers in getSlotProps #45197

Merged
merged 8 commits into from
Feb 4, 2025
1 change: 1 addition & 0 deletions packages/mui-material/src/internal/SwitchBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ const SwitchBase = React.forwardRef(function SwitchBase(props, ref) {
className: classes.input,
externalForwardedProps,
getSlotProps: (handlers) => ({
...handlers,
onChange: (event) => {
handlers.onChange?.(event);
handleInputChange(event);
Expand Down
27 changes: 27 additions & 0 deletions packages/mui-material/src/internal/SwitchBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,31 @@ describe('<SwitchBase />', () => {
expect(getByRole('checkbox')).to.have.property('value', 'red');
});
});

it('should call event handlers in slotProps when provided', () => {
const rootOnClick = spy();
const inputOnClick = spy();
const { getByRole } = render(
<SwitchBase
icon="unchecked"
checkedIcon="checked"
type="checkbox"
slotProps={{
root: {
onClick: rootOnClick,
},
input: {
onClick: inputOnClick,
},
}}
/>,
);

act(() => {
getByRole('checkbox').click();
});

expect(rootOnClick.callCount).to.equal(1);
expect(inputOnClick.callCount).to.equal(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is failing in master branch as onClick handler wasn't spread in getSlotProps and it's passing in this PR

});
});