Skip to content

Commit

Permalink
feat(checks): normalize check options to alway be an object (#2219)
Browse files Browse the repository at this point in the history
* feat(checks): normalize check options to alway be an object

* test getOptions

* Update lib/core/base/check.js

Co-authored-by: Wilco Fiers <WilcoFiers@users.noreply.github.com>

Co-authored-by: Wilco Fiers <WilcoFiers@users.noreply.github.com>
  • Loading branch information
straker and WilcoFiers authored May 12, 2020
1 parent 64318a5 commit da12da7
Show file tree
Hide file tree
Showing 69 changed files with 1,671 additions and 734 deletions.
2 changes: 1 addition & 1 deletion lib/checks/aria/aria-valid-attr-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validateAttr } from '../../commons/aria';
import { getNodeAttributes } from '../../core/utils';

function ariaValidAttrEvaluate(node, options) {
options = Array.isArray(options) ? options : [];
options = Array.isArray(options.value) ? options.value : [];

var invalid = [],
aria = /^aria-/;
Expand Down
2 changes: 1 addition & 1 deletion lib/checks/aria/aria-valid-attr-value-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validateAttrValue } from '../../commons/aria';
import { getNodeAttributes } from '../../core/utils';

function ariaValidAttrValueEvaluate(node, options) {
options = Array.isArray(options) ? options : [];
options = Array.isArray(options.value) ? options.value : [];

let needsReview = '';
let messageKey = '';
Expand Down
2 changes: 1 addition & 1 deletion lib/checks/language/valid-lang-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { validLangs, getBaseLang } from '../../core/utils';
function validLangEvaluate(node, options) {
var langs, invalid;

langs = (options ? options : validLangs()).map(getBaseLang);
langs = (options.value ? options.value : validLangs()).map(getBaseLang);

invalid = ['lang', 'xml:lang'].reduce(function(invalid, langAttr) {
var langVal = node.getAttribute(langAttr);
Expand Down
43 changes: 32 additions & 11 deletions lib/core/base/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ export function createExecutionContext(spec) {
return spec;
}

/**
* Normalize check options to always be an object.
* @param {Object} options
* @return Object
*/
function normalizeOptions(options = {}) {
if (Array.isArray(options) || typeof options !== 'object') {
options = { value: options };
}

return options;
}

function Check(spec) {
if (spec) {
this.id = spec.id;
Expand Down Expand Up @@ -65,15 +78,15 @@ Check.prototype.enabled = true;
Check.prototype.run = function(node, options, context, resolve, reject) {
'use strict';
options = options || {};
var enabled = options.hasOwnProperty('enabled')
? options.enabled
: this.enabled,
checkOptions = options.options || this.options;
const enabled = options.hasOwnProperty('enabled')
? options.enabled
: this.enabled;
const checkOptions = this.getOptions(options.options);

if (enabled) {
var checkResult = new CheckResult(this);
var helper = checkHelper(checkResult, options, resolve, reject);
var result;
const checkResult = new CheckResult(this);
const helper = checkHelper(checkResult, options, resolve, reject);
let result;

try {
result = this.evaluate.call(
Expand Down Expand Up @@ -118,7 +131,7 @@ Check.prototype.runSync = function(node, options, context) {
return null;
}

const checkOptions = options.options || this.options;
const checkOptions = this.getOptions(options.options);
const checkResult = new CheckResult(this);
const helper = checkHelper(checkResult, options);

Expand Down Expand Up @@ -160,13 +173,21 @@ Check.prototype.runSync = function(node, options, context) {
*/

Check.prototype.configure = function(spec) {
['options', 'enabled']
.filter(prop => spec.hasOwnProperty(prop))
.forEach(prop => (this[prop] = spec[prop]));
if (spec.hasOwnProperty('enabled')) {
this.enabled = spec.enabled;
}

if (spec.hasOwnProperty('options')) {
this.options = normalizeOptions(spec.options);
}

['evaluate', 'after']
.filter(prop => spec.hasOwnProperty(prop))
.forEach(prop => (this[prop] = createExecutionContext(spec[prop])));
};

Check.prototype.getOptions = function getOptions(options = {}) {
return Object.assign({}, this.options, normalizeOptions(options));
};

export default Check;
36 changes: 26 additions & 10 deletions test/checks/aria/allowed-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ describe('aria-allowed-attr', function() {
fixture.appendChild(node);

assert.isFalse(
checks['aria-allowed-attr'].evaluate.call(checkContext, node)
axe.testUtils
.getCheckEvaluate('aria-allowed-attr')
.call(checkContext, node)
);
assert.deepEqual(checkContext._data, ['aria-selected="true"']);
});
Expand All @@ -32,7 +34,9 @@ describe('aria-allowed-attr', function() {
fixture.appendChild(node);

assert.isTrue(
checks['aria-allowed-attr'].evaluate.call(checkContext, node)
axe.testUtils
.getCheckEvaluate('aria-allowed-attr')
.call(checkContext, node)
);
});

Expand All @@ -45,7 +49,9 @@ describe('aria-allowed-attr', function() {
fixture.appendChild(node);

assert.isFalse(
checks['aria-allowed-attr'].evaluate.call(checkContext, node)
axe.testUtils
.getCheckEvaluate('aria-allowed-attr')
.call(checkContext, node)
);
assert.deepEqual(checkContext._data, ['aria-selected="true"']);
});
Expand All @@ -59,7 +65,9 @@ describe('aria-allowed-attr', function() {
fixture.appendChild(node);

assert.isTrue(
checks['aria-allowed-attr'].evaluate.call(checkContext, node)
axe.testUtils
.getCheckEvaluate('aria-allowed-attr')
.call(checkContext, node)
);
assert.isNull(checkContext._data);
});
Expand All @@ -73,7 +81,9 @@ describe('aria-allowed-attr', function() {
fixture.appendChild(node);

assert.isTrue(
checks['aria-allowed-attr'].evaluate.call(checkContext, node)
axe.testUtils
.getCheckEvaluate('aria-allowed-attr')
.call(checkContext, node)
);
assert.isNull(checkContext._data);
});
Expand All @@ -88,7 +98,9 @@ describe('aria-allowed-attr', function() {
fixture.appendChild(node);

assert.isTrue(
checks['aria-allowed-attr'].evaluate.call(checkContext, node)
axe.testUtils
.getCheckEvaluate('aria-allowed-attr')
.call(checkContext, node)
);
assert.isNull(checkContext._data);
});
Expand All @@ -108,9 +120,11 @@ describe('aria-allowed-attr', function() {
'<div role="mccheddarton" id="target" aria-checked="true" aria-snuggles="true"></div>';
var target = fixture.children[0];
assert.isTrue(
checks['aria-allowed-attr'].evaluate.call(checkContext, target, {
mccheddarton: ['aria-checked', 'aria-snuggles']
})
axe.testUtils
.getCheckEvaluate('aria-allowed-attr')
.call(checkContext, target, {
mccheddarton: ['aria-checked', 'aria-snuggles']
})
);
delete axe.commons.aria.lookupTable.role.mccheddarton;
});
Expand Down Expand Up @@ -142,7 +156,9 @@ describe('aria-allowed-attr', function() {
bagley: ['aria-snuggles2']
};
assert.isTrue(
checks['aria-allowed-attr'].evaluate.call(checkContext, target, options)
axe.testUtils
.getCheckEvaluate('aria-allowed-attr')
.call(checkContext, target, options)
);
delete axe.commons.aria.lookupTable.role.mccheddarton;
delete axe.commons.aria.lookupTable.role.bagley;
Expand Down
Loading

0 comments on commit da12da7

Please sign in to comment.