-
Notifications
You must be signed in to change notification settings - Fork 319
/
accordion.test.js
493 lines (368 loc) · 21.5 KB
/
accordion.test.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
const { getExamples } = require('../../../../lib/jest-helpers')
const { goToComponent, goToExample, renderAndInitialise, getAccessibleName } = require('../../../../lib/puppeteer-helpers')
describe('/components/accordion', () => {
describe('/components/accordion/preview', () => {
describe('when JavaScript is unavailable or fails', () => {
beforeAll(async () => {
await page.setJavaScriptEnabled(false)
})
afterAll(async () => {
await page.setJavaScriptEnabled(true)
})
it('falls back to making all accordion sections visible', async () => {
await goToComponent(page, 'accordion')
const numberOfExampleSections = 2
for (var i = 0; i < numberOfExampleSections; i++) {
var isContentVisible = await page.waitForSelector('.govuk-accordion .govuk-accordion__section:nth-of-type(' + (i + 1) + ') .govuk-accordion__section-content',
{ visible: true, timeout: 1000 }
)
expect(isContentVisible).toBeTruthy()
}
})
it('does not display "↓/↑" in the section headings', async () => {
await goToComponent(page, 'accordion')
const numberOfIcons = await page.evaluate(() => document.body.querySelectorAll('.govuk-accordion .govuk-accordion__section .govuk-accordion-nav__chevron').length)
expect(numberOfIcons).toEqual(0)
})
})
describe('when JavaScript is available', () => {
afterEach(async () => {
// clear accordion state
await page.evaluate(() => window.sessionStorage.clear())
})
it('should indicate that the sections are not expanded', async () => {
await goToComponent(page, 'accordion')
const numberOfExampleSections = 2
for (var i = 0; i < numberOfExampleSections; i++) {
const sectionHeaderButtonExpanded = await page.evaluate(function (i) {
return document.body.querySelector('.govuk-accordion .govuk-accordion__section:nth-of-type(' + (2 + i) + ') .govuk-accordion__section-button').getAttribute('aria-expanded')
}, i)
expect(sectionHeaderButtonExpanded).toEqual('false')
}
})
it('should change the Show all sections button to Hide all sections when both sections are opened', async () => {
await goToComponent(page, 'accordion')
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header')
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(3) .govuk-accordion__section-header')
const openOrCloseAllButtonText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__show-all').textContent)
await page.click('.govuk-accordion__show-all')
expect(openOrCloseAllButtonText).toEqual('Hide all sections')
})
it('should open both sections when the Show all sections button is clicked', async () => {
await goToComponent(page, 'accordion')
await page.click('.govuk-accordion__show-all')
const firstSectionHeaderButtonExpanded = await page.evaluate(() => document.body.querySelectorAll('.govuk-accordion__section').item(0).querySelector('.govuk-accordion__section-button').getAttribute('aria-expanded'))
expect(firstSectionHeaderButtonExpanded).toBeTruthy()
const secondSectionHeaderButtonExpanded = await page.evaluate(() => document.body.querySelectorAll('.govuk-accordion__section').item(1).querySelector('.govuk-accordion__section-button').getAttribute('aria-expanded'))
expect(secondSectionHeaderButtonExpanded).toBeTruthy()
})
it('should already have all sections open if they have the expanded class', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-all-sections-already-open'
})
const numberOfExampleSections = 2
for (var i = 0; i < numberOfExampleSections; i++) {
const sectionHeaderButtonExpanded = await page.evaluate(function (i) {
return document.body.querySelector('.govuk-accordion .govuk-accordion__section:nth-of-type(' + (2 + i) + ') .govuk-accordion__section-button').getAttribute('aria-expanded')
}, i)
expect(sectionHeaderButtonExpanded).toEqual('true')
}
const openOrCloseAllButtonText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__show-all').textContent)
expect(openOrCloseAllButtonText).toEqual('Hide all sections')
})
it('should maintain the expanded state after a page refresh', async () => {
const sectionHeaderButton = '.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-button'
await goToComponent(page, 'accordion')
await page.click(sectionHeaderButton)
const expandedState = await page.evaluate((sectionHeaderButton) => {
return document.body.querySelector(sectionHeaderButton).getAttribute('aria-expanded')
}, sectionHeaderButton)
await page.reload({
waitUntil: 'load'
})
const expandedStateAfterRefresh = await page.evaluate((sectionHeaderButton) => {
return document.body.querySelector(sectionHeaderButton).getAttribute('aria-expanded')
}, sectionHeaderButton)
expect(expandedState).toEqual(expandedStateAfterRefresh)
})
it('should transform the button span to <button>', async () => {
await goToComponent(page, 'accordion')
const buttonTag = await page.evaluate(() => document.body.querySelector('.govuk-accordion .govuk-accordion__section-button').tagName)
expect(buttonTag).toEqual('BUTTON')
})
it('should contain a heading text container', async () => {
await goToComponent(page, 'accordion')
const headingTextContainer = await page.evaluate(() => document.body.querySelector('.govuk-accordion .govuk-accordion__section-button > .govuk-accordion__section-heading-text'))
expect(headingTextContainer).toBeTruthy()
})
describe('focus containers', () => {
it('should contain a heading text focus container', async () => {
await goToComponent(page, 'accordion')
const headingTextFocusContainer = await page.evaluate(() => document.body.querySelector('.govuk-accordion .govuk-accordion__section-button .govuk-accordion__section-heading-text > .govuk-accordion__section-heading-text-focus'))
expect(headingTextFocusContainer).toBeTruthy()
})
it('should contain a summary focus container', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-additional-descriptions'
})
const summaryFocusContainer = await page.evaluate(() => document.body.querySelector('.govuk-accordion .govuk-accordion__section-button > .govuk-accordion__section-summary > .govuk-accordion__section-summary-focus'))
expect(summaryFocusContainer).toBeTruthy()
})
it('should contain a show/hide focus container', async () => {
await goToComponent(page, 'accordion')
const headingTextFocusContainer = await page.evaluate(() => document.body.querySelector('.govuk-accordion .govuk-accordion__section-button .govuk-accordion__section-toggle > .govuk-accordion__section-toggle-focus'))
expect(headingTextFocusContainer).toBeTruthy()
})
})
describe('"↓/↑" icon', () => {
it('should display "↓/↑" in the section headings', async () => {
await goToComponent(page, 'accordion')
const numberOfExampleSections = 2
const numberOfIcons = await page.evaluate(() => document.body.querySelectorAll('.govuk-accordion .govuk-accordion__section .govuk-accordion-nav__chevron').length)
expect(numberOfIcons).toEqual(numberOfExampleSections)
})
})
describe('hidden comma', () => {
it('should contain hidden comma " ," after the heading text for when CSS does not load', async () => {
await goToComponent(page, 'accordion')
const commaAfterHeadingTextClassName = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-heading-text').nextElementSibling.className)
const commaAfterHeadingTextContent = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-heading-text').nextElementSibling.innerHTML)
expect(commaAfterHeadingTextClassName).toEqual('govuk-visually-hidden govuk-accordion__section-heading-divider')
expect(commaAfterHeadingTextContent).toEqual(', ')
})
it('should contain hidden comma " ," after the summary line for when CSS does not load', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-additional-descriptions'
})
const commaAfterHeadingTextClassName = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-summary').nextElementSibling.className)
const commaAfterHeadingTextContent = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-summary').nextElementSibling.innerHTML)
expect(commaAfterHeadingTextClassName).toEqual('govuk-visually-hidden govuk-accordion__section-heading-divider')
expect(commaAfterHeadingTextContent).toEqual(', ')
})
})
describe('summary line', () => {
describe('location of summary', () => {
it('should move the additional description to the button text in the correct order', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-additional-descriptions'
})
const summaryClass = 'govuk-accordion__section-summary govuk-body'
const firstSummaryElement = await page.evaluate(() => document.body.querySelectorAll('.govuk-accordion__section-button > span')[2].className)
expect(firstSummaryElement).toMatch(summaryClass)
})
})
describe('div to span', () => {
it('should have converted the div to a span tag', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-additional-descriptions'
})
const firstSummaryElement = await page.evaluate(() => document.body.querySelector('.govuk-accordion .govuk-accordion__section .govuk-accordion__section-summary').outerHTML)
expect(firstSummaryElement).toMatch(/<span[^>]*>/)
})
})
})
it('should change the Show text to Hide when sections are opened', async () => {
await goToComponent(page, 'accordion')
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header')
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(3) .govuk-accordion__section-header')
const ShowOrHideButtonText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle-text').textContent)
expect(ShowOrHideButtonText).toEqual('Hide')
})
it('should have a data-nosnippet attribute on the "Show / hide" container to hide it from search result snippets', async () => {
await goToComponent(page, 'accordion')
const dataNoSnippetAttribute = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle').getAttribute('data-nosnippet'))
expect(dataNoSnippetAttribute).toEqual('')
})
describe('accessible name', () => {
it('Should set the accessible name of the show/hide section buttons', async () => {
await goToComponent(page, 'accordion')
const $element = await page.$('.govuk-accordion__section-button')
await expect(getAccessibleName(page, $element)).resolves.toBe(
'Section A , Show this section'
)
await $element.click()
await page.waitForTimeout(100)
await expect(getAccessibleName(page, $element)).resolves.toBe(
'Section A , Hide this section'
)
})
it('Includes the heading summary', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-additional-descriptions'
})
const $element = await page.$('.govuk-accordion__section-button')
await expect(getAccessibleName(page, $element)).resolves.toBe(
'Test , Additional description , Show this section'
)
await $element.click()
await page.waitForTimeout(100)
await expect(getAccessibleName(page, $element)).resolves.toBe(
'Test , Additional description , Hide this section'
)
})
})
describe('expandable content', () => {
it('should have an aria-labelledby that matches the heading text ID', async () => {
await goToComponent(page, 'accordion')
const ariaLabelledByValue = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-content').getAttribute('aria-labelledby'))
const headingTextId = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-heading-text').getAttribute('id'))
expect(ariaLabelledByValue).toEqual(headingTextId)
})
})
describe('localisation', () => {
it('should localise "Show all sections" based on data attribute', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-translations'
})
const showAllSectionsDataAttribute = await page.evaluate(() => document.body.querySelector('.govuk-accordion').getAttribute('data-i18n.show-all-sections'))
const allSectionsToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__show-all-text').innerHTML)
expect(allSectionsToggleText).toEqual(showAllSectionsDataAttribute)
})
it('should localise "Show all sections" based on JavaScript configuration', async () => {
await goToExample(page, 'translated')
const allSectionsToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__show-all-text').innerHTML)
expect(allSectionsToggleText).toBe('Dangos adrannau')
})
it('should localise "Hide all sections" based on data attribute', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-translations'
})
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header')
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(3) .govuk-accordion__section-header')
const hideAllSectionsDataAttribute = await page.evaluate(() => document.body.querySelector('.govuk-accordion').getAttribute('data-i18n.hide-all-sections'))
const allSectionsToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__show-all-text').innerHTML)
expect(allSectionsToggleText).toEqual(hideAllSectionsDataAttribute)
})
it('should localise "Hide all sections" based on JavaScript configuration', async () => {
await goToExample(page, 'translated')
await page.click('.govuk-accordion .govuk-accordion__show-all')
const allSectionsToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__show-all-text').innerHTML)
expect(allSectionsToggleText).toBe('Cuddio adrannau')
})
it('should localise "Show section" based on data attribute', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-translations'
})
const showSectionDataAttribute = await page.evaluate(() => document.body.querySelector('.govuk-accordion').getAttribute('data-i18n.show-section'))
const firstSectionToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle-text').innerHTML)
expect(firstSectionToggleText).toEqual(showSectionDataAttribute)
})
it('should localise "Show section" based on JavaScript configuration', async () => {
await goToExample(page, 'translated')
const firstSectionToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle-text').innerHTML)
expect(firstSectionToggleText).toBe('Dangos')
})
it('should localise "Show section" aria-label based on data attribute', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-translations'
})
const showSectionDataAttribute = await page.evaluate(() =>
document.body
.querySelector('.govuk-accordion')
.getAttribute('data-i18n.show-section-aria-label')
)
const firstSectionToggleAriaLabel = await page.evaluate(
() =>
document.body.querySelector(
'.govuk-accordion__section-button'
).getAttribute('aria-label')
)
expect(
firstSectionToggleAriaLabel.endsWith(showSectionDataAttribute)
).toBeTruthy()
})
it('should localise "Show section" aria-label based on JavaScript configuration', async () => {
await goToExample(page, 'translated')
const firstSectionToggleAriaLabel = await page.evaluate(() =>
document.body
.querySelector('.govuk-accordion__section-button')
.getAttribute('aria-label')
)
expect(firstSectionToggleAriaLabel.endsWith('Dangos adran')).toBeTruthy()
})
it('should localise "Hide section" based on data attribute', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-translations'
})
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header')
const hideSectionDataAttribute = await page.evaluate(() => document.body.querySelector('.govuk-accordion').getAttribute('data-i18n.hide-section'))
const firstSectionToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle-text').innerHTML)
expect(firstSectionToggleText).toEqual(hideSectionDataAttribute)
})
it('should localise "Hide section" based on JavaScript configuration', async () => {
await goToExample(page, 'translated')
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header')
const firstSectionToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle-text').innerHTML)
expect(firstSectionToggleText).toBe('Cuddio')
})
it('should localise "Hide section" aria-label based on data attribute', async () => {
await goToComponent(page, 'accordion', {
exampleName: 'with-translations'
})
await page.click(
'.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header'
)
const hideSectionDataAttribute = await page.evaluate(() =>
document.body
.querySelector('.govuk-accordion')
.getAttribute('data-i18n.hide-section-aria-label')
)
const firstSectionToggleAriaLabel = await page.evaluate(() =>
document.body
.querySelector('.govuk-accordion__section-button')
.getAttribute('aria-label')
)
expect(
firstSectionToggleAriaLabel.endsWith(hideSectionDataAttribute)
).toBeTruthy()
})
it('should localise "Hide section" aria-label based on JavaScript configuration', async () => {
await goToExample(page, 'translated')
await page.click(
'.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header'
)
const firstSectionToggleAriaLabel = await page.evaluate(() =>
document.body
.querySelector('.govuk-accordion__section-button')
.getAttribute('aria-label')
)
expect(
firstSectionToggleAriaLabel.endsWith('Cuddio adran')
).toBeTruthy()
})
describe('Cross Side Scripting prevention', () => {
let examples
beforeAll(async () => {
examples = await getExamples('accordion')
})
it('injects the localised strings as text not HTML', async () => {
await renderAndInitialise(page, 'accordion', {
nunjucksParams: examples.default,
javascriptConfig: {
i18n: {
showAllSections: 'Show <strong>all sections</strong>',
showSection: 'Show <strong>this section</strong>'
}
}
})
const { toggleAllContent, firstSectionToggleContent } =
await page.$eval('[data-module]', (el) => ({
toggleAllContent: el
.querySelector('.govuk-accordion__show-all-text')
.innerHTML.trim(),
firstSectionToggleContent: el
.querySelector('.govuk-accordion__section-toggle-text')
.innerHTML.trim()
}))
expect(toggleAllContent).toEqual(
'Show <strong>all sections</strong>'
)
expect(firstSectionToggleContent).toEqual(
'Show <strong>this section</strong>'
)
})
})
})
})
})
})