|
| 1 | +import { isValidElement, type ReactElement, type ReactNode } from "react"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 3 | + |
| 4 | +import { reactHookHarness as hooks } from "../../test/reactHookHarness"; |
| 5 | +import type { ThemeColorRole } from "../../themePalette"; |
| 6 | + |
| 7 | +vi.mock("react", async (importOriginal) => { |
| 8 | + const actual = await importOriginal<typeof import("react")>(); |
| 9 | + const { reactHookHarness } = await import("../../test/reactHookHarness"); |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + useCallback: reactHookHarness.useCallback, |
| 13 | + useMemo: reactHookHarness.useMemo, |
| 14 | + useRef: reactHookHarness.useRef, |
| 15 | + useState: reactHookHarness.useState, |
| 16 | + useEffect: () => {}, |
| 17 | + }; |
| 18 | +}); |
| 19 | +vi.mock("react/compiler-runtime", async () => { |
| 20 | + const { reactHookHarness } = await import("../../test/reactHookHarness"); |
| 21 | + return { c: reactHookHarness.useMemoCache }; |
| 22 | +}); |
| 23 | + |
| 24 | +import { ThemeEditorPanel } from "./ThemeEditorPanel"; |
| 25 | +import { ThemeColorField } from "./ThemeColorPicker"; |
| 26 | + |
| 27 | +type FieldProps = { |
| 28 | + role: ThemeColorRole; |
| 29 | + selected: boolean; |
| 30 | + onSelect: (role: ThemeColorRole, reveal?: boolean) => void; |
| 31 | +}; |
| 32 | + |
| 33 | +function findFields(node: ReactNode): ReactElement<FieldProps>[] { |
| 34 | + if (Array.isArray(node)) return node.flatMap(findFields); |
| 35 | + if (!isValidElement<{ children?: ReactNode }>(node)) return []; |
| 36 | + if (node.type === ThemeColorField) return [node as ReactElement<FieldProps>]; |
| 37 | + return findFields(node.props.children); |
| 38 | +} |
| 39 | + |
| 40 | +function renderPanel() { |
| 41 | + hooks.beginRender(); |
| 42 | + return ThemeEditorPanel({ |
| 43 | + open: true, |
| 44 | + onOpenChange: vi.fn(), |
| 45 | + onSaved: () => true, |
| 46 | + editingTheme: null, |
| 47 | + initialAppearance: "dark", |
| 48 | + restoreTheme: vi.fn(), |
| 49 | + }) as ReactElement<{ ref: { current: unknown }; children: ReactNode }>; |
| 50 | +} |
| 51 | + |
| 52 | +describe("theme role reveal", () => { |
| 53 | + beforeEach(() => { |
| 54 | + hooks.reset(); |
| 55 | + vi.stubGlobal("window", { localStorage: { getItem: () => null } }); |
| 56 | + }); |
| 57 | + afterEach(() => vi.unstubAllGlobals()); |
| 58 | + |
| 59 | + it("keeps role selection and reveal targets when motion changes or matchMedia is missing", () => { |
| 60 | + let reducedMotion = false; |
| 61 | + window.matchMedia = vi.fn(() => ({ matches: reducedMotion }) as MediaQueryList); |
| 62 | + const frames: FrameRequestCallback[] = []; |
| 63 | + vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => |
| 64 | + frames.push(callback), |
| 65 | + ); |
| 66 | + const scrollIntoView = vi.fn(); |
| 67 | + const querySelector = vi.fn(() => ({ scrollIntoView })); |
| 68 | + const panel = renderPanel(); |
| 69 | + panel.props.ref.current = { querySelector }; |
| 70 | + const select = findFields(panel)[0]!.props.onSelect; |
| 71 | + |
| 72 | + select("toolbar", true); |
| 73 | + frames.shift()!(0); |
| 74 | + expect(querySelector).toHaveBeenLastCalledWith('[data-theme-color-role="canvas"]'); |
| 75 | + expect(scrollIntoView).toHaveBeenLastCalledWith({ behavior: "smooth", block: "nearest" }); |
| 76 | + expect(findFields(renderPanel()).find((field) => field.props.selected)?.props.role).toBe( |
| 77 | + "canvas", |
| 78 | + ); |
| 79 | + |
| 80 | + select("surface", true); |
| 81 | + // Read at reveal time, including a preference change after the input event. |
| 82 | + reducedMotion = true; |
| 83 | + frames.shift()!(0); |
| 84 | + expect(querySelector).toHaveBeenLastCalledWith('[data-theme-color-role="surface"]'); |
| 85 | + expect(scrollIntoView).toHaveBeenLastCalledWith({ behavior: "auto", block: "nearest" }); |
| 86 | + expect(findFields(renderPanel()).find((field) => field.props.selected)?.props.role).toBe( |
| 87 | + "surface", |
| 88 | + ); |
| 89 | + |
| 90 | + vi.stubGlobal("window", { localStorage: { getItem: () => null } }); |
| 91 | + select("surface", true); |
| 92 | + frames.shift()!(0); |
| 93 | + expect(querySelector).toHaveBeenLastCalledWith('[data-theme-color-role="surface"]'); |
| 94 | + expect(scrollIntoView).toHaveBeenLastCalledWith({ behavior: "smooth", block: "nearest" }); |
| 95 | + expect(findFields(renderPanel()).find((field) => field.props.selected)?.props.role).toBe( |
| 96 | + "surface", |
| 97 | + ); |
| 98 | + }); |
| 99 | +}); |
0 commit comments