diff --git a/addon-test-support/format-violation.ts b/addon-test-support/format-violation.ts index bd7891ac..ae824ed7 100644 --- a/addon-test-support/format-violation.ts +++ b/addon-test-support/format-violation.ts @@ -3,18 +3,13 @@ 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} violation + * @param {string[]} markup (optional) string of HTML relevant to the violation */ export default function formatViolation( - violation: Partial | undefined, - markup?: string | string[] + violation: Partial, + 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.' @@ -22,19 +17,15 @@ export default function formatViolation( } 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}`; } diff --git a/tests/unit/format-violation-test.ts b/tests/unit/format-violation-test.ts index 1f0f385e..4cfc166d 100644 --- a/tests/unit/format-violation-test.ts +++ b/tests/unit/format-violation-test.ts @@ -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\nhttp://example.com`; assert.equal(message, expected); }); @@ -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); }); @@ -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); }); });