Skip to content

Commit

Permalink
Test click not setProps
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Apr 23, 2021
1 parent a1e5c24 commit bacce0c
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions packages/material-ui/src/internal/SwitchBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,29 @@ describe('<SwitchBase />', () => {
});

it('should call onChange when controlled', () => {
const checked = true;
const handleChange = spy();
const { getByRole, setProps } = render(
<SwitchBase
icon="unchecked"
checkedIcon="checked"
type="checkbox"
checked={checked}
onChange={handleChange}
/>,
);
const defaultChecked = true;
function ControlledSwichBase() {
const [checked, setChecked] = React.useState(defaultChecked);

getByRole('checkbox').click();
setProps({ checked: false });
return (
<SwitchBase
icon="unchecked"
checkedIcon="checked"
type="checkbox"
checked={checked}
onChange={(event) => setChecked(event.target.checked)}
/>
);
}

expect(handleChange.callCount).to.equal(1);
expect(handleChange.firstCall.args[0].target.checked).to.equal(!checked);
const { getByRole } = render(<ControlledSwichBase />);
const checkbox = getByRole('checkbox');

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

expect(checkbox).to.have.property('checked', !defaultChecked);
});

it('should not change checkbox state when event is default prevented', () => {
Expand Down

0 comments on commit bacce0c

Please sign in to comment.