-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathuseTheme.test.ts
More file actions
69 lines (61 loc) · 1.56 KB
/
useTheme.test.ts
File metadata and controls
69 lines (61 loc) · 1.56 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { afterEach, describe, expect, it, vi } from "vitest";
function installThemeDom(theme: string | null, matchMedia?: Window["matchMedia"]) {
const element = {
name: "",
setAttribute: vi.fn(),
};
const documentElement = {
classList: {
add: vi.fn(),
remove: vi.fn(),
toggle: vi.fn(),
},
offsetHeight: 0,
style: {},
};
const body = {
style: {},
};
vi.stubGlobal("localStorage", {
getItem: vi.fn(() => theme),
setItem: vi.fn(),
});
vi.stubGlobal("window", {
matchMedia,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
});
vi.stubGlobal("document", {
body,
createElement: vi.fn(() => element),
documentElement,
head: {
append: vi.fn(),
},
querySelector: vi.fn(() => null),
});
vi.stubGlobal(
"getComputedStyle",
vi.fn(() => ({ backgroundColor: "rgb(1, 2, 3)" })),
);
}
describe("useTheme module initialization", () => {
afterEach(() => {
vi.resetModules();
vi.unstubAllGlobals();
});
it("does not read matchMedia for explicit themes", async () => {
const matchMedia = vi.fn(() => ({
matches: true,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
})) as unknown as Window["matchMedia"];
installThemeDom("dark", matchMedia);
await import("./useTheme");
expect(matchMedia).not.toHaveBeenCalled();
});
it("does not require matchMedia when an explicit theme is stored", async () => {
installThemeDom("light");
await expect(import("./useTheme")).resolves.toBeDefined();
});
});