|
| 1 | +import { GlobalWindow } from "happy-dom"; |
| 2 | + |
| 3 | +// Setup basic DOM environment for testing-library |
| 4 | +const dom = new GlobalWindow(); |
| 5 | +/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */ |
| 6 | +(global as any).window = dom.window; |
| 7 | +(global as any).document = dom.window.document; |
| 8 | +// Polyfill console since happy-dom might interfere or we just want standard console |
| 9 | +(global as any).console = console; |
| 10 | +/* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */ |
| 11 | + |
| 12 | +import { afterEach, describe, expect, mock, test, beforeEach } from "bun:test"; |
| 13 | + |
| 14 | +import { render, cleanup } from "@testing-library/react"; |
| 15 | +import React from "react"; |
| 16 | +import { ThemeProvider, useTheme } from "./ThemeContext"; |
| 17 | +import { UI_THEME_KEY } from "@/common/constants/storage"; |
| 18 | + |
| 19 | +// Helper to access internals |
| 20 | +const TestComponent = () => { |
| 21 | + const { theme, toggleTheme } = useTheme(); |
| 22 | + return ( |
| 23 | + <div> |
| 24 | + <span data-testid="theme-value">{theme}</span> |
| 25 | + <button onClick={toggleTheme} data-testid="toggle-btn">Toggle</button> |
| 26 | + </div> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +describe("ThemeContext", () => { |
| 31 | + // Mock matchMedia |
| 32 | + const mockMatchMedia = mock(() => ({ |
| 33 | + matches: false, |
| 34 | + media: "", |
| 35 | + onchange: null, |
| 36 | + addListener: () => { |
| 37 | + // no-op |
| 38 | + }, |
| 39 | + removeListener: () => { |
| 40 | + // no-op |
| 41 | + }, |
| 42 | + addEventListener: () => { |
| 43 | + // no-op |
| 44 | + }, |
| 45 | + removeEventListener: () => { |
| 46 | + // no-op |
| 47 | + }, |
| 48 | + dispatchEvent: () => true, |
| 49 | + })); |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + // Ensure window exists (Bun test with happy-dom should provide it) |
| 53 | + if (typeof window !== "undefined") { |
| 54 | + window.matchMedia = mockMatchMedia; |
| 55 | + window.localStorage.clear(); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + afterEach(() => { |
| 60 | + cleanup(); |
| 61 | + if (typeof window !== "undefined") { |
| 62 | + window.localStorage.clear(); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + test("uses persisted state by default", () => { |
| 67 | + const { getByTestId } = render( |
| 68 | + <ThemeProvider> |
| 69 | + <TestComponent /> |
| 70 | + </ThemeProvider> |
| 71 | + ); |
| 72 | + // If matchMedia matches is false (default mock), resolveSystemTheme returns 'dark' (since it checks prefers-color-scheme: light) |
| 73 | + // resolveSystemTheme logic: window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark" |
| 74 | + expect(getByTestId("theme-value").textContent).toBe("dark"); |
| 75 | + }); |
| 76 | + |
| 77 | + test("respects forcedTheme prop", () => { |
| 78 | + const { getByTestId, rerender } = render( |
| 79 | + <ThemeProvider forcedTheme="light"> |
| 80 | + <TestComponent /> |
| 81 | + </ThemeProvider> |
| 82 | + ); |
| 83 | + expect(getByTestId("theme-value").textContent).toBe("light"); |
| 84 | + |
| 85 | + rerender( |
| 86 | + <ThemeProvider forcedTheme="dark"> |
| 87 | + <TestComponent /> |
| 88 | + </ThemeProvider> |
| 89 | + ); |
| 90 | + expect(getByTestId("theme-value").textContent).toBe("dark"); |
| 91 | + }); |
| 92 | + |
| 93 | + test("forcedTheme overrides persisted state", () => { |
| 94 | + window.localStorage.setItem(UI_THEME_KEY, JSON.stringify("light")); |
| 95 | + |
| 96 | + const { getByTestId } = render( |
| 97 | + <ThemeProvider forcedTheme="dark"> |
| 98 | + <TestComponent /> |
| 99 | + </ThemeProvider> |
| 100 | + ); |
| 101 | + expect(getByTestId("theme-value").textContent).toBe("dark"); |
| 102 | + |
| 103 | + // Check that localStorage is still light (since forcedTheme doesn't write to storage by itself) |
| 104 | + expect(JSON.parse(window.localStorage.getItem(UI_THEME_KEY)!)).toBe("light"); |
| 105 | + }); |
| 106 | +}); |
0 commit comments