| OLD | NEW |
| (Empty) | |
| 1 /** |
| 2 * @license |
| 3 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 * This code may only be used under the BSD style license found at http://polyme
r.github.io/LICENSE.txt |
| 5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.
txt |
| 6 * The complete set of contributors may be found at http://polymer.github.io/CON
TRIBUTORS.txt |
| 7 * Code distributed by Google as part of the polymer project is also |
| 8 * subject to an additional IP rights grant found at http://polymer.github.io/PA
TENTS.txt |
| 9 */ |
| 10 |
| 11 (function(Mocha, chai, axs) { |
| 12 |
| 13 Object.keys(Mocha.interfaces).forEach(function(iface) { |
| 14 var orig = Mocha.interfaces[iface]; |
| 15 |
| 16 Mocha.interfaces[iface] = function(suite) { |
| 17 orig.apply(this, arguments); |
| 18 |
| 19 var Suite = Mocha.Suite; |
| 20 var Test = Mocha.Test; |
| 21 |
| 22 suite.on('pre-require', function(context, file, mocha) { |
| 23 |
| 24 /** |
| 25 * Runs the Chrome Accessibility Developer Tools Audit against a test-f
ixture |
| 26 * |
| 27 * @param {String} fixtureId ID of the fixture element in the document
to use |
| 28 * @param {Array?} ignoredRules Array of rules to ignore for this suite |
| 29 * @param {Function?} beforeEach Function to be called before each test
to ensure proper setup |
| 30 */ |
| 31 context.a11ySuite = function(fixtureId, ignoredRules, beforeEach) { |
| 32 // capture a reference to the fixture element early |
| 33 var fixtureElement = document.getElementById(fixtureId); |
| 34 if (!fixtureElement) { |
| 35 return; |
| 36 } |
| 37 |
| 38 // build an audit config to disable certain ignorable tests |
| 39 var axsConfig = new axs.AuditConfiguration(); |
| 40 axsConfig.scope = document.body; |
| 41 axsConfig.showUnsupportedRulesWarning = false; |
| 42 axsConfig.auditRulesToIgnore = ignoredRules; |
| 43 |
| 44 // build mocha suite |
| 45 var a11ySuite = Suite.create(suite, 'A11y Audit - Fixture: ' + fixture
Id); |
| 46 |
| 47 // override the `eachTest` function to hackily create the tests |
| 48 // |
| 49 // eachTest is called right before test runs to calculate the total |
| 50 // number of tests |
| 51 a11ySuite.eachTest = function() { |
| 52 // instantiate fixture |
| 53 fixtureElement.create(); |
| 54 |
| 55 // Make sure lazy-loaded dom is ready (eg <template is='dom-repeat'>
) |
| 56 Polymer.dom.flush(); |
| 57 |
| 58 // If we have a beforeEach function, call it |
| 59 if (beforeEach) { |
| 60 beforeEach(); |
| 61 } |
| 62 |
| 63 // run audit |
| 64 var auditResults = axs.Audit.run(axsConfig); |
| 65 |
| 66 // create tests for audit results |
| 67 auditResults.forEach(function(result, index) { |
| 68 // only show applicable tests |
| 69 if (result.result !== 'NA') { |
| 70 var title = result.rule.heading; |
| 71 // fail test if audit result is FAIL |
| 72 var error = result.result === 'FAIL' ? axs.Audit.accessibilityEr
rorMessage(result) : null; |
| 73 var test = new Test(title, function() { |
| 74 if (error) { |
| 75 throw new Error(error); |
| 76 } |
| 77 }); |
| 78 test.file = file; |
| 79 a11ySuite.addTest(test); |
| 80 } |
| 81 }); |
| 82 |
| 83 // teardown fixture |
| 84 fixtureElement.restore(); |
| 85 |
| 86 suite.eachTest.apply(a11ySuite, arguments); |
| 87 this.eachTest = suite.eachTest; |
| 88 }; |
| 89 |
| 90 return a11ySuite; |
| 91 }; |
| 92 }); |
| 93 }; |
| 94 }); |
| 95 |
| 96 chai.use(function(chai, util) { |
| 97 var Assertion = chai.Assertion; |
| 98 |
| 99 // assert |
| 100 chai.assert.a11yLabel = function(node, exp, msg){ |
| 101 new Assertion(node).to.have.a11yLabel(exp, msg); |
| 102 }; |
| 103 |
| 104 // expect / should |
| 105 Assertion.addMethod('a11yLabel', function(str, msg) { |
| 106 if (msg) { |
| 107 util.flag(this, 'message', msg); |
| 108 } |
| 109 |
| 110 var node = this._obj; |
| 111 |
| 112 // obj must be a Node |
| 113 new Assertion(node).to.be.instanceOf(Node); |
| 114 |
| 115 // vind the text alternative with the help of accessibility dev tools |
| 116 var textAlternative = axs.properties.findTextAlternatives(node, {}); |
| 117 |
| 118 this.assert( |
| 119 textAlternative === str, |
| 120 'expected #{this} to have text alternative #{exp} but got #{act}', |
| 121 'expected #{this} to not have text alternative #{act}', |
| 122 str, |
| 123 textAlternative, |
| 124 true |
| 125 ); |
| 126 }); |
| 127 }); |
| 128 })(window.Mocha, window.chai, window.axs); |
| OLD | NEW |