import { useState } from "react"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, expect, it, vi } from "vitest"; import { Switch } from "./switch"; describe("Switch", () => { it("toggles by click and exposes slot/state hooks", async () => { const user = userEvent.setup(); const onCheckedChange = vi.fn(); render(); const control = screen.getByRole("switch", { name: "Email alerts" }); expect(control).toHaveAttribute("data-slot", "root"); expect(control).toHaveAttribute("data-state", "unchecked"); expect(control.querySelector('[data-slot="control"]')).not.toBeNull(); await user.click(control); expect(control).toBeChecked(); expect(control).toHaveAttribute("data-state", "checked"); expect(onCheckedChange).toHaveBeenCalledWith(true); }); it("supports controlled state updates", async () => { const user = userEvent.setup(); function ControlledSwitch() { const [checked, setChecked] = useState(false); return ; } render(); const control = screen.getByRole("switch", { name: "Controlled switch" }); expect(control).not.toBeChecked(); await user.click(control); expect(control).toBeChecked(); }); it("supports keyboard interaction and disabled/invalid states", async () => { const user = userEvent.setup(); render( <> ); const keyboardSwitch = screen.getByRole("switch", { name: "Keyboard switch" }); const disabledSwitch = screen.getByRole("switch", { name: "Disabled switch" }); await user.tab(); expect(keyboardSwitch).toHaveFocus(); await user.keyboard(" "); expect(keyboardSwitch).toBeChecked(); expect(disabledSwitch).toBeDisabled(); expect(disabledSwitch).toHaveAttribute("aria-invalid", "true"); expect(disabledSwitch).toHaveAttribute("data-disabled", ""); }); });