-
Notifications
You must be signed in to change notification settings - Fork 19
/
AccessibilityTestSpec.js
224 lines (186 loc) · 7.83 KB
/
AccessibilityTestSpec.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
/* global describe, afterEach, it, expect */
var TEST_SELECTOR = '.js-test-a11y'
var AccessibilityTest = window.GOVUK.AccessibilityTest
function addToDom (html, style) {
var div = document.createElement('div')
var htmlToInject = ''
htmlToInject += '<div class="' + TEST_SELECTOR.replace('.', '') + '">'
if (style) {
htmlToInject += '<style>' + style + '</style>'
}
htmlToInject += html + '</div>'
div.innerHTML = htmlToInject
document.getElementsByTagName('body')[0].appendChild(div)
}
function removeFromDom (selector) {
var nodeToRemove = document.querySelector(selector)
if (nodeToRemove) {
nodeToRemove.parentNode.removeChild(nodeToRemove)
}
}
function renderErrorMessage (option) {
var url = window.location.href
var message = ''
if (!option.skipHeader) {
message += '\nAccessibility issues at ' + url + '\n\n'
}
message += (
'Problem: ' + option.problem + ' (' + option.id + ')' + '\n' +
'\n' +
'\n' +
' Check the HTML:\n' +
' `' + option.html + '`\n' +
' found with the selector:\n' +
' "' + option.selector + '"\n' +
'\n' +
'\n' +
'Try fixing it with this help: ' + option.helpUrl
)
return message
}
describe('AccessibilityTest', function () {
afterEach(function () {
removeFromDom(TEST_SELECTOR)
document.body.className = '';
})
it('should do nothing if no callback is specified', function () {
AccessibilityTest(TEST_SELECTOR)
})
it('should not run axe if no selector is found', function (done) {
spyOn(window.axe, 'run').and.callThrough()
AccessibilityTest(TEST_SELECTOR, function (err, violations, incompleteWarnings) {
expect(window.axe.run).not.toHaveBeenCalled()
expect(err).toBe(undefined)
expect(violations).toBe(undefined)
expect(incompleteWarnings).toBe(undefined)
done()
})
})
// TODO: Remove when aXe core patched
// https://github.com/dequelabs/axe-core/issues/525
it('should prevent aXe from erroring when SVG is present by disabling restoreScroll', function (done) {
spyOn(window.axe, 'run').and.callThrough()
addToDom('<div style="height: 1000px; width: 100px;"></div><svg class="svg" xmlns="http://www.w3.org/2000/svg" width="13" height="17" viewBox="0 0 13 17">\
<path fill="currentColor" d="M6.5 0L0 6.5 1.4 8l4-4v12.7h2V4l4.3 4L13 6.4z"></path>\
</svg>')
AccessibilityTest(TEST_SELECTOR, function (err, violations, incompleteWarnings) {
expect(err).toBe(undefined)
// Protect against test failing if PhantomJS updated
if (!(document.querySelector('svg').children instanceof HTMLCollection)) {
axeOptions = window.axe.run.calls.argsFor(0)
expect(axeOptions['restoreScroll']).toBe(undefined)
}
done()
})
})
it('should add a class to the body when it finishes', function (done) {
addToDom('<div>text</div>')
AccessibilityTest(TEST_SELECTOR, function (err, violations, incompleteWarnings) {
expect(document.body.classList.contains('js-test-a11y-finished')).toBe(true)
done()
})
})
it('should add a class to the body when it finds no violations', function (done) {
addToDom('<div>text</div>')
AccessibilityTest(TEST_SELECTOR, function (err, violations, incompleteWarnings) {
expect(document.body.classList.contains('js-test-a11y-success')).toBe(true)
done()
})
})
it('should not scroll the page', function (done) {
var bigElement = '<div style="height: 1000px; width: 100px;"></div>'
addToDom(
bigElement + '<a href="#">Low contrast</a>' + bigElement,
'a { background: white; color: #ddd }'
)
expect(window.scrollY).toBe(0, 'should start at top of the page')
AccessibilityTest(TEST_SELECTOR, function () {
expect(window.scrollY).toBe(0, 'should end at top of the page')
done()
})
})
it('should throw if there\'s a contrast issue', function (done) {
addToDom('<a href="#">Low contrast</a>', 'a { background: white; color: #ddd }')
AccessibilityTest(TEST_SELECTOR, function (err, violations, incompleteWarnings) {
if (err) {
throw err
}
var errorMessage = renderErrorMessage({
id: 'color-contrast',
problem: 'Elements must have sufficient color contrast',
html: '<a href="#" style="">Low contrast</a>',
selector: TEST_SELECTOR + ' > a',
helpUrl: 'https://dequeuniversity.com/rules/axe/2.3/color-contrast?application=axeAPI'
})
expect(violations).toBe(errorMessage)
expect(document.body.classList.contains('js-test-a11y-finished')).toBe(true)
expect(document.body.classList.contains('js-test-a11y-failed')).toBe(true)
done()
})
})
it('should throw if there\'s a alt tag issue', function (done) {
addToDom('<img src="">')
AccessibilityTest(TEST_SELECTOR, function (err, violations, incompleteWarnings) {
if (err) {
throw err
}
var errorMessage = renderErrorMessage({
id: 'image-alt',
problem: 'Images must have alternate text',
html: '<img src="">',
selector: TEST_SELECTOR + ' > img',
helpUrl: 'https://dequeuniversity.com/rules/axe/2.3/image-alt?application=axeAPI'
})
expect(violations).toBe(errorMessage)
done()
})
})
it('process incomplete warnings into object for rendering in guide', function (done) {
addToDom('<a href="#">Link</a>', 'a { background-image: url("/"); }')
AccessibilityTest(TEST_SELECTOR, function (err, violations, pageResults) {
expect(pageResults.incompleteWarnings[0].summary).toBe("Elements must have sufficient color contrast")
expect(pageResults.incompleteWarnings[0].url).toBe("https://dequeuniversity.com/rules/axe/2.3/color-contrast?application=axeAPI")
expect(pageResults.incompleteWarnings[0].selectors[0].selector[0]).toBe('.js-test-a11y > a')
expect(pageResults.incompleteWarnings[0].selectors[0].reasons[0]).toBe('Element\'s background color could not be determined due to a background image')
done()
})
})
it('process violations into object for rendering in guide', function (done) {
addToDom('<img src=""><a href="#">Low contrast</a>', 'a { background: white; color: #ddd }')
AccessibilityTest(TEST_SELECTOR, function (err, violations, pageResults) {
expect(pageResults.violations[0].summary).toBe("Elements must have sufficient color contrast")
expect(pageResults.violations[0].url).toBe("https://dequeuniversity.com/rules/axe/2.3/color-contrast?application=axeAPI")
expect(pageResults.violations[0].selectors[0].selector[0]).toBe('.js-test-a11y > a')
expect(pageResults.violations[0].selectors[0].reasons[0]).toBe('Element has insufficient color contrast of 1.35 (foreground color: #dddddd, background color: #ffffff, font size: 12.0pt, font weight: normal)')
done()
})
})
it('should throw on multiple issues', function (done) {
addToDom('<img src=""><a href="#">Low contrast</a>', 'a { background: white; color: #ddd }')
AccessibilityTest(TEST_SELECTOR, function (err, violations, incompleteWarnings) {
if (err) {
throw err
}
var errorMessage = (
renderErrorMessage({
id: 'color-contrast',
problem: 'Elements must have sufficient color contrast',
html: '<a href="#" style="">Low contrast</a>',
selector: TEST_SELECTOR + ' > a',
helpUrl: 'https://dequeuniversity.com/rules/axe/2.3/color-contrast?application=axeAPI'
}) +
'\n\n- - -\n\n' +
renderErrorMessage({
id: 'image-alt',
skipHeader: true,
problem: 'Images must have alternate text',
html: '<img src="">',
selector: TEST_SELECTOR + ' > img',
helpUrl: 'https://dequeuniversity.com/rules/axe/2.3/image-alt?application=axeAPI'
})
)
expect(violations).toBe(errorMessage)
done()
})
})
})