Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/components/ui/tabs/TabsWithIcons.a11y.test.tsx
T

60 lines
2.2 KiB
TypeScript

import "@testing-library/jest-dom";
import { render, screen, within } from "@testing-library/react";
import { MemoryRouter } from "react-router";
import { describe, expect, it, vi } from "vitest";
import { assertNoA11yViolations } from "~/utils/test/a11y";
import TabsWithIcons from "./TabsWithIcons";
vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));
describe("TabsWithIcons accessibility", () => {
it("exposes selected state for link tabs and button tabs", () => {
render(
<MemoryRouter>
<TabsWithIcons
tabs={[
{ name: "Overview", href: "/overview", current: true },
{ name: "Settings", href: "/settings", current: false },
{ name: "Recent", onClick: vi.fn(), current: false },
{ name: "Pinned", onClick: vi.fn(), current: true },
]}
/>
</MemoryRouter>
);
expect(screen.getByRole("link", { name: "Overview" })).toHaveAttribute("aria-current", "page");
expect(screen.getByRole("link", { name: "Settings" })).not.toHaveAttribute("aria-current");
expect(screen.getByRole("button", { name: "Recent" })).toHaveAttribute("aria-pressed", "false");
expect(screen.getByRole("button", { name: "Pinned" })).toHaveAttribute("aria-pressed", "true");
});
it("keeps mobile select labels unique when multiple instances render", () => {
const { container } = render(
<MemoryRouter>
<TabsWithIcons
tabs={[
{ name: "First", href: "/first", current: true },
{ name: "Second", href: "/second", current: false },
]}
/>
<TabsWithIcons
tabs={[
{ name: "Third", href: "/third", current: false },
{ name: "Fourth", href: "/fourth", current: true },
]}
/>
</MemoryRouter>
);
const selects = screen.getAllByRole("combobox", { name: "shared.select" });
expect(selects).toHaveLength(2);
expect(selects[0]).not.toHaveAttribute("id", selects[1].getAttribute("id") ?? "");
expect(within(selects[0]).getByRole("option", { name: "First" })).toBeInTheDocument();
expect(within(selects[1]).getByRole("option", { name: "Fourth" })).toBeInTheDocument();
assertNoA11yViolations(container);
});
});