From 4f266c7c2e74d9931ff89d4364bb00b12b704706 Mon Sep 17 00:00:00 2001 From: Marcy Sutton Date: Fri, 5 Jan 2018 16:24:13 -0800 Subject: [PATCH] feat: add required-attr options, integration tests --- lib/checks/aria/required-attr.js | 11 ++++ test/checks/aria/required-attr.js | 8 +++ .../configure-options/configure-options.html | 25 ++++++++++ .../configure-options/configure-options.js | 50 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 test/integration/full/configure-options/configure-options.html create mode 100644 test/integration/full/configure-options/configure-options.js diff --git a/lib/checks/aria/required-attr.js b/lib/checks/aria/required-attr.js index 23bfe0a3df..f21bb4446e 100644 --- a/lib/checks/aria/required-attr.js +++ b/lib/checks/aria/required-attr.js @@ -1,3 +1,11 @@ +options = Array.isArray(options) ? options : []; + +var uniqueArray = (arrArg) => { + return arrArg.filter((elem, pos, arr) => { + return arr.indexOf(elem) === pos; + }); +}; + var missing = []; if (node.hasAttributes()) { @@ -5,6 +13,9 @@ if (node.hasAttributes()) { role = node.getAttribute('role'), required = axe.commons.aria.requiredAttr(role); + if (options.length) { + required = uniqueArray(required.concat(options)); + } if (role && required) { for (var i = 0, l = required.length; i < l; i++) { attr = required[i]; diff --git a/test/checks/aria/required-attr.js b/test/checks/aria/required-attr.js index 8d410221da..3193aa2ce0 100644 --- a/test/checks/aria/required-attr.js +++ b/test/checks/aria/required-attr.js @@ -57,4 +57,12 @@ describe('aria-required-attr', function () { axe.commons.aria.requiredAttr = orig; }); + describe('options', function () { + it('should require provided attribute names', function () { + fixture.innerHTML = '
'; + var target = fixture.children[0]; + assert.isFalse(checks['aria-required-attr'].evaluate.call(checkContext, target, ['aria-valuemax', 'aria-bats'])); + assert.deepEqual(checkContext._data, ['aria-valuenow', 'aria-valuemax', 'aria-valuemin', 'aria-bats']); + }); + }); }); \ No newline at end of file diff --git a/test/integration/full/configure-options/configure-options.html b/test/integration/full/configure-options/configure-options.html new file mode 100644 index 0000000000..c6564d1201 --- /dev/null +++ b/test/integration/full/configure-options/configure-options.html @@ -0,0 +1,25 @@ + + + + + + + + + + + + +
+ +
+ + + + diff --git a/test/integration/full/configure-options/configure-options.js b/test/integration/full/configure-options/configure-options.js new file mode 100644 index 0000000000..941e7ab8e5 --- /dev/null +++ b/test/integration/full/configure-options/configure-options.js @@ -0,0 +1,50 @@ +describe('Check Configure Options', function() { + 'use strict'; + + var target = document.querySelector('#target'); + + describe('aria-allowed-attr', function() { + it('should allow an attribute supplied in options', function(done) { + target.setAttribute('role', 'separator'); + target.setAttribute('aria-valuenow', '0'); + + axe.configure({ + checks: [{ + id: 'aria-allowed-attr', + options: ['aria-valuenow'] + }] + }); + axe.run(target, { + runOnly: { + type: 'rule', + values: [ 'aria-allowed-attr' ] + } + }, function(error, results) { + assert.lengthOf(results.violations, 0, 'violations'); + done(); + }); + }); + }); + + describe('aria-required-attr', function() { + it('should report unique attributes when supplied from options', function(done) { + target.setAttribute('role', 'slider'); + axe.configure({ + checks: [{ + id: 'aria-required-attr', + options: ['aria-checked'] + }] + }); + axe.run('#target', { + runOnly: { + type: 'rule', + values: [ 'aria-required-attr' ] + } + }, function(error, results) { + assert.lengthOf(results.violations, 1, 'violations'); + assert.sameMembers(results.violations[0].nodes[0].any[0].data, ['aria-valuemax', 'aria-valuemin', 'aria-checked']); + done(); + }); + }); + }); +});