Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { ThemeProvider } from "next-themes";
import type { ReactNode } from "react";
import { ThemeToggle } from "../../src/shared/theme-toggle.js";
// Mirrors the consumer mount in packages/web (defaultTheme="light",
// enableSystem={false}, two explicit themes). The migration effect in
// ThemeToggle keys off the next-themes storage, so the tests exercise the
// real provider against jsdom localStorage.
function Harness({ children }: { children: ReactNode }) {
return (
<ThemeProvider
attribute="class"
defaultTheme="light"
enableSystem={false}
themes={["light", "dark"]}
>
{children}
</ThemeProvider>
);
}
const STORAGE_KEY = "theme"; // next-themes default key
describe("ThemeToggle theme default + persistence", () => {
beforeEach(() => {
localStorage.clear();
document.documentElement.className = "";
// next-themes reads matchMedia even with enableSystem=false.
vi.stubGlobal(
"matchMedia",
vi.fn().mockReturnValue({
matches: false,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
}),
);
});
afterEach(() => {
vi.unstubAllGlobals();
});
it("defaults a fresh visitor (empty storage) to light", async () => {
render(
<Harness>
<ThemeToggle />
</Harness>,
);
await waitFor(() => {
expect(screen.getByRole("button", { name: "Switch to dark theme" })).toBeInTheDocument();
});
// Migration must not write anything for a fresh visitor.
expect(localStorage.getItem(STORAGE_KEY)).not.toBe("dark");
});
it("keeps an explicit stored dark choice (never clobbered by the default)", async () => {
localStorage.setItem(STORAGE_KEY, "dark");
render(
<Harness>
<ThemeToggle />
</Harness>,
);
await waitFor(() => {
expect(screen.getByRole("button", { name: "Switch to light theme" })).toBeInTheDocument();
});
expect(localStorage.getItem(STORAGE_KEY)).toBe("dark");
});
it("keeps an explicit stored light choice", async () => {
localStorage.setItem(STORAGE_KEY, "light");
render(
<Harness>
<ThemeToggle />
</Harness>,
);
await waitFor(() => {
expect(screen.getByRole("button", { name: "Switch to dark theme" })).toBeInTheDocument();
});
expect(localStorage.getItem(STORAGE_KEY)).toBe("light");
});
it("migrates a stale persisted 'system' value to the light default", async () => {
localStorage.setItem(STORAGE_KEY, "system");
render(
<Harness>
<ThemeToggle />
</Harness>,
);
await waitFor(() => {
expect(localStorage.getItem(STORAGE_KEY)).toBe("light");
});
expect(
screen.getByRole("button", { name: "Switch to dark theme" }),
).toBeInTheDocument();
});
it("names the theme in effect beside the icon only when asked", async () => {
const { rerender } = render(
<Harness>
<ThemeToggle />
</Harness>,
);
const button = await screen.findByRole("button", { name: "Switch to dark theme" });
expect(button).not.toHaveTextContent("Light");
rerender(
<Harness>
<ThemeToggle showLabel />
</Harness>,
);
const labelled = screen.getByRole("button", { name: "Light theme. Switch to dark theme" });
expect(labelled).toHaveTextContent("Light");
});
it("switches to the opposite theme on click", async () => {
render(
<Harness>
<ThemeToggle />
</Harness>,
);
fireEvent.click(await screen.findByRole("button", { name: "Switch to dark theme" }));
await waitFor(() => expect(localStorage.getItem(STORAGE_KEY)).toBe("dark"));
fireEvent.click(await screen.findByRole("button", { name: "Switch to light theme" }));
await waitFor(() => expect(localStorage.getItem(STORAGE_KEY)).toBe("light"));
});
});