Skip to content

Commit

Permalink
[material-ui][SwitchBase] Fix spreading of handlers in getSlotProps (
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 authored Feb 4, 2025
1 parent 1fb6063 commit 9074d47
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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);
});
});

0 comments on commit 9074d47

Please sign in to comment.