-
Notifications
You must be signed in to change notification settings - Fork 379
/
to-have-accessible-errormessage.js
277 lines (225 loc) · 10.3 KB
/
to-have-accessible-errormessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import {render} from './helpers/test-utils'
describe('.toHaveAccessibleErrorMessage', () => {
const input = 'input'
const errorId = 'error-id'
const error = 'This field is invalid'
const strings = {true: String(true), false: String(false)}
describe('Positive Test Cases', () => {
it("Fails the test if an invalid `id` is provided for the target element's `aria-errormessage`", () => {
const secondId = 'id2'
const secondError = 'LISTEN TO ME!!!'
const {queryByTestId} = render(`
<div>
<${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId} ${secondId}" />
<div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div>
<div data-testid="${secondId}" id="${secondId}" role="alert">${secondError}</div>
</div>
`)
const field = queryByTestId('input')
expect(() => expect(field).toHaveAccessibleErrorMessage())
.toThrowErrorMatchingInlineSnapshot(`
<dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</>
Expected element's \`aria-errormessage\` attribute to be empty or a single, valid ID:
Received:
<red> aria-errormessage="error-id id2"</>
`)
// Assume the remaining error messages are the EXACT same as above
expect(() =>
expect(field).toHaveAccessibleErrorMessage(new RegExp(error[0])),
).toThrow()
expect(() => expect(field).toHaveAccessibleErrorMessage(error)).toThrow()
expect(() =>
expect(field).toHaveAccessibleErrorMessage(secondError),
).toThrow()
expect(() =>
expect(field).toHaveAccessibleErrorMessage(new RegExp(secondError[0])),
).toThrow()
})
it('Fails the test if the target element is valid according to the WAI-ARIA spec', () => {
const noAriaInvalidAttribute = 'no-aria-invalid-attribute'
const validFieldState = 'false'
const invalidFieldStates = [
'true',
'',
'grammar',
'spelling',
'asfdafbasdfasa',
]
function renderFieldWithState(state) {
return render(`
<div>
<${input} data-testid="${input}" aria-invalid="${state}" aria-errormessage="${errorId}" />
<div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div>
<input data-testid="${noAriaInvalidAttribute}" aria-errormessage="${errorId}" />
</div>
`)
}
// Success Cases
invalidFieldStates.forEach(invalidState => {
const {queryByTestId} = renderFieldWithState(invalidState)
const field = queryByTestId('input')
expect(field).toHaveAccessibleErrorMessage()
expect(field).toHaveAccessibleErrorMessage(error)
})
// Failure Case
const {queryByTestId} = renderFieldWithState(validFieldState)
const field = queryByTestId('input')
const fieldWithoutAttribute = queryByTestId(noAriaInvalidAttribute)
expect(() => expect(fieldWithoutAttribute).toHaveAccessibleErrorMessage())
.toThrowErrorMatchingInlineSnapshot(`
<dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</>
Expected element to be marked as invalid with attribute:
<green> aria-invalid="true"</>
Received:
<red> null</>
`)
expect(() => expect(field).toHaveAccessibleErrorMessage())
.toThrowErrorMatchingInlineSnapshot(`
<dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</>
Expected element to be marked as invalid with attribute:
<green> aria-invalid="true"</>
Received:
<red> aria-invalid="false</>
`)
// Assume the remaining error messages are the EXACT same as above
expect(() => expect(field).toHaveAccessibleErrorMessage(error)).toThrow()
expect(() =>
expect(field).toHaveAccessibleErrorMessage(new RegExp(error, 'i')),
).toThrow()
})
it('Passes the test if the target element has ANY recognized, non-empty error message', () => {
const {queryByTestId} = render(`
<div>
<${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId}" />
<div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div>
</div>
`)
const field = queryByTestId(input)
expect(field).toHaveAccessibleErrorMessage()
})
it('Fails the test if NO recognized, non-empty error message was found for the target element', () => {
const empty = 'empty'
const emptyErrorId = 'empty-error'
const missing = 'missing'
const {queryByTestId} = render(`
<div>
<input data-testid="${empty}" aria-invalid="${strings.true}" aria-errormessage="${emptyErrorId}" />
<div data-testid="${emptyErrorId}" id="${emptyErrorId}" role="alert"></div>
<input data-testid="${missing}" aria-invalid="${strings.true}" aria-errormessage="${missing}-error" />
</div>
`)
const fieldWithEmptyError = queryByTestId(empty)
const fieldMissingError = queryByTestId(missing)
expect(() => expect(fieldWithEmptyError).toHaveAccessibleErrorMessage())
.toThrowErrorMatchingInlineSnapshot(`
<dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</>
Expected element to have accessible error message:
Received:
`)
expect(() => expect(fieldMissingError).toHaveAccessibleErrorMessage())
.toThrowErrorMatchingInlineSnapshot(`
<dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</>
Expected element to have accessible error message:
Received:
`)
})
it('Passes the test if the target element has the error message that was SPECIFIED', () => {
const {queryByTestId} = render(`
<div>
<${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId}" />
<div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div>
</div>
`)
const field = queryByTestId(input)
const halfOfError = error.slice(0, Math.floor(error.length * 0.5))
expect(field).toHaveAccessibleErrorMessage(error)
expect(field).toHaveAccessibleErrorMessage(new RegExp(halfOfError), 'i')
expect(field).toHaveAccessibleErrorMessage(
expect.stringContaining(halfOfError),
)
expect(field).toHaveAccessibleErrorMessage(
expect.stringMatching(new RegExp(halfOfError), 'i'),
)
})
it('Fails the test if the target element DOES NOT have the error message that was SPECIFIED', () => {
const {queryByTestId} = render(`
<div>
<${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId}" />
<div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div>
</div>
`)
const field = queryByTestId(input)
const msg = 'asdflkje2984fguyvb bnafdsasfa;lj'
expect(() => expect(field).toHaveAccessibleErrorMessage(''))
.toThrowErrorMatchingInlineSnapshot(`
<dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</>
Expected element to have accessible error message:
Received:
<red> This field is invalid</>
`)
// Assume this error is SIMILAR to the error above
expect(() => expect(field).toHaveAccessibleErrorMessage(msg)).toThrow()
expect(() =>
expect(field).toHaveAccessibleErrorMessage(
error.slice(0, Math.floor(error.length * 0.5)),
),
).toThrow()
expect(() =>
expect(field).toHaveAccessibleErrorMessage(new RegExp(msg), 'i'),
).toThrowErrorMatchingInlineSnapshot(`
<dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</>
Expected element to have accessible error message:
<green> /asdflkje2984fguyvb bnafdsasfa;lj/</>
Received:
<red> This field is invalid</>
`)
})
it('Normalizes the whitespace of the received error message', () => {
const {queryByTestId} = render(`
<div>
<${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId}" />
<div data-testid="${errorId}" id="${errorId}" role="alert">
Step
1
of
9000
</div>
</div>
`)
const field = queryByTestId(input)
expect(field).toHaveAccessibleErrorMessage('Step 1 of 9000')
})
})
// These tests for the `.not` use cases will help us cover our bases and complete test coverage
describe('Negated Test Cases', () => {
it("Passes the test if an invalid `id` is provided for the target element's `aria-errormessage`", () => {
const secondId = 'id2'
const secondError = 'LISTEN TO ME!!!'
const {queryByTestId} = render(`
<div>
<${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId} ${secondId}" />
<div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div>
<div data-testid="${secondId}" id="${secondId}" role="alert">${secondError}</div>
</div>
`)
const field = queryByTestId('input')
expect(field).not.toHaveAccessibleErrorMessage()
expect(field).not.toHaveAccessibleErrorMessage(error)
expect(field).not.toHaveAccessibleErrorMessage(new RegExp(error[0]))
expect(field).not.toHaveAccessibleErrorMessage(secondError)
expect(field).not.toHaveAccessibleErrorMessage(new RegExp(secondError[0]))
})
it('Passes the test if the target element is valid according to the WAI-ARIA spec', () => {
const {queryByTestId} = render(`
<div>
<${input} data-testid="${input}" aria-errormessage="${errorId}" />
<div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div>
</div>
`)
const field = queryByTestId(input)
expect(field).not.toHaveAccessibleErrorMessage()
expect(field).not.toHaveAccessibleErrorMessage(error)
expect(field).not.toHaveAccessibleErrorMessage(new RegExp(error[0]))
})
})
})