import React from "react"; import { render, screen } from "@testing-library/react"; import { expect, test, describe } from "vitest"; import { axe, toHaveNoViolations } from "jest-axe"; import Checkbox from "../../../app/components/Checkbox"; expect.extend(toHaveNoViolations); describe("Checkbox Accessibility", () => { test("should not have accessibility violations when unchecked", async () => { const { container } = render(); const results = await axe(container); expect(results).toHaveNoViolations(); }); test("should not have accessibility violations when checked", async () => { const { container } = render( , ); const results = await axe(container); expect(results).toHaveNoViolations(); }); test("should not have accessibility violations when disabled", async () => { const { container } = render( , ); const results = await axe(container); expect(results).toHaveNoViolations(); }); test("should not have accessibility violations in inverse mode", async () => { const { container } = render( , ); const results = await axe(container); expect(results).toHaveNoViolations(); }); test("should have proper ARIA attributes", () => { render(); const checkbox = screen.getByRole("checkbox"); expect(checkbox).toHaveAttribute("role", "checkbox"); expect(checkbox).toHaveAttribute("aria-checked", "true"); expect(checkbox).toHaveAttribute("tabIndex", "0"); }); test("should have proper ARIA attributes when disabled", () => { render(); const checkbox = screen.getByRole("checkbox"); expect(checkbox).toHaveAttribute("role", "checkbox"); expect(checkbox).toHaveAttribute("aria-checked", "false"); expect(checkbox).toHaveAttribute("aria-disabled", "true"); expect(checkbox).toHaveAttribute("tabIndex", "-1"); }); test("should have proper ARIA attributes when checked", () => { render(); const checkbox = screen.getByRole("checkbox"); expect(checkbox).toHaveAttribute("role", "checkbox"); expect(checkbox).toHaveAttribute("aria-checked", "true"); expect(checkbox).toHaveAttribute("tabIndex", "0"); }); test("should have proper ARIA attributes when unchecked", () => { render(); const checkbox = screen.getByRole("checkbox"); expect(checkbox).toHaveAttribute("role", "checkbox"); expect(checkbox).toHaveAttribute("aria-checked", "false"); expect(checkbox).toHaveAttribute("tabIndex", "0"); }); test("should have proper ARIA attributes with custom aria-label", () => { render(); const checkbox = screen.getByRole("checkbox"); expect(checkbox).toHaveAttribute( "aria-label", "Custom accessibility label", ); }); test("should have proper focus management", () => { const { rerender } = render(); const checkbox = screen.getByRole("checkbox"); // Should be focusable when not disabled expect(checkbox).toHaveAttribute("tabIndex", "0"); // Should not be focusable when disabled rerender(); const disabledCheckbox = screen.getByRole("checkbox"); expect(disabledCheckbox).toHaveAttribute("tabIndex", "-1"); }); test("should have proper keyboard navigation", () => { render(); const checkbox = screen.getByRole("checkbox"); // Should be focusable expect(checkbox).toHaveAttribute("tabIndex", "0"); // Should support keyboard interaction expect(checkbox).toHaveAttribute("role", "checkbox"); }); test("should have proper semantic structure", () => { render(); // Should have a label element const label = screen.getByText("Test checkbox").closest("label"); expect(label).toBeInTheDocument(); // Should have a checkbox role const checkbox = screen.getByRole("checkbox"); expect(checkbox).toBeInTheDocument(); // Should be associated with the label expect(label).toContainElement(checkbox); }); test("should have proper color contrast", async () => { const { container } = render(); const results = await axe(container); // Check for color contrast violations const contrastViolations = results.violations.filter( (violation) => violation.id === "color-contrast", ); expect(contrastViolations).toHaveLength(0); }); test("should have proper focus indicators", async () => { const { container } = render(); const results = await axe(container); // Check for focus indicator violations const focusViolations = results.violations.filter( (violation) => violation.id === "focus-order-semantics", ); expect(focusViolations).toHaveLength(0); }); test("should have proper form integration", () => { render(); // Should have hidden input for form submission const hiddenInput = screen.getByDisplayValue("test-value"); expect(hiddenInput).toBeInTheDocument(); expect(hiddenInput).toHaveAttribute("type", "checkbox"); expect(hiddenInput).toHaveAttribute("name", "test-checkbox"); expect(hiddenInput).toBeChecked(); }); });