36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
readThemePreference,
|
|
resolveTheme,
|
|
saveThemePreference,
|
|
THEME_STORAGE_KEY,
|
|
toggleTheme,
|
|
} from "@/lib/theme-preference.js";
|
|
|
|
function makeStorage(initial = {}) {
|
|
const values = new Map(Object.entries(initial));
|
|
return {
|
|
getItem: (key) => values.get(key) ?? null,
|
|
setItem: (key, value) => values.set(key, value),
|
|
};
|
|
}
|
|
|
|
describe("theme preference", () => {
|
|
it("restores a saved dark preference ahead of system preference", () => {
|
|
const storage = makeStorage({ [THEME_STORAGE_KEY]: "dark" });
|
|
expect(resolveTheme({ savedTheme: readThemePreference(storage), systemPrefersDark: false })).toBe("dark");
|
|
});
|
|
|
|
it("uses system dark preference when no explicit choice exists", () => {
|
|
expect(resolveTheme({ savedTheme: null, systemPrefersDark: true })).toBe("dark");
|
|
});
|
|
|
|
it("switches themes and persists the explicit choice", () => {
|
|
const storage = makeStorage();
|
|
const nextTheme = toggleTheme("light");
|
|
saveThemePreference(nextTheme, storage);
|
|
|
|
expect(nextTheme).toBe("dark");
|
|
expect(readThemePreference(storage)).toBe("dark");
|
|
});
|
|
}); |