|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { renderHook, act } from "@testing-library/react"; |
| 3 | +import type { ReactNode } from "react"; |
| 4 | +import { ThemeProvider, useTheme } from "./theme"; |
| 5 | + |
| 6 | +function wrapper({ children }: { children: ReactNode }) { |
| 7 | + return <ThemeProvider>{children}</ThemeProvider>; |
| 8 | +} |
| 9 | + |
| 10 | +describe("ThemeProvider", () => { |
| 11 | + beforeEach(() => { |
| 12 | + localStorage.clear(); |
| 13 | + document.documentElement.removeAttribute("data-theme"); |
| 14 | + document.documentElement.classList.remove("dark"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("defaults to dark when localStorage is empty", () => { |
| 18 | + const { result } = renderHook(() => useTheme(), { wrapper }); |
| 19 | + expect(result.current.preference).toBe("dark"); |
| 20 | + expect(result.current.resolved).toBe("dark"); |
| 21 | + }); |
| 22 | + |
| 23 | + it("reads stored preference from localStorage", () => { |
| 24 | + localStorage.setItem("memo-theme", "light"); |
| 25 | + const { result } = renderHook(() => useTheme(), { wrapper }); |
| 26 | + expect(result.current.preference).toBe("light"); |
| 27 | + expect(result.current.resolved).toBe("light"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("ignores invalid stored values", () => { |
| 31 | + localStorage.setItem("memo-theme", "invalid-value"); |
| 32 | + const { result } = renderHook(() => useTheme(), { wrapper }); |
| 33 | + expect(result.current.preference).toBe("dark"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("persists preference changes to localStorage", () => { |
| 37 | + const { result } = renderHook(() => useTheme(), { wrapper }); |
| 38 | + act(() => result.current.setPreference("light")); |
| 39 | + expect(localStorage.getItem("memo-theme")).toBe("light"); |
| 40 | + expect(result.current.preference).toBe("light"); |
| 41 | + }); |
| 42 | + |
| 43 | + /** |
| 44 | + * Regression test for Sentry MEMO-2H: SecurityError when localStorage is |
| 45 | + * blocked (restricted browsers, privacy settings, embedded contexts). |
| 46 | + * ThemeProvider must fall back to dark theme instead of crashing. |
| 47 | + */ |
| 48 | + describe("localStorage SecurityError handling", () => { |
| 49 | + let getItemSpy: ReturnType<typeof vi.spyOn>; |
| 50 | + let setItemSpy: ReturnType<typeof vi.spyOn>; |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + getItemSpy = vi.spyOn(Storage.prototype, "getItem").mockImplementation( |
| 54 | + () => { |
| 55 | + throw new DOMException( |
| 56 | + "Failed to read the 'localStorage' property from 'Window': Access is denied for this document.", |
| 57 | + "SecurityError", |
| 58 | + ); |
| 59 | + }, |
| 60 | + ); |
| 61 | + setItemSpy = vi.spyOn(Storage.prototype, "setItem").mockImplementation( |
| 62 | + () => { |
| 63 | + throw new DOMException( |
| 64 | + "Failed to set the 'localStorage' property on 'Window': Access is denied for this document.", |
| 65 | + "SecurityError", |
| 66 | + ); |
| 67 | + }, |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + getItemSpy.mockRestore(); |
| 73 | + setItemSpy.mockRestore(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("falls back to dark theme when getItem throws SecurityError", () => { |
| 77 | + const { result } = renderHook(() => useTheme(), { wrapper }); |
| 78 | + expect(result.current.preference).toBe("dark"); |
| 79 | + expect(result.current.resolved).toBe("dark"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("does not crash when setItem throws SecurityError", () => { |
| 83 | + const { result } = renderHook(() => useTheme(), { wrapper }); |
| 84 | + expect(() => { |
| 85 | + act(() => result.current.setPreference("light")); |
| 86 | + }).not.toThrow(); |
| 87 | + expect(result.current.preference).toBe("light"); |
| 88 | + expect(result.current.resolved).toBe("light"); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments