Skip to content

Commit

Permalink
fix: Return inapplicable results (#473).
Browse files Browse the repository at this point in the history
* fix: Return inapplicable results (#473).

* fix: Tweak aggregateRule to always return inapplicable if nodes are empty. Add test.

* fix: Remove .only

* fix: Rename aggregateRule() to aggregateNodeResults()
  • Loading branch information
robdodson authored and WilcoFiers committed Dec 13, 2017
1 parent 5e63a5c commit 0a2a5cc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,42 @@

/**
* Calculates the result of a Rule based on its types and the result of its child Checks
* @param {RuleResult} ruleResult The RuleResult to calculate the result of
* @param {Array} nodeResults The array of nodes tested by the Rule
*/
axe.utils.aggregateRule = function (subResults) {
axe.utils.aggregateNodeResults = function (nodeResults) {
let ruleResult = {};

// For each node, retrieve the result and impact
subResults = subResults.map(function (subResult) {
nodeResults = nodeResults.map(function (nodeResult) {
// Known result
if (subResult.any && subResult.all && subResult.none) {
return axe.utils.aggregateChecks(subResult);
if (nodeResult.any && nodeResult.all && nodeResult.none) {
return axe.utils.aggregateChecks(nodeResult);

} else if (Array.isArray(subResult.node)) {
return axe.utils.finalizeRuleResult(subResult);
} else if (Array.isArray(nodeResult.node)) {
return axe.utils.finalizeRuleResult(nodeResult);

} else {
throw new TypeError('Invalid Result type');
}
});

// Aggregate the result
let resultList = subResults.map((node) => node.result);
ruleResult.result = axe.utils.aggregate(axe.constants.results, resultList, ruleResult.result);
// If there were no nodes passed in, mark the test as inapplicable
if (nodeResults && nodeResults.length) {
let resultList = nodeResults.map((node) => node.result);
ruleResult.result = axe.utils.aggregate(axe.constants.results, resultList, ruleResult.result);
} else {
ruleResult.result = 'inapplicable';
}

// Create an array for each type
axe.constants.resultGroups
.forEach((group) => ruleResult[group] = []);

// Fill the array with nodes
subResults.forEach(function (subResult) {
var groupName = axe.constants.resultGroupMap[subResult.result];
ruleResult[groupName].push(subResult);
nodeResults.forEach(function (nodeResult) {
var groupName = axe.constants.resultGroupMap[nodeResult.result];
ruleResult[groupName].push(nodeResult);
});

// Take the highest impact of failed or canttell rules
Expand All @@ -54,4 +59,4 @@ axe.utils.aggregateRule = function (subResults) {
return ruleResult;
};

}());
}());
2 changes: 1 addition & 1 deletion lib/core/utils/finalize-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @return {object}
*/
axe.utils.finalizeRuleResult = function (ruleResult) {
Object.assign(ruleResult, axe.utils.aggregateRule(ruleResult.nodes));
Object.assign(ruleResult, axe.utils.aggregateNodeResults(ruleResult.nodes));
delete ruleResult.nodes;

return ruleResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('axe.utils.aggregateRule', function() {
describe('axe.utils.aggregateNodeResults', function() {
'use strict';
var FAIL = 'failed';
var PASS = 'passed';
Expand Down Expand Up @@ -38,20 +38,16 @@ describe('axe.utils.aggregateRule', function() {
});

it('should be a function', function() {
assert.isFunction(axe.utils.aggregateRule);
assert.isFunction(axe.utils.aggregateNodeResults);
});

it('Should be `inapplicable` when no results are given', function () {
var ruleResult = axe.utils.aggregateRule( createTestResults(
{}, {}
));

var ruleResult = axe.utils.aggregateNodeResults([]);
assert.equal(ruleResult.result, INAPPLICABLE);
assert.lengthOf(ruleResult.inapplicable, 2);
});

it('should assign FAIL to ruleResult over PASS', function() {
var ruleResult = axe.utils.aggregateRule( createTestResults(
var ruleResult = axe.utils.aggregateNodeResults( createTestResults(
{ all: false },
{ all: true },
{ all: true }
Expand All @@ -62,7 +58,7 @@ describe('axe.utils.aggregateRule', function() {
});

it('should assign FAIL to ruleResult over CANTTELL', function() {
var ruleResult = axe.utils.aggregateRule( createTestResults(
var ruleResult = axe.utils.aggregateNodeResults( createTestResults(
{ all: false },
{ all: 0 },
{ all: true }
Expand All @@ -74,7 +70,7 @@ describe('axe.utils.aggregateRule', function() {
});

it('should assign PASS to ruleResult if there are only passing checks', function() {
var ruleResult = axe.utils.aggregateRule( createTestResults(
var ruleResult = axe.utils.aggregateNodeResults( createTestResults(
{ all: true },
{ all: true },
{ all: true }
Expand All @@ -86,7 +82,7 @@ describe('axe.utils.aggregateRule', function() {
});

it('should assign FAIL if there are no passing anys checks', function() {
var ruleResult = axe.utils.aggregateRule( createTestResults(
var ruleResult = axe.utils.aggregateNodeResults( createTestResults(
{ any: false },
{ any: false },
{ any: false }
Expand All @@ -97,7 +93,7 @@ describe('axe.utils.aggregateRule', function() {
});

it('should assign CANTTELL over PASS', function() {
var ruleResult = axe.utils.aggregateRule( createTestResults(
var ruleResult = axe.utils.aggregateNodeResults( createTestResults(
{ all: true },
{ all: 0 },
{ all: 0 }
Expand All @@ -108,14 +104,14 @@ describe('axe.utils.aggregateRule', function() {
});

it('should provide impact on incomplete', function () {
var ruleResult = axe.utils.aggregateRule( createTestResults({
var ruleResult = axe.utils.aggregateNodeResults( createTestResults({
none: { result: undefined, impact: 'serious' }
}));
assert.equal(ruleResult.impact, 'serious');
});

it('should raise the highest "raisedMetadata" on failing checks', function() {
var ruleResult = axe.utils.aggregateRule( createTestResults({
var ruleResult = axe.utils.aggregateNodeResults( createTestResults({
none: { result: true, impact: 'moderate' },
any: { result: true, impact: 'minor' },
all: [
Expand Down
14 changes: 7 additions & 7 deletions test/core/utils/finalize-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@ describe('axe.utils.finalizeRuleResult', function() {

var origAggregate;
beforeEach(function() {
origAggregate = axe.utils.aggregateRule;
origAggregate = axe.utils.aggregateNodeResults;
});

afterEach(function () {
axe.utils.aggregateRule = origAggregate;
axe.utils.aggregateNodeResults = origAggregate;
});

it('should be a function', function() {
assert.isFunction(axe.utils.finalizeRuleResult);
});

it('returns the first param object', function () {
axe.utils.aggregateRule = function () {};
axe.utils.aggregateNodeResults = function () {};

var goingIn = {};
var comingOut = axe.utils.finalizeRuleResult(goingIn);

assert.equal(goingIn, comingOut);
});

it('passes the nodes property to utils.aggregateRule', function() {
it('passes the nodes property to utils.aggregateNodeResults', function() {
var isCalled = false;
var nodes = [];
axe.utils.aggregateRule = function (n) {
axe.utils.aggregateNodeResults = function (n) {
assert.equal(n, nodes);
isCalled = true;
};
axe.utils.finalizeRuleResult({ nodes: nodes });
assert.ok(isCalled);
});

it('adds properties returned from utils.aggregateRule to the return value', function() {
axe.utils.aggregateRule = function () {
it('adds properties returned from utils.aggregateNodeResults to the return value', function() {
axe.utils.aggregateNodeResults = function () {
return {
vulgaris:'magistralis'
};
Expand Down

0 comments on commit 0a2a5cc

Please sign in to comment.