Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tests/unit/themeToggle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,35 @@ const buildMatchMedia = (matches: boolean) =>
dispatchEvent: vi.fn(),
}));

const createLocalStorageMock = () => {
let store: Record<string, string> = {};

return {
getItem: (key: string) => (key in store ? store[key] : null),
setItem: (key: string, value: string) => {
store[key] = String(value);
},
removeItem: (key: string) => {
delete store[key];
},
clear: () => {
store = {};
},
key: (index: number) => Object.keys(store)[index] ?? null,
get length() {
return Object.keys(store).length;
},
};
};

describe("ThemeToggle", () => {
beforeEach(() => {
// Some JS DOM shims expose a partial localStorage; tests need a full Storage-like API.
Object.defineProperty(window, "localStorage", {
value: createLocalStorageMock(),
configurable: true,
});

document.documentElement.classList.remove("dark");
window.localStorage.clear();
vi.stubGlobal("matchMedia", buildMatchMedia(false));
Expand Down