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

[base-ui][Switch] Add missing role attribute #40907

Merged
merged 9 commits into from
Feb 14, 2024
10 changes: 5 additions & 5 deletions packages/mui-base/src/useSwitch/useSwitch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('useSwitch', () => {
render(<Switch />);

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

expect(handleChange.callCount).to.equal(1);
Expand All @@ -85,11 +85,11 @@ describe('useSwitch', () => {
return <input {...getInputProps()} />;
}
render(<Switch />);
const checkbox = screen.getByRole('checkbox');
const switchElement = screen.getByRole('switch');

simulatePointerDevice();
act(() => {
checkbox.focus();
switchElement.focus();
});

expect(handleBlur.callCount).to.equal(0);
Expand All @@ -99,7 +99,7 @@ describe('useSwitch', () => {
);

act(() => {
checkbox.blur();
switchElement.blur();
});

expect(handleBlur.callCount).to.equal(1);
Expand All @@ -108,7 +108,7 @@ describe('useSwitch', () => {
programmaticFocusTriggersFocusVisible() ? 1 : 0,
);

focusVisible(checkbox);
focusVisible(switchElement);

expect(handleBlur.callCount).to.equal(1);
expect(handleFocus.callCount).to.equal(2);
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-base/src/useSwitch/useSwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export function useSwitch(props: UseSwitchParameters): UseSwitchReturnValue {
ref: handleInputRef,
required,
type: 'checkbox',
role: 'switch',
'aria-checked': checkedProp,
...otherProps,
onChange: createHandleInputChange(otherProps),
onFocus: createHandleFocus(otherProps),
Expand Down
1 change: 1 addition & 0 deletions packages/mui-joy/src/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ const Checkbox = React.forwardRef(function Checkbox(inProps, ref) {
name,
value,
readOnly,
role: undefined,
required: required ?? formControl?.required,
'aria-describedby': formControl?.['aria-describedby'],
...(indeterminate && {
Expand Down
1 change: 1 addition & 0 deletions packages/mui-joy/src/Radio/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ const Radio = React.forwardRef(function Radio(inProps, ref) {
const [SlotInput, inputProps] = useSlot('input', {
additionalProps: {
type: 'radio',
role: undefined,
id,
name,
readOnly,
Expand Down
26 changes: 13 additions & 13 deletions packages/mui-joy/src/Switch/Switch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,40 +72,40 @@ describe('<Switch />', () => {
expect(switchComponent.childNodes[0]).to.have.class(classes.track);
});

it('renders a `role="checkbox"` with the Unchecked state by default', () => {
it('renders a `role="switch"` with the Off state by default', () => {
const { getByRole } = render(<Switch />);

expect(getByRole('checkbox')).to.have.property('checked', false);
expect(getByRole('switch')).to.have.property('checked', false);
});

it('renders a checkbox with the Checked state when checked', () => {
it('renders a switch with the Checked state when On', () => {
const { getByRole } = render(<Switch defaultChecked />);

expect(getByRole('checkbox')).to.have.property('checked', true);
expect(getByRole('switch')).to.have.property('checked', true);
});

it('the switch can be disabled', () => {
const { getByRole } = render(<Switch disabled />);

expect(getByRole('checkbox')).to.have.property('disabled', true);
expect(getByRole('switch')).to.have.property('disabled', true);
});

it('the switch can be readonly', () => {
const { getByRole } = render(<Switch readOnly />);

expect(getByRole('checkbox')).to.have.property('readOnly', true);
expect(getByRole('switch')).to.have.property('readOnly', true);
});

it('the Checked state changes after change events', () => {
const { getByRole } = render(<Switch defaultChecked />);

// how a user would trigger it
act(() => {
getByRole('checkbox').click();
fireEvent.change(getByRole('checkbox'), { target: { checked: '' } });
getByRole('switch').click();
fireEvent.change(getByRole('switch'), { target: { checked: '' } });
});

expect(getByRole('checkbox')).to.have.property('checked', false);
expect(getByRole('switch')).to.have.property('checked', false);
});

describe('decorator', () => {
Expand All @@ -130,8 +130,8 @@ describe('<Switch />', () => {

// how a user would trigger it
act(() => {
getByRole('checkbox').click();
fireEvent.change(getByRole('checkbox'), { target: { checked: '' } });
getByRole('switch').click();
fireEvent.change(getByRole('switch'), { target: { checked: '' } });
});

expect(getByText('On')).toBeVisible();
Expand All @@ -146,8 +146,8 @@ describe('<Switch />', () => {

// how a user would trigger it
act(() => {
getByRole('checkbox').click();
fireEvent.change(getByRole('checkbox'), { target: { checked: '' } });
getByRole('switch').click();
fireEvent.change(getByRole('switch'), { target: { checked: '' } });
});

expect(getByText('On')).toBeVisible();
Expand Down
Loading