From cdc83f967b2b49e5299f2c69e14c99b4de064fc6 Mon Sep 17 00:00:00 2001 From: LangerSword Date: Tue, 24 Nov 2026 22:01:09 +0530 Subject: [PATCH] fix(a11y): label icon-only card and battle actions --- components/CardActions.tsx | 2 + components/SplitDownload.tsx | 4 +- tests/actions-a11y.test.ts | 81 ++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 tests/actions-a11y.test.ts diff --git a/components/CardActions.tsx b/components/CardActions.tsx index 653972b..a27d1f2 100644 --- a/components/CardActions.tsx +++ b/components/CardActions.tsx @@ -61,6 +61,7 @@ export function CardActions({ login, name, rtg, finishLabel }: { login: string; - {extraItems?.(() => setMenuOpen(false))} diff --git a/tests/actions-a11y.test.ts b/tests/actions-a11y.test.ts new file mode 100644 index 0000000..1515f08 --- /dev/null +++ b/tests/actions-a11y.test.ts @@ -0,0 +1,81 @@ +import { createElement } from "react"; +import { renderToStaticMarkup } from "react-dom/server"; +import { describe, expect, it, vi } from "vitest"; + +// Menu items are closed on first paint; open the menu so its actions are in +// the rendered DOM we assert on. useRouter is a Next context hook. +vi.mock("next/navigation", () => ({ useRouter: () => ({ push: () => {} }) })); +vi.mock("@/hooks/useImageActions", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + useImageActions: (args: Parameters[0]) => ({ + ...actual.useImageActions(args), + menuOpen: true, + }), + }; +}); + +import { BattleActions } from "../components/BattleActions"; +import { CardActions } from "../components/CardActions"; + +type Action = { tag: string; attrs: string; body: string }; + +function actions(html: string): Action[] { + return Array.from(html.matchAll(/<(button|a)\b([^>]*)>([\s\S]*?)<\/\1>/g), (m) => ({ + tag: m[1], + attrs: m[2], + body: m[3], + })); +} + +function ariaLabel(a: Action): string | null { + return a.attrs.match(/aria-label="([^"]*)"/)?.[1] ?? null; +} + +function visibleText(html: string): string { + return html.replace(/<[^>]*>/g, "").replace(/\{[^}]*\}/g, "").trim(); +} + +const cardHtml = renderToStaticMarkup( + createElement(CardActions, { login: "testdriver", name: "Test Driver", rtg: 91 }), +); +const battleHtml = renderToStaticMarkup( + createElement(BattleActions, { + username: "testdriver", + opponent: "rival", + text: "race", + duel: { rows: [], score: { challenger: 0, opponent: 0 }, aColor: "#fff", bColor: "#000", subtitle: "" }, + }), +); + +describe("card and battle action labels", () => { + it("names every icon-only action in the rendered DOM", () => { + for (const html of [cardHtml, battleHtml]) { + const iconOnly = actions(html).filter((a) => a.body.includes(" { + const menuLabels = actions(cardHtml) + .filter((a) => a.attrs.includes('role="menuitem"')) + .map(ariaLabel); + expect(menuLabels).toEqual( + expect.arrayContaining(["Copy image", "Copy link", "Copy embed Markdown", "Copy JSON API URL"]), + ); + }); + + it("keeps the visible copy unchanged", () => { + expect(cardHtml).toContain("Copy embed Markdown"); + expect(cardHtml).toContain("Copy JSON API URL"); + expect(cardHtml).toContain("Download card"); + expect(battleHtml).toContain("Download result"); + }); +});