Skip to content

Commit 14d1630

Browse files
authoredJun 11, 2020
feat: [toBeValid/toBeInvalid] Check aria-invalid on any element (#259)
1 parent 159a7a2 commit 14d1630

File tree

3 files changed

+83
-15
lines changed

3 files changed

+83
-15
lines changed
 

‎README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,9 @@ expect(
274274
toBeInvalid()
275275
```
276276

277-
This allows you to check if a form element, or the entire `form`, is currently
278-
invalid.
277+
This allows you to check if an element, is currently invalid.
279278

280-
An `input`, `select`, `textarea`, or `form` element is invalid if it has an
279+
An element is invalid if it has an
281280
[`aria-invalid` attribute](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-invalid_attribute)
282281
with no value or a value of `"true"`, or if the result of
283282
[`checkValidity()`](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation)
@@ -359,14 +358,13 @@ expect(getByTestId('supported-role-aria')).toBeRequired()
359358
toBeValid()
360359
```
361360

362-
This allows you to check if the value of a form element, or the entire `form`,
363-
is currently valid.
361+
This allows you to check if the value of an element, is currently valid.
364362

365-
An `input`, `select`, `textarea`, or `form` element is valid if it has no
366-
[`aria-invalid` attribute](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-invalid_attribute)
363+
An element is valid if it has no
364+
[`aria-invalid` attribute](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-invalid_attribute)s
367365
or an attribute value of `"false"`. The result of
368366
[`checkValidity()`](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation)
369-
must also be `true`.
367+
must also be `true` if it's a form element.
370368

371369
#### Examples
372370

@@ -1162,6 +1160,7 @@ Thanks goes to these people ([emoji key][emojis]):
11621160

11631161
<!-- markdownlint-enable -->
11641162
<!-- prettier-ignore-end -->
1163+
11651164
<!-- ALL-CONTRIBUTORS-LIST:END -->
11661165

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

‎src/__tests__/to-be-invalid.js

+64
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,38 @@ describe('.toBeInvalid', () => {
8080
expect(() => expect(queryByTestId('valid')).toBeInvalid()).toThrowError()
8181
expect(() => expect(invalidFormNode).not.toBeInvalid()).toThrowError()
8282
})
83+
84+
test('handles any element', () => {
85+
const {queryByTestId} = render(`
86+
<ol data-testid="valid">
87+
<li data-testid="no-aria-invalid" > </li>
88+
<li data-testid="aria-invalid" aria-invalid> </li>
89+
<li data-testid="aria-invalid-value" aria-invalid="true"> </li>
90+
<li data-testid="aria-invalid-false" aria-invalid="false"> </li>
91+
</ol>
92+
`)
93+
94+
expect(queryByTestId('valid')).not.toBeInvalid()
95+
expect(queryByTestId('no-aria-invalid')).not.toBeInvalid()
96+
expect(queryByTestId('aria-invalid')).toBeInvalid()
97+
expect(queryByTestId('aria-invalid-value')).toBeInvalid()
98+
expect(queryByTestId('aria-invalid-false')).not.toBeInvalid()
99+
100+
// negative test cases wrapped in throwError assertions for coverage.
101+
expect(() => expect(queryByTestId('valid')).toBeInvalid()).toThrowError()
102+
expect(() =>
103+
expect(queryByTestId('no-aria-invalid')).toBeInvalid(),
104+
).toThrowError()
105+
expect(() =>
106+
expect(queryByTestId('aria-invalid')).not.toBeInvalid(),
107+
).toThrowError()
108+
expect(() =>
109+
expect(queryByTestId('aria-invalid-value')).not.toBeInvalid(),
110+
).toThrowError()
111+
expect(() =>
112+
expect(queryByTestId('aria-invalid-false')).toBeInvalid(),
113+
).toThrowError()
114+
})
83115
})
84116

85117
describe('.toBeValid', () => {
@@ -129,4 +161,36 @@ describe('.toBeValid', () => {
129161
expect(() => expect(queryByTestId('valid')).not.toBeValid()).toThrowError()
130162
expect(() => expect(invalidFormNode).toBeValid()).toThrowError()
131163
})
164+
165+
test('handles any element', () => {
166+
const {queryByTestId} = render(`
167+
<ol data-testid="valid">
168+
<li data-testid="no-aria-invalid" > </li>
169+
<li data-testid="aria-invalid" aria-invalid> </li>
170+
<li data-testid="aria-invalid-value" aria-invalid="true"> </li>
171+
<li data-testid="aria-invalid-false" aria-invalid="false"> </li>
172+
</ol>
173+
`)
174+
175+
expect(queryByTestId('valid')).toBeValid()
176+
expect(queryByTestId('no-aria-invalid')).toBeValid()
177+
expect(queryByTestId('aria-invalid')).not.toBeValid()
178+
expect(queryByTestId('aria-invalid-value')).not.toBeValid()
179+
expect(queryByTestId('aria-invalid-false')).toBeValid()
180+
181+
// negative test cases wrapped in throwError assertions for coverage.
182+
expect(() => expect(queryByTestId('valid')).not.toBeValid()).toThrowError()
183+
expect(() =>
184+
expect(queryByTestId('no-aria-invalid')).not.toBeValid(),
185+
).toThrowError()
186+
expect(() =>
187+
expect(queryByTestId('aria-invalid')).toBeValid(),
188+
).toThrowError()
189+
expect(() =>
190+
expect(queryByTestId('aria-invalid-value')).toBeValid(),
191+
).toThrowError()
192+
expect(() =>
193+
expect(queryByTestId('aria-invalid-false')).not.toBeValid(),
194+
).toThrowError()
195+
})
132196
})

‎src/to-be-invalid.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ function isElementHavingAriaInvalid(element) {
1010
)
1111
}
1212

13+
function isSupportsValidityMethod(element) {
14+
return FORM_TAGS.includes(getTag(element))
15+
}
16+
1317
function isElementInvalid(element) {
14-
return !element.checkValidity()
18+
const isHaveAriaInvalid = isElementHavingAriaInvalid(element)
19+
if (isSupportsValidityMethod(element)) {
20+
return isHaveAriaInvalid || !element.checkValidity()
21+
} else {
22+
return isHaveAriaInvalid
23+
}
1524
}
1625

1726
export function toBeInvalid(element) {
1827
checkHtmlElement(element, toBeInvalid, this)
1928

20-
const isInvalid =
21-
isElementHavingAriaInvalid(element) || isElementInvalid(element)
29+
const isInvalid = isElementInvalid(element)
2230

2331
return {
2432
pass: isInvalid,
@@ -37,10 +45,7 @@ export function toBeInvalid(element) {
3745
export function toBeValid(element) {
3846
checkHtmlElement(element, toBeValid, this)
3947

40-
const isValid =
41-
!isElementHavingAriaInvalid(element) &&
42-
FORM_TAGS.includes(getTag(element)) &&
43-
!isElementInvalid(element)
48+
const isValid = !isElementInvalid(element)
4449

4550
return {
4651
pass: isValid,

0 commit comments

Comments
 (0)
Please sign in to comment.