Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rule): add reviewOnFail option to have rule return as needs review instead of violation #2235

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion lib/core/base/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ function Rule(spec, parentAudit) {
*/
this.pageLevel = typeof spec.pageLevel === 'boolean' ? spec.pageLevel : false;

/**
* Flag to force the rule to return as needs review rather than a violation if any of the checks fail.
* @type {Boolean}
*/
this.reviewOnFail =
typeof spec.reviewOnFail === 'boolean' ? spec.reviewOnFail : false;

/**
* Checks that any may return true to satisfy rule
* @type {Array}
Expand Down Expand Up @@ -226,11 +233,28 @@ Rule.prototype.run = function(context, options = {}, resolve, reject) {
});

checkQueue
.then(function(results) {
.then(results => {
const result = getResult(results);
if (result) {
result.node = new DqElement(node.actualNode, options);
ruleResult.nodes.push(result);

// mark rule as incomplete rather than failure for rules with reviewOnFail
if (this.reviewOnFail) {
['any', 'all'].forEach(type => {
result[type].forEach(checkResult => {
if (checkResult.result === false) {
checkResult.result = undefined;
}
});
});

result.none.forEach(checkResult => {
if (checkResult.result === true) {
checkResult.result = undefined;
}
});
}
}
resolveNode();
})
Expand Down Expand Up @@ -285,6 +309,23 @@ Rule.prototype.runSync = function(context, options = {}) {
? new DqElement(node.actualNode, options)
: null;
ruleResult.nodes.push(result);

// mark rule as incomplete rather than failure for rules with reviewOnFail
if (this.reviewOnFail) {
['any', 'all'].forEach(type => {
result[type].forEach(checkResult => {
if (checkResult.result === false) {
checkResult.result = undefined;
}
});
});

result.none.forEach(checkResult => {
if (checkResult.result === true) {
checkResult.result = undefined;
}
});
}
}
});

Expand Down Expand Up @@ -519,6 +560,11 @@ Rule.prototype.configure = function(spec) {
typeof spec.pageLevel === 'boolean' ? spec.pageLevel : false;
}

if (spec.hasOwnProperty('reviewOnFail')) {
this.reviewOnFail =
typeof spec.reviewOnFail === 'boolean' ? spec.reviewOnFail : false;
}

if (spec.hasOwnProperty('any')) {
this.any = spec.any;
}
Expand Down
107 changes: 107 additions & 0 deletions test/core/base/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,47 @@ describe('Rule', function() {
);
});

it('should mark checks as incomplete if reviewOnFail is set to true', function(done) {
var rule = new Rule(
{
reviewOnFail: true,
all: ['cats'],
any: ['cats'],
none: ['dogs']
},
{
checks: {
cats: new Check({
id: 'cats',
evaluate: function() {
return false;
}
}),
dogs: new Check({
id: 'dogs',
evaluate: function() {
return true;
}
})
}
}
);

rule.run(
{
include: [axe.utils.getFlattenedTree(fixture)[0]]
},
{},
function(results) {
assert.isUndefined(results.nodes[0].all[0].result);
assert.isUndefined(results.nodes[0].any[0].result);
assert.isUndefined(results.nodes[0].none[0].result);
done();
},
isNotCalled
);
});

describe('NODE rule', function() {
it('should create a RuleResult', function() {
var orig = window.RuleResult;
Expand Down Expand Up @@ -1305,6 +1346,44 @@ describe('Rule', function() {
}
});

it('should mark checks as incomplete if reviewOnFail is set to true', function() {
var rule = new Rule(
{
reviewOnFail: true,
all: ['cats'],
any: ['cats'],
none: ['dogs']
},
{
checks: {
cats: new Check({
id: 'cats',
evaluate: function() {
return false;
}
}),
dogs: new Check({
id: 'dogs',
evaluate: function() {
return true;
}
})
}
}
);

var results = rule.runSync(
{
include: [axe.utils.getFlattenedTree(fixture)[0]]
},
{}
);

assert.isUndefined(results.nodes[0].all[0].result);
assert.isUndefined(results.nodes[0].any[0].result);
assert.isUndefined(results.nodes[0].none[0].result);
});

describe.skip('NODE rule', function() {
it('should create a RuleResult', function() {
var orig = window.RuleResult;
Expand Down Expand Up @@ -1615,6 +1694,27 @@ describe('Rule', function() {
});
});

describe('.reviewOnFail', function() {
it('should be set', function() {
var spec = {
reviewOnFail: true
};
assert.equal(new Rule(spec).reviewOnFail, spec.reviewOnFail);
});

it('should default to false', function() {
var spec = {};
assert.isFalse(new Rule(spec).reviewOnFail);
});

it('should default to false if given a bad value', function() {
var spec = {
reviewOnFail: 'monkeys'
};
assert.isFalse(new Rule(spec).reviewOnFail);
});
});

describe('.id', function() {
it('should be set', function() {
var spec = {
Expand Down Expand Up @@ -1775,6 +1875,13 @@ describe('Rule', function() {
rule.configure({ pageLevel: true });
assert.equal(rule._get('pageLevel'), true);
});
it('should override reviewOnFail', function() {
var rule = new Rule({ reviewOnFail: false });

assert.equal(rule._get('reviewOnFail'), false);
rule.configure({ reviewOnFail: true });
assert.equal(rule._get('reviewOnFail'), true);
});
it('should override any', function() {
var rule = new Rule({ any: ['one', 'two'] });

Expand Down