Skip to content

Commit e2b4f08

Browse files
sai6855mj12albert
authored andcommitted
[material-ui][Chip] Fix handling on event handlers (mui#46263)
1 parent 45d1897 commit e2b4f08

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/mui-material/src/Chip/Chip.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,15 +531,15 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
531531
...handlers,
532532
onClick: (event) => {
533533
handlers.onClick?.(event);
534-
onClick(event);
534+
onClick?.(event);
535535
},
536536
onKeyDown: (event) => {
537537
handlers.onKeyDown?.(event);
538-
handleKeyDown(event);
538+
handleKeyDown?.(event);
539539
},
540540
onKeyUp: (event) => {
541541
handlers.onKeyUp?.(event);
542-
handleKeyUp(event);
542+
handleKeyUp?.(event);
543543
},
544544
}),
545545
});

packages/mui-material/src/Chip/Chip.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,4 +733,36 @@ describe('<Chip />', () => {
733733
).not.to.throw();
734734
});
735735
});
736+
737+
it('should not throw on clicking Chip when onClick is not provided', () => {
738+
expect(() => {
739+
const { getByTestId } = render(<Chip data-testid="chip" />);
740+
const chip = getByTestId('chip');
741+
fireEvent.click(chip);
742+
}).not.throw();
743+
});
744+
745+
it('should not throw on keydown when onKeyDown is not provided', () => {
746+
expect(() => {
747+
const { getByTestId } = render(<Chip data-testid="chip" onClick={() => {}} />);
748+
const chip = getByTestId('chip');
749+
act(() => {
750+
chip.focus();
751+
});
752+
753+
fireEvent.keyDown(chip, { key: 'Enter' });
754+
}).not.throw();
755+
});
756+
757+
it('should not throw on keyup when onKeyUp is not provided', () => {
758+
expect(() => {
759+
const { getByTestId } = render(<Chip data-testid="chip" onClick={() => {}} />);
760+
const chip = getByTestId('chip');
761+
act(() => {
762+
chip.focus();
763+
});
764+
765+
fireEvent.keyUp(chip, { key: ' ' });
766+
}).not.throw();
767+
});
736768
});

0 commit comments

Comments
 (0)