-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathwai-aria.spec.js
403 lines (354 loc) · 15.8 KB
/
wai-aria.spec.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import path from 'path';
import puppeteer from 'puppeteer';
describe('WAI ARIA Spec', () => {
let browser;
afterEach(async () => {
if (browser && browser.close) {
await browser.close();
}
});
async function setup() {
browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
],
});
const page = await browser.newPage();
await page.goto(`file://${path.resolve(__dirname, 'dist/index.html')}`);
// Seems like the browser is a bit slower on CI, and we're trying to
// select headings before they're registered in the 'store'.
await page.waitForSelector(
'[data-accordion-component="AccordionItemHeading"]',
);
const headingsHandles = await page.$$(
'#classic-accordion [data-accordion-component="AccordionItemHeading"]',
);
const buttonsHandles = await page.$$(
'#classic-accordion [data-accordion-component="AccordionItemButton"]',
);
const itemsHandles = await page.$$(
'#classic-accordion [data-accordion-component="AccordionItem"]',
);
return { browser, page, headingsHandles, buttonsHandles, itemsHandles };
}
describe('Canary tests', () => {
it('Loads the Cypress Testing Sandbox', async () => {
const { browser, page } = await setup();
const title = await page.evaluate(() => document.title);
expect(title).toBe(
'React Accessible Accordion - Integration Test Sandbox',
);
});
it('has rendered the "classic accordion" example', async () => {
const { browser, page } = await setup();
const qtyClassicAccordion = await page.evaluate(
() => document.querySelectorAll('#classic-accordion').length,
);
expect(qtyClassicAccordion).toEqual(1);
});
});
describe('Chrome Accessibility Tree', () => {
it('matches snapshots', async () => {
const { browser, page } = await setup();
expect(await page.accessibility.snapshot()).toMatchSnapshot();
});
});
describe('Keyboard Interaction', () => {
describe('Enter or Space', () => {
it(`When focus is on the accordion header for a collapsed panel,
expands the associated panel. If the implementation allows only
one panel to be expanded, and if another panel is expanded,
collapses that panel.`, async () => {
const { browser, page, buttonsHandles } = await setup();
expect(buttonsHandles.length).toEqual(3);
const firstButtonHandle = buttonsHandles[0];
const secondButtonHandle = buttonsHandles[1];
function evaluateIsExpanded(buttonHandle) {
return page
.evaluate(
(heading) => heading.getAttribute('aria-expanded'),
buttonHandle,
)
.then((ariaExpanded) => ariaExpanded === 'true');
}
// ENTER key
await firstButtonHandle.focus();
expect(await evaluateIsExpanded(firstButtonHandle)).toEqual(
false,
);
await page.keyboard.press('Enter');
expect(await evaluateIsExpanded(firstButtonHandle)).toEqual(
true,
);
// SPACE key
await secondButtonHandle.focus();
expect(await evaluateIsExpanded(secondButtonHandle)).toEqual(
false,
);
await page.keyboard.press('Space');
expect(await evaluateIsExpanded(secondButtonHandle)).toEqual(
true,
);
});
xit(`When focus is on the accordion header for an expanded panel,
collapses the panel if the implementation supports collapsing.
Some implementations require one panel to be expanded at all
times and allow only one panel to be expanded; so, they do not
support a collapse function.`, () => {
// todo
});
});
describe('Tab', () => {
it(`Moves focus to the next focusable element; all focusable
elements in the accordion are included in the page Tab
sequence.`, async () => {
const { browser, page, buttonsHandles } = await setup();
const [firstButtonHandle, secondButtonHandle] = buttonsHandles;
await firstButtonHandle.focus();
await page.keyboard.press('Tab');
const secondIsFocussed = await page.evaluate(
(button) => document.activeElement === button,
secondButtonHandle,
);
expect(secondIsFocussed).toEqual(true);
});
});
describe('Shift + Tab', () => {
it(`Moves focus to the previous focusable element; all focusable
elements in the accordion are included in the page Tab
sequence.`, async () => {
const { browser, page, buttonsHandles } = await setup();
const [firstButtonHandle, secondButtonHandle] = buttonsHandles;
await secondButtonHandle.focus();
await page.keyboard.down('Shift');
await page.keyboard.press('Tab');
await page.keyboard.up('Shift');
const firstIsFocussed = await page.evaluate(
(button) => document.activeElement === button,
firstButtonHandle,
);
expect(firstIsFocussed).toEqual(true);
});
});
describe('Down Arrow (Optional)', () => {
it(`If focus is on an accordion header, moves focus to the next
accordion header.`, async () => {
const { browser, page, buttonsHandles } = await setup();
const [firstButtonHandle, secondButtonHandle] = buttonsHandles;
await firstButtonHandle.focus();
await page.keyboard.press('ArrowDown');
const secondIsFocussed = await page.evaluate(
(button) => document.activeElement === button,
secondButtonHandle,
);
expect(secondIsFocussed).toEqual(true);
});
xit(`If focus is on the last accordion header, either does nothing
or moves focus to the first accordion header.`, () => {
// todo
});
});
describe('Up Arrow (Optional)', () => {
it(`If focus is on an accordion header, moves focus to the previous
accordion header.`, async () => {
const { browser, page, buttonsHandles } = await setup();
const [firstButtonHandle, secondButtonHandle] = buttonsHandles;
await secondButtonHandle.focus();
await page.keyboard.press('ArrowUp');
const firstIsFocussed = await page.evaluate(
(button) => document.activeElement === button,
firstButtonHandle,
);
expect(firstIsFocussed).toEqual(true);
});
xit(`If focus is on the first accordion header, either does nothing
or moves focus to the last accordion header.`, () => {
// todo
});
});
describe('Home (Optional)', () => {
it(`When focus is on an accordion header, moves focus to the first
accordion header.`, async () => {
const { browser, page, buttonsHandles } = await setup();
const [
firstButtonHandle,
secondButtonHandle,
thirdButtonHandle,
] = buttonsHandles;
await thirdButtonHandle.focus();
await page.keyboard.press('Home');
const firstIsFocussed = await page.evaluate(
(button) => document.activeElement === button,
firstButtonHandle,
);
expect(firstIsFocussed).toEqual(true);
});
});
describe('End (Optional)', () => {
it(`When focus is on an accordion header, moves focus to the last
accordion header.`, async () => {
const { browser, page, buttonsHandles } = await setup();
const [
firstButtonHandle,
secondButtonHandle,
thirdButtonHandle,
] = buttonsHandles;
await firstButtonHandle.focus();
await page.keyboard.press('End');
const thirdIsFocussed = await page.evaluate(
(button) => document.activeElement === button,
thirdButtonHandle,
);
expect(thirdIsFocussed).toEqual(true);
});
});
});
describe('WAI-ARIA Roles, States, and Properties', () => {
it(`The title of each accordion header is contained in an element with
role button.`, async () => {
// TODO: Use 'title' elements inside the headings.
const { browser, page, buttonsHandles } = await setup();
expect(buttonsHandles).toHaveLength(3);
for (const buttonHandle of buttonsHandles) {
expect(
await page.evaluate(
(button) => button.getAttribute('role'),
buttonHandle,
),
).toBe('button');
}
});
it(`Each accordion header button is wrapped in an element with role
heading that has a value set for aria-level that is appropriate for
the information architecture of the page.`, async () => {
const { browser, page, buttonsHandles } = await setup();
expect(buttonsHandles).toHaveLength(3);
for (const buttonHandle of buttonsHandles) {
expect(
await page.evaluate(
(button) => button.parentElement.getAttribute('role'),
buttonHandle,
),
).toBe('heading');
expect(
await page.evaluate(
(button) =>
button.parentElement.getAttribute('aria-level'),
buttonHandle,
),
).toBeTruthy();
}
});
xit(`If the native host language has an element with an implicit
heading and aria-level, such as an HTML heading tag, a native host
language element may be used.`, () => {
// Not applicable.
});
it(`The button element is the only element inside the heading element.
That is, if there are other visually persistent elements, they are
not included inside the heading element.`, async () => {
const { headingsHandles, page } = await setup();
expect(headingsHandles).toHaveLength(3);
for (const handle of headingsHandles) {
expect(
await page.evaluate(
(heading) => heading.childNodes.length === 1,
handle,
),
).toEqual(true);
expect(
await page.evaluate(
(heading) =>
heading.firstChild.getAttribute(
'data-accordion-component',
) === 'AccordionItemButton',
handle,
),
).toEqual(true);
}
});
it(`If the accordion panel associated with an accordion header is
visible, the header button element has aria-expanded set to true.
If the panel is not visible, aria-expanded is set to false.`, async () => {
const { browser, page, buttonsHandles } = await setup();
expect(buttonsHandles.length).toEqual(3);
for (const handle of buttonsHandles) {
// Before expanding
expect(
await page.evaluate(
(button) => button.getAttribute('aria-expanded'),
handle,
),
).toEqual('false');
// Click to expand
await handle.click();
// After expanding
expect(
await page.evaluate(
(button) => button.getAttribute('aria-expanded'),
handle,
),
).toEqual('true');
}
});
it(`The accordion header button element has aria-controls set to the ID
of the element containing the accordion panel content.`, async () => {
const { browser, page, itemsHandles } = await setup();
expect(itemsHandles.length).toEqual(3);
for (const itemHandle of itemsHandles) {
const buttonHandle = await itemHandle.$(
'[data-accordion-component="AccordionItemButton"]',
);
const panelHandle = await itemHandle.$(
'[data-accordion-component="AccordionItemPanel"]',
);
const buttonAriaControls = await page.evaluate(
(button) => button.getAttribute('aria-controls'),
buttonHandle,
);
const panelId = await page.evaluate(
(panel) => panel.id,
panelHandle,
);
expect(buttonAriaControls).toBeTruthy();
expect(panelId).toBeTruthy();
expect(buttonAriaControls).toEqual(panelId);
}
});
it(`Optionally, each element that serves as a container for panel
content has role region and aria-labelledby with a value that refers
to the button that controls display of the panel.`, async () => {
const { browser, page, itemsHandles } = await setup();
expect(itemsHandles.length).toEqual(3);
for (const itemHandle of itemsHandles) {
const buttonHandle = await itemHandle.$(
'[data-accordion-component="AccordionItemButton"]',
);
const panelHandle = await itemHandle.$(
'[data-accordion-component="AccordionItemPanel"]',
);
const buttonId = await page.evaluate(
(button) => button.id,
buttonHandle,
);
const panelAriaLabelledBy = await page.evaluate(
(panel) => panel.getAttribute('aria-labelledby'),
panelHandle,
);
const panelRole = await page.evaluate(
(panel) => panel.getAttribute('role'),
panelHandle,
);
expect(panelAriaLabelledBy).toBeTruthy();
expect(buttonId).toBeTruthy();
expect(panelAriaLabelledBy).toEqual(buttonId);
expect(panelRole).toEqual('region');
}
});
});
});