-
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.
Add jasmine tests and refactor to allow better testing
Throw errors separately from the main testing to ease testing
- Loading branch information
1 parent
cc124f8
commit 8ab3503
Showing
5 changed files
with
181 additions
and
37 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
131 changes: 127 additions & 4 deletions
131
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 |
---|---|---|
@@ -1,11 +1,134 @@ | ||
/* global describe, beforeEach, it, expect */ | ||
/* global jasmine, describe, afterEach, beforeEach, it, expect */ | ||
|
||
var AccessibilityTest = window.GOVUK.AccessibilityTest | ||
|
||
function addToDom (html) { | ||
var div = document.createElement('div') | ||
div.innerHTML = '<div class="js-test-a11y">' + html + '</div>' | ||
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 () { | ||
// beforeEach | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 11000 | ||
}) | ||
|
||
afterEach(function () { | ||
removeFromDom(selector) | ||
}) | ||
|
||
it('default', function () { | ||
expect(true).toBe(true) | ||
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="#" style="background: white; color: #ddd">Low contrast</a>') | ||
|
||
AccessibilityTest(selector, function (err, result) { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
var errorMessage = renderErrorMessage({ | ||
problem: 'Elements must have sufficient color contrast', | ||
html: '<a href="#" style="background-color: white; color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">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="#" style="background: white; color: #ddd">Low contrast</a>') | ||
|
||
AccessibilityTest(selector, function (err, result) { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
var errorMessage = ( | ||
renderErrorMessage({ | ||
problem: 'Elements must have sufficient color contrast', | ||
html: '<a href="#" style="background-color: white; color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">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() | ||
}) | ||
}) | ||
}) |
Empty file.
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