Skip to content

Commit 2197647

Browse files
committed
Filter accessibility violations before unmounting
Change a potentially confusing behavior where components were unmounted before calling the `shouldIgnoreViolation` callback. Querying the rendered tree within the callback, eg. to get the ID of an element to check if a violation relates to it, could fail due to this. Fixes #48.
1 parent a076a7e commit 2197647

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/accessibility.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export type Scenario = {
2929
shouldIgnoreViolation?: (violation: Result) => boolean;
3030
};
3131

32-
async function testScenario(elementOrWrapper: VNode | ReactWrapper) {
32+
async function testScenario(
33+
elementOrWrapper: VNode | ReactWrapper,
34+
{ shouldIgnoreViolation }: Pick<Scenario, 'shouldIgnoreViolation'>,
35+
) {
3336
const container = document.createElement('div');
3437
document.body.appendChild(container);
3538

@@ -52,10 +55,16 @@ async function testScenario(elementOrWrapper: VNode | ReactWrapper) {
5255
// or "inapplicable" (no relevant HTML elements found).
5356
resultTypes: ['violations'],
5457
});
58+
59+
let violations = results.violations;
60+
if (shouldIgnoreViolation) {
61+
violations = violations.filter(v => !shouldIgnoreViolation(v));
62+
}
63+
5564
wrapper.unmount();
5665
container.remove();
5766

58-
return results.violations;
67+
return violations;
5968
}
6069

6170
function asArray<T>(itemOrList: T | T[]): T[] {
@@ -95,14 +104,13 @@ export function checkAccessibility(
95104
);
96105
}
97106

98-
const violations = await testScenario(elementOrWrapper);
99-
const filteredViolations = shouldIgnoreViolation
100-
? violations.filter(v => !shouldIgnoreViolation(v))
101-
: violations;
102-
for (const violation of filteredViolations) {
107+
const violations = await testScenario(elementOrWrapper, {
108+
shouldIgnoreViolation,
109+
});
110+
for (const violation of violations) {
103111
console.error('axe-core violation', JSON.stringify(violation, null, 2));
104112
}
105-
if (filteredViolations.length > 0) {
113+
if (violations.length > 0) {
106114
throw new Error(`Scenario "${name}" has accessibility violations`);
107115
}
108116
}

0 commit comments

Comments
 (0)