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

Fixes issue when violations count is reported to be 0 #272

Merged
merged 2 commits into from
Jul 1, 2021
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
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) {
scalvert marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});