Skip to content

Commit 2331748

Browse files
authored
docs: add docs for toHaveErrorMessage
update test cases to match example
1 parent 43c7be2 commit 2331748

File tree

2 files changed

+82
-31
lines changed

2 files changed

+82
-31
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ clear to read and to maintain.
7575
- [`toBeChecked`](#tobechecked)
7676
- [`toBePartiallyChecked`](#tobepartiallychecked)
7777
- [`toHaveDescription`](#tohavedescription)
78+
- [`toHaveErrorMessage`](#tohaveerrormessage)
7879
- [Deprecated matchers](#deprecated-matchers)
7980
- [`toBeInTheDOM`](#tobeinthedom)
8081
- [Inspiration](#inspiration)
@@ -1042,6 +1043,58 @@ expect(deleteButton).not.toHaveDescription()
10421043
expect(deleteButton).toHaveDescription('') // Missing or empty description always becomes a blank string
10431044
```
10441045

1046+
### `toHaveErrorMessage`
1047+
1048+
```typescript
1049+
toHaveErrorMessage(text: string | RegExp)
1050+
```
1051+
1052+
This allows you to check whether the given element has an
1053+
[ARIA error message](https://www.w3.org/TR/wai-aria/#aria-errormessage) or not.
1054+
1055+
Use the `aria-errormessage` attribute to reference another element that contains
1056+
custom error message text. Multiple ids is **NOT** allowed. Authors MUST use
1057+
`aria-invalid` in conjunction with `aria-errormessage`. Leran more from
1058+
[`aria-errormessage` spec](https://www.w3.org/TR/wai-aria/#aria-errormessage).
1059+
1060+
Whitespace is normalized.
1061+
1062+
When a `string` argument is passed through, it will perform a whole
1063+
case-sensitive match to the error message text.
1064+
1065+
To perform a case-insensitive match, you can use a `RegExp` with the `/i`
1066+
modifier.
1067+
1068+
To perform a partial match, you can pass a `RegExp` or use
1069+
`expect.stringContaining("partial string")`.
1070+
1071+
#### Examples
1072+
1073+
```html
1074+
<label for="startTime"> Please enter a start time for the meeting: </label>
1075+
<input
1076+
id="startTime"
1077+
type="text"
1078+
aria-errormessage="msgID"
1079+
aria-invalid="true"
1080+
value="11:30 PM"
1081+
/>
1082+
<span id="msgID" aria-live="assertive" style="visibility:visible">
1083+
Invalid time: the time must be between 9:00 AM and 5:00 PM"
1084+
</span>
1085+
```
1086+
1087+
```javascript
1088+
const timeInput = getByLabel('startTime')
1089+
1090+
expect(timeInput).toHaveErrorMessage(
1091+
'Invalid time: the time must be between 9:00 AM and 5:00 PM',
1092+
)
1093+
expect(timeInput).toHaveErrorMessage(/invalid time/i) // to partially match
1094+
expect(timeInput).toHaveErrorMessage(expect.stringContaining('Invalid time')) // to partially match
1095+
expect(timeInput).not.toHaveErrorMessage('Pikachu!')
1096+
```
1097+
10451098
## Deprecated matchers
10461099

10471100
### `toBeInTheDOM`

src/__tests__/to-have-errormessage.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,40 @@ import {render} from './helpers/test-utils'
44
describe('.toHaveErrorMessage', () => {
55
test('resolves for object with correct aria-errormessage reference', () => {
66
const {queryByTestId} = render(`
7-
<div id="errormessage">The errormessage</div>
8-
<div data-testid="invalid" aria-errormessage="errormessage" aria-invalid="true"></div>
9-
<div data-testid="implicitly_invalid" aria-errormessage="errormessage" aria-invalid></div>
7+
<label for="startTime"> Please enter a start time for the meeting: </label>
8+
<input data-testid="startTime" type="text" aria-errormessage="msgID" aria-invalid="true" value="11:30 PM" >
9+
<span id="msgID" aria-live="assertive" style="visibility:visible"> Invalid time: the time must be between 9:00 AM and 5:00 PM </span>
1010
`)
1111

12-
// element with aria-invalid="true"
13-
expect(queryByTestId('invalid')).toHaveErrorMessage('The errormessage')
14-
expect(queryByTestId('invalid')).toHaveErrorMessage(
15-
expect.stringContaining('The'),
16-
)
17-
expect(queryByTestId('invalid')).toHaveErrorMessage(/The/)
18-
expect(queryByTestId('invalid')).toHaveErrorMessage(
19-
expect.stringMatching(/The/),
20-
)
21-
expect(queryByTestId('invalid')).toHaveErrorMessage(/errormessage/)
22-
expect(queryByTestId('invalid')).not.toHaveErrorMessage('Something else')
23-
expect(queryByTestId('invalid')).not.toHaveErrorMessage('The')
12+
const timeInput = queryByTestId('startTime')
2413

25-
// element with aria-invalid attribute
26-
expect(queryByTestId('implicitly_invalid')).toHaveErrorMessage(
27-
'The errormessage',
28-
)
29-
expect(queryByTestId('implicitly_invalid')).toHaveErrorMessage(
30-
expect.stringContaining('The'),
31-
)
32-
expect(queryByTestId('implicitly_invalid')).toHaveErrorMessage(/The/)
33-
expect(queryByTestId('implicitly_invalid')).toHaveErrorMessage(
34-
expect.stringMatching(/The/),
35-
)
36-
expect(queryByTestId('implicitly_invalid')).toHaveErrorMessage(
37-
/errormessage/,
14+
expect(timeInput).toHaveErrorMessage(
15+
'Invalid time: the time must be between 9:00 AM and 5:00 PM',
3816
)
39-
expect(queryByTestId('implicitly_invalid')).not.toHaveErrorMessage(
40-
'Something else',
17+
expect(timeInput).toHaveErrorMessage(/invalid time/i) // to partially match
18+
expect(timeInput).toHaveErrorMessage(
19+
expect.stringContaining('Invalid time'),
20+
) // to partially match
21+
expect(timeInput).not.toHaveErrorMessage('Pikachu!')
22+
})
23+
24+
test('works correctly on implicit invalid element', () => {
25+
const {queryByTestId} = render(`
26+
<label for="startTime"> Please enter a start time for the meeting: </label>
27+
<input data-testid="startTime" type="text" aria-errormessage="msgID" aria-invalid value="11:30 PM" >
28+
<span id="msgID" aria-live="assertive" style="visibility:visible"> Invalid time: the time must be between 9:00 AM and 5:00 PM </span>
29+
`)
30+
31+
const timeInput = queryByTestId('startTime')
32+
33+
expect(timeInput).toHaveErrorMessage(
34+
'Invalid time: the time must be between 9:00 AM and 5:00 PM',
4135
)
42-
expect(queryByTestId('implicitly_invalid')).not.toHaveErrorMessage('The')
36+
expect(timeInput).toHaveErrorMessage(/invalid time/i) // to partially match
37+
expect(timeInput).toHaveErrorMessage(
38+
expect.stringContaining('Invalid time'),
39+
) // to partially match
40+
expect(timeInput).not.toHaveErrorMessage('Pikachu!')
4341
})
4442

4543
test('rejects for valid object', () => {

0 commit comments

Comments
 (0)