1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import { beforeEach, describe, expect, it } from "vitest";
import {
DEFAULT_THEME,
THEME_KEY,
isTheme,
resetTheme,
setTheme,
toggleTheme,
useTheme,
} from "./theme";
describe("theme store", () => {
beforeEach(() => {
localStorage.clear();
resetTheme();
});
it("starts light and stamps the document", () => {
expect(useTheme.getState().theme).toBe(DEFAULT_THEME);
expect(document.documentElement.dataset.theme).toBe("light");
});
it("setTheme applies, stamps and remembers the theme", () => {
setTheme("dark");
expect(useTheme.getState().theme).toBe("dark");
expect(document.documentElement.dataset.theme).toBe("dark");
expect(localStorage.getItem(THEME_KEY)).toBe("dark");
});
it("toggleTheme alternates between light and dark", () => {
toggleTheme();
expect(useTheme.getState().theme).toBe("dark");
toggleTheme();
expect(useTheme.getState().theme).toBe("light");
expect(document.documentElement.dataset.theme).toBe("light");
});
it("resetTheme goes back to light", () => {
setTheme("dark");
resetTheme();
expect(useTheme.getState().theme).toBe("light");
expect(localStorage.getItem(THEME_KEY)).toBe("light");
});
});
describe("isTheme", () => {
it("accepts the two themes and rejects anything else", () => {
expect(isTheme("light")).toBe(true);
expect(isTheme("dark")).toBe(true);
expect(isTheme("system")).toBe(false);
expect(isTheme(null)).toBe(false);
expect(isTheme(42)).toBe(false);
});
});
|