Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions packages/ui-checkbox/src/Checkbox/__tests__/Checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,37 @@ describe('<Checkbox />', () => {
expect(input).not.toHaveAttribute('aria-invalid')
})

describe('`toggle` variant', () => {
it('should expose a button role and pressed state', () => {
renderCheckbox({ variant: 'toggle', defaultChecked: true })
const toggle = screen.getByRole('button')

expect(toggle).toHaveAttribute('aria-pressed', 'true')
expect(screen.queryByRole('checkbox')).not.toBeInTheDocument()
})

it('should update the pressed state when toggled', async () => {
renderCheckbox({ variant: 'toggle', defaultChecked: false })
const toggle = screen.getByRole('button')

expect(toggle).toHaveAttribute('aria-pressed', 'false')

await userEvent.click(toggle)

await waitFor(() => {
expect(toggle).toHaveAttribute('aria-pressed', 'true')
})
})

it('should leave the `simple` variant as a checkbox', () => {
renderCheckbox({ variant: 'simple' })
const input = screen.getByRole('checkbox')

expect(input).not.toHaveAttribute('role')
expect(input).not.toHaveAttribute('aria-pressed')
})
})

describe('for a11y', () => {
it('`simple` variant should meet standards', async () => {
const { container } = renderCheckbox({ variant: 'simple' })
Expand Down
2 changes: 2 additions & 0 deletions packages/ui-checkbox/src/Checkbox/v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
readOnly={readOnly}
aria-readonly={readOnly ? true : undefined}
aria-checked={indeterminate ? 'mixed' : undefined}
role={variant === 'toggle' ? 'button' : undefined}
aria-pressed={variant === 'toggle' ? this.checked : undefined}
aria-invalid={this.invalid ? 'true' : undefined}
// Keep messages in the description so the accessible name contains only the label.
aria-labelledby={
Expand Down
Loading