-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy paththeme.test.tsx
More file actions
91 lines (80 loc) · 3.24 KB
/
theme.test.tsx
File metadata and controls
91 lines (80 loc) · 3.24 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { renderHook, act } from "@testing-library/react";
import type { ReactNode } from "react";
import { ThemeProvider, useTheme } from "./theme";
function wrapper({ children }: { children: ReactNode }) {
return <ThemeProvider>{children}</ThemeProvider>;
}
describe("ThemeProvider", () => {
beforeEach(() => {
localStorage.clear();
document.documentElement.removeAttribute("data-theme");
document.documentElement.classList.remove("dark");
});
it("defaults to dark when localStorage is empty", () => {
const { result } = renderHook(() => useTheme(), { wrapper });
expect(result.current.preference).toBe("dark");
expect(result.current.resolved).toBe("dark");
});
it("reads stored preference from localStorage", () => {
localStorage.setItem("memo-theme", "light");
const { result } = renderHook(() => useTheme(), { wrapper });
expect(result.current.preference).toBe("light");
expect(result.current.resolved).toBe("light");
});
it("ignores invalid stored values", () => {
localStorage.setItem("memo-theme", "invalid-value");
const { result } = renderHook(() => useTheme(), { wrapper });
expect(result.current.preference).toBe("dark");
});
it("persists preference changes to localStorage", () => {
const { result } = renderHook(() => useTheme(), { wrapper });
act(() => result.current.setPreference("light"));
expect(localStorage.getItem("memo-theme")).toBe("light");
expect(result.current.preference).toBe("light");
});
/**
* Regression test for Sentry MEMO-2H: SecurityError when localStorage is
* blocked (restricted browsers, privacy settings, embedded contexts).
* ThemeProvider must fall back to dark theme instead of crashing.
*/
describe("localStorage SecurityError handling", () => {
let getItemSpy: ReturnType<typeof vi.spyOn>;
let setItemSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
getItemSpy = vi.spyOn(Storage.prototype, "getItem").mockImplementation(
() => {
throw new DOMException(
"Failed to read the 'localStorage' property from 'Window': Access is denied for this document.",
"SecurityError",
);
},
);
setItemSpy = vi.spyOn(Storage.prototype, "setItem").mockImplementation(
() => {
throw new DOMException(
"Failed to set the 'localStorage' property on 'Window': Access is denied for this document.",
"SecurityError",
);
},
);
});
afterEach(() => {
getItemSpy.mockRestore();
setItemSpy.mockRestore();
});
it("falls back to dark theme when getItem throws SecurityError", () => {
const { result } = renderHook(() => useTheme(), { wrapper });
expect(result.current.preference).toBe("dark");
expect(result.current.resolved).toBe("dark");
});
it("does not crash when setItem throws SecurityError", () => {
const { result } = renderHook(() => useTheme(), { wrapper });
expect(() => {
act(() => result.current.setPreference("light"));
}).not.toThrow();
expect(result.current.preference).toBe("light");
expect(result.current.resolved).toBe("light");
});
});
});