/** * @file Sidebar.theme.test.tsx * @description Tests for the Sidebar's theme controls: the expanded panel's Dark / Light / System buttons and the collapsed one-click toggle, both writing through useTheme. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect, beforeEach } from "vitest"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { MemoryRouter } from "react-router-dom"; import { Sidebar } from "../Sidebar"; function renderSidebar(collapsed = false) { return render( {}} /> ); } describe("Sidebar theme controls", () => { beforeEach(() => { localStorage.clear(); document.documentElement.classList.remove("dark"); }); it("expanded panel offers Dark, Light and System with Dark pressed by default", () => { renderSidebar(); expect(screen.getByRole("button", { name: "Dark" })).toHaveAttribute("aria-pressed", "true"); expect(screen.getByRole("button", { name: "Light" })).toHaveAttribute("aria-pressed", "false"); expect(screen.getByRole("button", { name: "System" })).toHaveAttribute("aria-pressed", "false"); }); it("clicking System sets the mode and presses only System", async () => { const user = userEvent.setup(); renderSidebar(); await user.click(screen.getByRole("button", { name: "System" })); expect(localStorage.getItem("theme")).toBe("system"); expect(screen.getByRole("button", { name: "System" })).toHaveAttribute("aria-pressed", "true"); expect(screen.getByRole("button", { name: "Dark" })).toHaveAttribute("aria-pressed", "false"); }); it("clicking Light applies light mode to ", async () => { const user = userEvent.setup(); renderSidebar(); await user.click(screen.getByRole("button", { name: "Light" })); expect(localStorage.getItem("theme")).toBe("light"); expect(document.documentElement.classList.contains("dark")).toBe(false); }); it("collapsed toggle flips to the opposite of the resolved mode", async () => { const user = userEvent.setup(); localStorage.setItem("theme", "light"); renderSidebar(true); await user.click(screen.getByRole("button", { name: "Switch to Dark" })); expect(localStorage.getItem("theme")).toBe("dark"); expect(screen.getByRole("button", { name: "Switch to Light" })).toBeInTheDocument(); }); });