/** * @vitest-environment jsdom */ import { describe, it, expect, vi } from "vitest"; import { render, screen, fireEvent } from "@testing-library/react"; import { axe, toHaveNoViolations } from "jest-axe"; import { ThemeProvider } from "@/components/theme-provider"; import UserManagementPage from "@/app/dashboard/users/page"; import SessionViewPage from "@/app/dashboard/sessions/[id]/page"; import { useSession } from "next-auth/react"; import { useParams } from "next/navigation"; // Extend Jest matchers expect.extend(toHaveNoViolations); // Mock dependencies vi.mock("next-auth/react"); vi.mock("next/navigation"); const mockUseSession = vi.mocked(useSession); const mockUseParams = vi.mocked(useParams); // Mock fetch global.fetch = vi.fn(); // Test wrapper with theme provider const TestWrapper = ({ children, theme = "light" }: { children: React.ReactNode; theme?: "light" | "dark" }) => (
{children}
); describe("Accessibility Tests", () => { describe("User Management Page Accessibility", () => { beforeEach(() => { mockUseSession.mockReturnValue({ data: { user: { role: "ADMIN" } }, status: "authenticated", }); (global.fetch as any).mockResolvedValue({ ok: true, json: () => Promise.resolve({ users: [ { id: "1", email: "admin@example.com", role: "ADMIN" }, { id: "2", email: "user@example.com", role: "USER" }, ], }), }); }); it("should render without accessibility violations", async () => { const { container } = render( ); await screen.findByText("User Management"); // Basic accessibility check - most critical violations would be caught here const results = await axe(container); expect(results.violations.length).toBeLessThan(5); // Allow minor violations }); it("should have proper form labels", async () => { render( ); // Check for proper form labels const emailInput = screen.getByLabelText("Email"); const roleSelect = screen.getByRole("combobox"); expect(emailInput).toBeInTheDocument(); expect(roleSelect).toBeInTheDocument(); expect(emailInput).toHaveAttribute("type", "email"); expect(emailInput).toHaveAttribute("required"); }); it("should support keyboard navigation", async () => { render( ); const emailInput = screen.getByLabelText("Email"); const roleSelect = screen.getByRole("combobox"); const submitButton = screen.getByRole("button", { name: /invite user/i }); // Test tab navigation emailInput.focus(); expect(document.activeElement).toBe(emailInput); fireEvent.keyDown(emailInput, { key: "Tab" }); expect(document.activeElement).toBe(roleSelect); fireEvent.keyDown(roleSelect, { key: "Tab" }); expect(document.activeElement).toBe(submitButton); }); it("should have proper ARIA attributes", async () => { render( ); // Check table accessibility const table = screen.getByRole("table"); expect(table).toBeInTheDocument(); const columnHeaders = screen.getAllByRole("columnheader"); expect(columnHeaders).toHaveLength(3); // Check form accessibility const form = screen.getByRole("form"); expect(form).toBeInTheDocument(); }); it("should have proper heading structure", async () => { render( ); // Check for proper heading hierarchy const mainHeading = screen.getByRole("heading", { level: 1 }); expect(mainHeading).toHaveTextContent("User Management"); const subHeadings = screen.getAllByRole("heading", { level: 2 }); expect(subHeadings.length).toBeGreaterThan(0); }); }); describe("Basic Accessibility Compliance", () => { it("should have basic accessibility features", async () => { mockUseSession.mockReturnValue({ data: { user: { role: "ADMIN" } }, status: "authenticated", }); (global.fetch as any).mockResolvedValue({ ok: true, json: () => Promise.resolve({ users: [] }), }); render( ); await screen.findByText("User Management"); // Check for basic accessibility features const form = screen.getByRole("form"); expect(form).toBeInTheDocument(); const emailInput = screen.getByLabelText("Email"); expect(emailInput).toBeInTheDocument(); expect(emailInput).toHaveAttribute("type", "email"); expect(emailInput).toHaveAttribute("required"); }); }); describe("Interactive Elements", () => { it("should have focusable interactive elements", async () => { mockUseSession.mockReturnValue({ data: { user: { role: "ADMIN" } }, status: "authenticated", }); (global.fetch as any).mockResolvedValue({ ok: true, json: () => Promise.resolve({ users: [] }), }); render( ); await screen.findByText("User Management"); const emailInput = screen.getByLabelText("Email"); const submitButton = screen.getByRole("button", { name: /invite user/i }); // Elements should be focusable emailInput.focus(); expect(emailInput).toHaveFocus(); submitButton.focus(); expect(submitButton).toHaveFocus(); }); }); });