Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+48
@@ -0,0 +1,48 @@
|
||||
import { createElement, type ReactElement, type ReactNode } from "react";
|
||||
import { afterAll, describe, expect, vi } from "vitest";
|
||||
|
||||
type AxeCall = {
|
||||
ui: ReactElement;
|
||||
options: unknown;
|
||||
};
|
||||
|
||||
const axeCalls: AxeCall[] = [];
|
||||
|
||||
vi.mock("../../test-setup/axe.js", () => ({
|
||||
expectNoAxeViolations: vi.fn(async (ui: ReactElement, options: unknown) => {
|
||||
axeCalls.push({ ui, options });
|
||||
}),
|
||||
}));
|
||||
|
||||
import { generateA11yTest } from "./a11y-template.js";
|
||||
|
||||
function NamedComponent({ children }: { readonly children?: ReactNode }) {
|
||||
return createElement("button", null, children);
|
||||
}
|
||||
|
||||
function DisplayNamedComponent({ labelKey }: { readonly labelKey: string }) {
|
||||
return createElement("span", null, labelKey);
|
||||
}
|
||||
DisplayNamedComponent.displayName = "FriendlyDisplayName";
|
||||
|
||||
const NamelessComponent = Object.defineProperty(
|
||||
() => createElement("div", null, "nameless"),
|
||||
"name",
|
||||
{ value: "" },
|
||||
);
|
||||
|
||||
describe("generateA11yTest", () => {
|
||||
generateA11yTest(NamedComponent, { children: "Save" }, { axe: { tags: ["wcag2a"] } });
|
||||
generateA11yTest(DisplayNamedComponent, { labelKey: "common.status" });
|
||||
generateA11yTest(NamelessComponent, {}, { name: "ExplicitName" });
|
||||
|
||||
afterAll(() => {
|
||||
expect(axeCalls).toHaveLength(3);
|
||||
expect(axeCalls[0]?.ui.type).toBe(NamedComponent);
|
||||
expect(axeCalls[0]?.ui.props).toMatchObject({ children: "Save" });
|
||||
expect(axeCalls[0]?.options).toEqual({ tags: ["wcag2a"] });
|
||||
expect(axeCalls[1]?.ui.type).toBe(DisplayNamedComponent);
|
||||
expect(axeCalls[1]?.ui.props).toMatchObject({ labelKey: "common.status" });
|
||||
expect(axeCalls[2]?.ui.type).toBe(NamelessComponent);
|
||||
});
|
||||
});
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* `generateA11yTest` is the canonical test-template documented in the WCAG
|
||||
* internal-coverage spec
|
||||
* (`docs/improvement-plan/10-compliance-polish/wcag-internal.md`, section a).
|
||||
*
|
||||
* Components in `packages/ui-app` and `packages/ui-subframe` should ship a
|
||||
* co-located `<ComponentName>.a11y.test.tsx` that simply calls this helper
|
||||
* with sensible default props. Complex components (Modal, Combobox,
|
||||
* DataTable, …) keep additional manually-authored tests for focus-trap and
|
||||
* keyboard-navigation behaviour.
|
||||
*
|
||||
* The helper intentionally lives in the public test-utils tree so that
|
||||
* consumer packages (apps/web, apps/storybook) can re-use the same template
|
||||
* without copying boilerplate.
|
||||
*/
|
||||
import type { ComponentType, ComponentProps, ReactNode } from "react";
|
||||
import { describe, it } from "vitest";
|
||||
import { createElement } from "react";
|
||||
|
||||
import { expectNoAxeViolations } from "../../test-setup/axe.js";
|
||||
|
||||
export interface GenerateA11yTestOptions {
|
||||
/**
|
||||
* Human-readable name shown in test output. Defaults to
|
||||
* `<Component.displayName>` or `<Component.name>`.
|
||||
*/
|
||||
readonly name?: string;
|
||||
/**
|
||||
* Optional axe-rule overrides (see {@link AxeRunOptions} in
|
||||
* `test-setup/axe.ts`). Use to relax rules for components that
|
||||
* intentionally violate them in isolation (rare).
|
||||
*/
|
||||
readonly axe?: Parameters<typeof expectNoAxeViolations>[1];
|
||||
}
|
||||
|
||||
function displayName<P>(Component: ComponentType<P>): string {
|
||||
return (Component as { displayName?: string }).displayName
|
||||
?? (Component as { name?: string }).name
|
||||
?? "Component";
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit a `describe(... a11y) { it("has no axe violations") }` block for the
|
||||
* given component + default props.
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* import { Button } from "./Button";
|
||||
* import { generateA11yTest } from "../test-utils/a11y-template";
|
||||
*
|
||||
* generateA11yTest(Button, { children: "Click me" });
|
||||
* ```
|
||||
*/
|
||||
export function generateA11yTest<T extends ComponentType<any>>(
|
||||
Component: T,
|
||||
defaultProps: Partial<ComponentProps<T>> & { children?: ReactNode },
|
||||
options: GenerateA11yTestOptions = {},
|
||||
): void {
|
||||
const label = options.name ?? displayName(Component);
|
||||
describe(`${label} a11y`, () => {
|
||||
it("has no axe violations with default props", async () => {
|
||||
await expectNoAxeViolations(
|
||||
createElement(Component as ComponentType<any>, defaultProps as any),
|
||||
options.axe,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user