Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions components/CardActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function CardActions({ login, name, rtg, finishLabel }: { login: string;
<button
type="button"
role="menuitem"
aria-label="Copy embed Markdown"
className="menu-item"
onClick={() => {
copy(`[![My gitone card](${pngUrl})](${origin}/${login})`);
Expand All @@ -74,6 +75,7 @@ export function CardActions({ login, name, rtg, finishLabel }: { login: string;
<button
type="button"
role="menuitem"
aria-label="Copy JSON API URL"
className="menu-item"
onClick={() => {
copy(`${origin}/api/card/${login}`);
Expand Down
4 changes: 2 additions & 2 deletions components/SplitDownload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ export function SplitDownload({
onKeyDown={onMenuKeyDown}
className={`menu-pop absolute right-0 z-30 w-[236px] p-1.5 ${dropUp ? "bottom-full mb-2" : "top-full mt-2"}`}
>
<button type="button" role="menuitem" onClick={copyImage} className="menu-item">
<button type="button" role="menuitem" aria-label="Copy image" onClick={copyImage} className="menu-item">
<Copy size={15} aria-hidden /> Copy image
</button>
<button type="button" role="menuitem" onClick={copyLink} className="menu-item">
<button type="button" role="menuitem" aria-label="Copy link" onClick={copyLink} className="menu-item">
<Link2 size={15} aria-hidden /> Copy link
</button>
{extraItems?.(() => setMenuOpen(false))}
Expand Down
81 changes: 81 additions & 0 deletions tests/actions-a11y.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import("@/hooks/useImageActions")>();
return {
...actual,
useImageActions: (args: Parameters<typeof actual.useImageActions>[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("<svg") && !visibleText(a.body));
expect(iconOnly.length).toBeGreaterThan(0);
for (const a of iconOnly) {
expect(ariaLabel(a), `${a.tag}: ${a.attrs}`).toBeTruthy();
}
}
expect(cardHtml).toContain('aria-label="Share on X"');
expect(cardHtml).toContain('aria-label="Download card as a PNG"');
expect(battleHtml).toContain('aria-label="Download result as a PNG"');
});

it("keeps a stable name for copy, link and embed items when their text flashes to Copied", () => {
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");
});
});
Loading