From 38b2d98a0dbdcc7ff9f563d0d56a84170eaf1137 Mon Sep 17 00:00:00 2001 From: oussamabadr Date: Wed, 21 Sep 2022 05:00:40 +0200 Subject: [PATCH] Replace violation callback with AxeResults Callback --- src/index.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 89d9981..fdcadb5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,7 +49,7 @@ function isEmptyObjectorNull(value: any) { const checkA11y = ( context?: axe.ElementContext, options?: Options, - violationCallback?: (violations: axe.Result[]) => void, + axeResultsCallback?: (axeResults: axe.AxeResults) => void, skipFailures = false ) => { cy.window({ log: false }) @@ -60,27 +60,32 @@ const checkA11y = ( if (isEmptyObjectorNull(options)) { options = undefined; } - if (isEmptyObjectorNull(violationCallback)) { - violationCallback = undefined; + if (isEmptyObjectorNull(axeResultsCallback)) { + axeResultsCallback = undefined; } const { includedImpacts, ...axeOptions } = options || {}; return win.axe .run(context || win.document, axeOptions) - .then(({ violations }) => { - return includedImpacts && + .then((results) => { + if ( + includedImpacts && Array.isArray(includedImpacts) && Boolean(includedImpacts.length) - ? violations.filter( - (v) => v.impact && includedImpacts.includes(v.impact) - ) - : violations; + ) { + results.violations = results.violations.filter( + (v) => v.impact && includedImpacts.includes(v.impact) + ); + } + return results; }); }) - .then((violations) => { + .then((results) => { + if (axeResultsCallback) { + axeResultsCallback(results); + } + + const violations = results.violations; if (violations.length) { - if (violationCallback) { - violationCallback(violations); - } violations.forEach((v) => { const selectors = v.nodes .reduce((acc, node) => acc.concat(node.target), [])