Skip to content

Commit

Permalink
Merge pull request #272 from ember-a11y/fix-zero-count
Browse files Browse the repository at this point in the history
Fixes issue when violations count is reported to be 0
  • Loading branch information
scalvert committed Jul 1, 2021
2 parents f142821 + 417f4e5 commit 162cf0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
27 changes: 9 additions & 18 deletions addon-test-support/format-violation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,29 @@ import type { Result } from 'axe-core';
/**
* Formats the axe violation for human consumption
*
* @param {AxeViolation} violation
* @param {string | string[]} markup (optional) string of HTML relevant to the violation
* @param {Partial<Result>} violation
* @param {string[]} markup (optional) string of HTML relevant to the violation
*/
export default function formatViolation(
violation: Partial<Result> | undefined,
markup?: string | string[]
violation: Partial<Result>,
markup: string[]
) {
if (!violation) {
throw new Error(
'formatViolation called without required parameter: violation'
);
}
if (!violation.impact || !violation.help || !violation.helpUrl) {
throw new Error(
'formatViolation called with improper structure of parameter: violation. Required properties: impact, help, helpUrl.'
);
}

let count = 1;
let formattedMarkup = '';

if (markup) {
if (Array.isArray(markup)) {
count = markup.length;
markup = markup.join('\n');
}
markup = ` Offending nodes are: \n${markup}`;
} else {
markup = '';
if (markup.length) {
count = markup.length;
formattedMarkup = ` Offending nodes are: \n${markup.join('\n')}`;
}

let plural = count === 1 ? '' : 's';
let violationCount = `Violated ${count} time${plural}.`;

return `[${violation.impact}]: ${violation.help} \n${violationCount}${markup}\n${violation.helpUrl}`;
return `[${violation.impact}]: ${violation.help} \n${violationCount}${formattedMarkup}\n${violation.helpUrl}`;
}
14 changes: 3 additions & 11 deletions tests/unit/format-violation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module('Unit | Utils | formatViolation', function () {
],
};

let message = formatViolation(violation, violation.nodes[0].html);
let message = formatViolation(violation, [violation.nodes[0].html]);
let expected = `[critical]: it should be better \nViolated 1 time. Offending nodes are: \n<input type="text">\nhttp://example.com`;
assert.equal(message, expected);
});
Expand All @@ -38,7 +38,7 @@ module('Unit | Utils | formatViolation', function () {
nodes: [],
};

let message = formatViolation(violation);
let message = formatViolation(violation, []);
let expected = `[critical]: it should be better \nViolated 1 time.\nhttp://example.com`;
assert.equal(message, expected);
});
Expand All @@ -64,15 +64,7 @@ module('Unit | Utils | formatViolation', function () {
let expected = /formatViolation called with improper structure of parameter: violation. Required properties: impact, help, helpUrl./;

assert.throws(function () {
formatViolation(violation, violation.nodes[0].html);
}, expected);
});

test('validates violation parameter exists', function (assert) {
let expected = /formatViolation called without required parameter: violation/;

assert.throws(function () {
formatViolation(undefined);
formatViolation(violation, [violation.nodes[0].html]);
}, expected);
});
});

0 comments on commit 162cf0c

Please sign in to comment.