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

Add shouldIgnoreViolation optional prop to Scenario for a11y tests #12

Merged
merged 1 commit into from
Nov 8, 2023
Merged
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
20 changes: 17 additions & 3 deletions src/accessibility.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { run } from 'axe-core';
import type { Result } from 'axe-core';
import { ReactWrapper, mount } from 'enzyme';
import { isValidElement } from 'preact';
import type { VNode } from 'preact';
Expand All @@ -18,6 +19,14 @@ export type Scenario = {
* component with a `<div />` in that case.
*/
content: () => VNode | ReactWrapper;

/**
* This callback will be applied to all found accessibility violations,
* allowing you to ignore those that are not relevant for your test.
* A use case for this is any combination that axe-core considers invalid
* because certain screen readers won't play well, but their use is marginal.
*/
shouldIgnoreViolation?: (violation: Result) => boolean;
};

async function testScenario(elementOrWrapper: VNode | ReactWrapper) {
Expand Down Expand Up @@ -66,7 +75,9 @@ export function checkAccessibility(
scenarios: Scenario | Scenario[],
): () => Promise<void> {
return async () => {
for (const { name = 'default', content } of asArray(scenarios)) {
for (const { name = 'default', content, shouldIgnoreViolation } of asArray(
scenarios,
)) {
if (typeof content !== 'function') {
throw new Error(
`"content" key for accessibility scenario "${name}" should be a function but is a ${typeof content}`,
Expand All @@ -85,10 +96,13 @@ export function checkAccessibility(
}

const violations = await testScenario(elementOrWrapper);
for (const violation of violations) {
const filteredViolations = shouldIgnoreViolation
? violations.filter(v => !shouldIgnoreViolation(v))
: violations;
for (const violation of filteredViolations) {
console.error('axe-core violation', JSON.stringify(violation, null, 2));
}
if (violations.length > 0) {
if (filteredViolations.length > 0) {
throw new Error(`Scenario "${name}" has accessibility violations`);
}
}
Expand Down