-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These tests will look for the class '.js-test-a11y' and run axe against them, it'll then throw an error to be caught in dev tools or better in an applications' integration tests.
- Loading branch information
1 parent
739ef94
commit 3d454df
Showing
15 changed files
with
443 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
### Running tests | ||
|
||
The default rake task runs all tests: | ||
``` | ||
bundle exec rake | ||
``` | ||
|
||
Javascript is tested using Jasmine and the [Jasmine gem](https://github.com/pivotal/jasmine-gem). Tests can be run either in the browser or on the command line via the dummy app’s tasks: | ||
```sh | ||
# browser | ||
bundle exec rake app:jasmine | ||
|
||
# command line | ||
bundle exec rake app:jasmine:ci | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
app/assets/javascripts/govuk_publishing_components/accessibility-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
(function (window, document, axe) { | ||
window.GOVUK = window.GOVUK || {} | ||
|
||
function AccessibilityTest (selector, callback) { | ||
if (typeof callback !== 'function') { | ||
return | ||
} | ||
|
||
if (!document.querySelector(selector)) { | ||
return callback('No selector "' + selector + '" found') | ||
} | ||
|
||
var axeOptions = { | ||
restoreScroll: true, | ||
include: [selector] | ||
} | ||
axe.run(axeOptions, function (err, results, blah, boo) { | ||
if (err) { | ||
callback('aXe Error: ' + err) | ||
} | ||
|
||
var violations = (typeof results === 'undefined') ? [] : results.violations | ||
|
||
if (violations.length === 0) { | ||
return callback('No accessibility issues found') | ||
} | ||
|
||
var errorText = ( | ||
'\n' + 'Accessibility issues at ' + | ||
results.url + '\n\n' + | ||
violations.map(function (violation) { | ||
var help = 'Problem: ' + violation.help | ||
var helpUrl = 'Try fixing it with this help: ' + violation.helpUrl | ||
var htmlAndTarget = violation.nodes.map(_renderNode).join('\n\n') | ||
|
||
return [ | ||
help, | ||
htmlAndTarget, | ||
helpUrl | ||
].join('\n\n\n') | ||
}).join('\n\n- - -\n\n') | ||
) | ||
callback(undefined, errorText) | ||
}) | ||
} | ||
|
||
var _renderNode = function (node) { | ||
return ( | ||
' Check the HTML:\n' + | ||
' `' + node.html + '`\n' + | ||
' found with the selector:\n' + | ||
' "' + node.target.join(', ') + '"' | ||
) | ||
} | ||
|
||
var _throwUncaughtError = function (error) { | ||
// aXe swallows throw errors so we pass it through a setTimeout | ||
// so that it's not in aXe's context | ||
setTimeout(function () { | ||
throw new Error(error) | ||
}, 0) | ||
} | ||
|
||
window.GOVUK.AccessibilityTest = AccessibilityTest | ||
|
||
// Cut the mustard at IE9+ since aXe only works with those browsers. | ||
// http://responsivenews.co.uk/post/18948466399/cutting-the-mustard | ||
if (document.addEventListener) { | ||
document.addEventListener('DOMContentLoaded', function () { | ||
AccessibilityTest('.js-test-a11y', function (err, results) { | ||
if (err) { | ||
return | ||
} | ||
_throwUncaughtError(results) | ||
}) | ||
}) | ||
} | ||
})(window, document, window.axe) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/assets/javascripts/govuk_publishing_components/vendor/axe.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,4 +166,3 @@ html { | |
} | ||
} | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
app/views/govuk_publishing_components/component_guide/component_doc/_preview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<div class="component-guide-preview"> | ||
<div class="js-test-a11y component-guide-preview"> | ||
<%= render @component_doc.partial_path, fixture.html_safe_data %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
spec/javascripts/govuk_publishing_components/AccessibilityTestSpec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* global jasmine, describe, afterEach, beforeEach, it, expect */ | ||
|
||
var AccessibilityTest = window.GOVUK.AccessibilityTest | ||
|
||
function addToDom (html, style) { | ||
var div = document.createElement('div') | ||
var htmlToInject = '' | ||
if (style) { | ||
htmlToInject += '<style>' + style + '</style>' | ||
} | ||
htmlToInject += '<div class="js-test-a11y">' + 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 + '\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 () { | ||
var selector = '.js-test-a11y' | ||
|
||
beforeEach(function () { | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 11000 | ||
}) | ||
|
||
afterEach(function () { | ||
removeFromDom(selector) | ||
}) | ||
|
||
it('should do nothing if no callback is specified', function () { | ||
AccessibilityTest(selector) | ||
}) | ||
|
||
it('should error if no selector is found', function (done) { | ||
AccessibilityTest(selector, function (err, result) { | ||
expect(result).toBe(undefined) | ||
expect(err).toBe('No selector "' + selector + '" found') | ||
done() | ||
}) | ||
}) | ||
|
||
it('should throw if there\'s a contrast issue', function (done) { | ||
addToDom('<a href="#">Low contrast</a>', 'a { background: white; color: #ddd }') | ||
|
||
AccessibilityTest(selector, function (err, result) { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
var errorMessage = renderErrorMessage({ | ||
problem: 'Elements must have sufficient color contrast', | ||
html: '<a href="#" style="">Low contrast</a>', | ||
selector: '.js-test-a11y > a', | ||
helpUrl: 'https://dequeuniversity.com/rules/axe/3.0.0-alpha/color-contrast?application=axeAPI' | ||
}) | ||
|
||
expect(result).toBe(errorMessage) | ||
|
||
done() | ||
}) | ||
}) | ||
|
||
it('should throw if there\'s a alt tag issue', function (done) { | ||
addToDom('<img src="">') | ||
|
||
AccessibilityTest(selector, function (err, result) { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
var errorMessage = renderErrorMessage({ | ||
problem: 'Images must have alternate text', | ||
html: '<img src="">', | ||
selector: '.js-test-a11y > img', | ||
helpUrl: 'https://dequeuniversity.com/rules/axe/3.0.0-alpha/image-alt?application=axeAPI' | ||
}) | ||
|
||
expect(result).toBe(errorMessage) | ||
|
||
done() | ||
}) | ||
}) | ||
|
||
it('should throw on multiple issues', function (done) { | ||
addToDom('<img src=""><a href="#">Low contrast</a>', 'a { background: white; color: #ddd }') | ||
|
||
AccessibilityTest(selector, function (err, result) { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
var errorMessage = ( | ||
renderErrorMessage({ | ||
problem: 'Elements must have sufficient color contrast', | ||
html: '<a href="#" style="">Low contrast</a>', | ||
selector: '.js-test-a11y > a', | ||
helpUrl: 'https://dequeuniversity.com/rules/axe/3.0.0-alpha/color-contrast?application=axeAPI' | ||
}) + | ||
'\n\n- - -\n\n' + | ||
renderErrorMessage({ | ||
skipHeader: true, | ||
problem: 'Images must have alternate text', | ||
html: '<img src="">', | ||
selector: '.js-test-a11y > img', | ||
helpUrl: 'https://dequeuniversity.com/rules/axe/3.0.0-alpha/image-alt?application=axeAPI' | ||
}) | ||
) | ||
|
||
expect(result).toBe(errorMessage) | ||
|
||
done() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.