Skip to content

Commit 144c647

Browse files
authored
feat: add switch role to toBeChecked (#228)
1 parent 69681df commit 144c647

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ clear to read and to maintain.
4646
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
4747
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
4848

49-
5049
- [Installation](#installation)
5150
- [Usage](#usage)
5251
- [Custom matchers](#custom-matchers)
@@ -776,8 +775,9 @@ toBeChecked()
776775
```
777776

778777
This allows you to check whether the given element is checked. It accepts an
779-
`input` of type `checkbox` or `radio` and elements with a `role` of `checkbox`
780-
or `radio` with a valid `aria-checked` attribute of `"true"` or `"false"`.
778+
`input` of type `checkbox` or `radio` and elements with a `role` of `checkbox`,
779+
`radio` or `switch` with a valid `aria-checked` attribute of `"true"` or
780+
`"false"`.
781781

782782
#### Examples
783783

@@ -795,6 +795,8 @@ or `radio` with a valid `aria-checked` attribute of `"true"` or `"false"`.
795795
<input type="radio" value="foo" data-testid="input-radio-unchecked" />
796796
<div role="radio" aria-checked="true" data-testid="aria-radio-checked" />
797797
<div role="radio" aria-checked="false" data-testid="aria-radio-unchecked" />
798+
<div role="switch" aria-checked="true" data-testid="aria-switch-checked" />
799+
<div role="switch" aria-checked="false" data-testid="aria-switch-unchecked" />
798800
```
799801

800802
```javascript
@@ -815,6 +817,11 @@ expect(inputRadioChecked).toBeChecked()
815817
expect(inputRadioUnchecked).not.toBeChecked()
816818
expect(ariaRadioChecked).toBeChecked()
817819
expect(ariaRadioUnchecked).not.toBeChecked()
820+
821+
const ariaSwitchChecked = getByTestId('aria-switch-checked')
822+
const ariaSwitchUnchecked = getByTestId('aria-switch-unchecked')
823+
expect(ariaSwitchChecked).toBeChecked()
824+
expect(ariaSwitchUnchecked).not.toBeChecked()
818825
```
819826

820827
## Deprecated matchers
@@ -867,8 +874,8 @@ I'm not aware of any, if you are please [make a pull request][prs] and add it
867874
here!
868875

869876
If you would like to further test the accessibility and validity of the DOM
870-
consider [`jest-axe`](https://github.com/nickcolley/jest-axe). It doesn't
871-
overlap with `jest-dom` but can complement it for more in-depth accessibility
877+
consider [`jest-axe`](https://github.com/nickcolley/jest-axe). It doesn't
878+
overlap with `jest-dom` but can complement it for more in-depth accessibility
872879
checking (eg: validating `aria` attributes or ensuring unique id attributes).
873880

874881
## Guiding Principles
@@ -951,6 +958,7 @@ Thanks goes to these people ([emoji key][emojis]):
951958

952959
<!-- markdownlint-enable -->
953960
<!-- prettier-ignore-end -->
961+
954962
<!-- ALL-CONTRIBUTORS-LIST:END -->
955963

956964
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/to-be-checked.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ describe('.toBeChecked', () => {
4141
expect(queryByTestId('aria-radio-unchecked')).not.toBeChecked()
4242
})
4343

44+
test('handles element with role="switch"', () => {
45+
const {queryByTestId} = render(`
46+
<div role="switch" aria-checked="true" data-testid="aria-switch-checked" />
47+
<div role="switch" aria-checked="false" data-testid="aria-switch-unchecked" />
48+
`)
49+
50+
expect(queryByTestId('aria-switch-checked')).toBeChecked()
51+
expect(queryByTestId('aria-switch-unchecked')).not.toBeChecked()
52+
})
53+
4454
test('throws when checkbox input is checked but expected not to be', () => {
4555
const {queryByTestId} = render(
4656
`<input type="checkbox" checked data-testid="input-checked" />`,
@@ -121,6 +131,26 @@ describe('.toBeChecked', () => {
121131
).toThrowError()
122132
})
123133

134+
test('throws when element with role="switch" is checked but expected not to be', () => {
135+
const {queryByTestId} = render(
136+
`<div role="switch" aria-checked="true" data-testid="aria-switch-checked" />`,
137+
)
138+
139+
expect(() =>
140+
expect(queryByTestId('aria-switch-checked')).not.toBeChecked(),
141+
).toThrowError()
142+
})
143+
144+
test('throws when element with role="switch" is not checked but expected to be', () => {
145+
const {queryByTestId} = render(
146+
`<div role="switch" aria-checked="false" data-testid="aria-switch-unchecked" />`,
147+
)
148+
149+
expect(() =>
150+
expect(queryByTestId('aria-switch-unchecked')).toBeChecked(),
151+
).toThrowError()
152+
})
153+
124154
test('throws when element with role="checkbox" has an invalid aria-checked attribute', () => {
125155
const {queryByTestId} = render(
126156
`<div role="checkbox" aria-checked="something" data-testid="aria-checkbox-invalid" />`,
@@ -129,7 +159,7 @@ describe('.toBeChecked', () => {
129159
expect(() =>
130160
expect(queryByTestId('aria-checkbox-invalid')).toBeChecked(),
131161
).toThrowError(
132-
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox" or role="radio" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
162+
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
133163
)
134164
})
135165

@@ -141,14 +171,26 @@ describe('.toBeChecked', () => {
141171
expect(() =>
142172
expect(queryByTestId('aria-radio-invalid')).toBeChecked(),
143173
).toThrowError(
144-
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox" or role="radio" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
174+
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
175+
)
176+
})
177+
178+
test('throws when element with role="switch" has an invalid aria-checked attribute', () => {
179+
const {queryByTestId} = render(
180+
`<div role="switch" aria-checked="something" data-testid="aria-switch-invalid" />`,
181+
)
182+
183+
expect(() =>
184+
expect(queryByTestId('aria-switch-invalid')).toBeChecked(),
185+
).toThrowError(
186+
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
145187
)
146188
})
147189

148190
test('throws when the element is not an input', () => {
149191
const {queryByTestId} = render(`<select data-testid="select"></select>`)
150192
expect(() => expect(queryByTestId('select')).toBeChecked()).toThrowError(
151-
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox" or role="radio" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
193+
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
152194
)
153195
})
154196
})

src/to-be-checked.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function toBeChecked(element) {
1313

1414
const isValidAriaElement = () => {
1515
return (
16-
['checkbox', 'radio'].includes(element.getAttribute('role')) &&
16+
['checkbox', 'radio', 'switch'].includes(element.getAttribute('role')) &&
1717
['true', 'false'].includes(element.getAttribute('aria-checked'))
1818
)
1919
}
@@ -22,7 +22,7 @@ export function toBeChecked(element) {
2222
return {
2323
pass: false,
2424
message: () =>
25-
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox" or role="radio" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
25+
'only inputs with type="checkbox" or type="radio" or elements with role="checkbox", role="radio" or role="switch" and a valid aria-checked attribute can be used with .toBeChecked(). Use .toHaveValue() instead',
2626
}
2727
}
2828

0 commit comments

Comments
 (0)