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

Switch: Completely remove aria-checked from switch, set id on input to be the same as label, replace brittle unit test with more precise e2e test #2455

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ describe('modus-switch', () => {
const input = await page.find('modus-switch >>> input');
expect(await modusSwitch.getProperty('checked')).toBeTruthy();
expect(await input.getProperty('checked')).toBeTruthy();
expect(await input.getAttribute('aria-checked').toLowerCase()).toEqual('true');

await element.click();
await page.waitForChanges();

expect(await modusSwitch.getProperty('checked')).toBeFalsy();
expect(await input.getProperty('checked')).toBeFalsy();
expect(await input.getAttribute('aria-checked').toLowerCase()).toEqual('false');
});
it('renders with medium size', async () => {
const page = await newE2EPage();
Expand All @@ -118,4 +116,30 @@ describe('modus-switch', () => {
const element = await page.find('modus-switch >>> .modus-switch');
expect(element).toHaveClass('small');
});

it('does not include "id" on input when "label" is not provided', async () => {
const page = await newE2EPage();
await page.setContent('<modus-switch></modus-switch>');

const element = await page.find('modus-switch >>> input');
expect(element).not.toHaveAttribute('id');
});

it('renders "id" on input when "label" is provided', async () => {
const page = await newE2EPage();
await page.setContent('<modus-switch label="test label"></modus-switch>');

const element = await page.find('modus-switch >>> input');
expect(element).toHaveAttribute('id');
expect(element.id).toEqual('test label');
});

it('sets tabindex to -1 when disabled', async () => {
const page = await newE2EPage();
await page.setContent('<modus-switch disabled></modus-switch>');

const element = await page.find('modus-switch >>> .modus-switch');
expect(element).toHaveAttribute('tabindex');
expect(element.getAttribute('tabindex')).toEqual('-1');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,7 @@ describe('modus-switch', () => {
<div class="switch">
<span class="slider"></span>
</div>
<input aria-checked="false" role="switch" type="checkbox">
</div>
</mock:shadow-root>
</modus-switch>
`);
});

it('sets tabindex to -1 when disabled', async () => {
const page = await newSpecPage({
components: [ModusSwitch],
html: '<modus-switch disabled></modus-switch>',
});
expect(page.root).toEqualHtml(`
<modus-switch disabled>
<mock:shadow-root>
<div class="disabled medium modus-switch" tabindex="-1">
<div class="switch">
<span class="slider"></span>
</div>
<input aria-checked="false" role="switch" aria-disabled="true" disabled type="checkbox">
<input role="switch" type="checkbox">
</div>
</mock:shadow-root>
</modus-switch>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export class ModusSwitch {
<span class={`slider`}></span>
</div>
<input
aria-checked={String(this.checked)}
aria-disabled={this.disabled ? 'true' : undefined}
aria-label={this.ariaLabel}
checked={this.checked}
disabled={this.disabled}
id={this.label}
ref={(el) => (this.checkboxInput = el as HTMLInputElement)}
role="switch"
type="checkbox"></input>
Expand Down