|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import React, { act } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { Dock } from "./Dock"; |
| 7 | +import { parseDockLayout } from "./dockLayoutSchema"; |
| 8 | +import { useDockLayoutStore } from "./dockLayoutStore"; |
| 9 | +import { PANEL_IDS } from "./panelRegistry"; |
| 10 | +import { readStudioUiPreferences } from "../../utils/studioUiPreferences"; |
| 11 | + |
| 12 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 13 | + |
| 14 | +class ResizeObserverStub { |
| 15 | + observe() {} |
| 16 | + unobserve() {} |
| 17 | + disconnect() {} |
| 18 | +} |
| 19 | +(globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = ResizeObserverStub; |
| 20 | + |
| 21 | +let root: Root | null = null; |
| 22 | + |
| 23 | +function mount(projectId: string | null) { |
| 24 | + const host = document.createElement("div"); |
| 25 | + document.body.append(host); |
| 26 | + root = createRoot(host); |
| 27 | + act(() => { |
| 28 | + root?.render( |
| 29 | + <Dock.Root projectId={projectId}> |
| 30 | + {PANEL_IDS.map((id) => ( |
| 31 | + <Dock.Panel key={id} id={id}> |
| 32 | + <div data-testid={`content-${id}`}>{id}</div> |
| 33 | + </Dock.Panel> |
| 34 | + ))} |
| 35 | + </Dock.Root>, |
| 36 | + ); |
| 37 | + }); |
| 38 | + return host; |
| 39 | +} |
| 40 | + |
| 41 | +beforeEach(() => { |
| 42 | + localStorage.clear(); |
| 43 | + vi.useFakeTimers(); |
| 44 | +}); |
| 45 | + |
| 46 | +afterEach(() => { |
| 47 | + act(() => root?.unmount()); |
| 48 | + root = null; |
| 49 | + document.body.innerHTML = ""; |
| 50 | + vi.useRealTimers(); |
| 51 | +}); |
| 52 | + |
| 53 | +// The default Edit layout tabs [compositions|assets|code|catalog] into one |
| 54 | +// group and [design|layers|renders|variables] into another; dockview shows |
| 55 | +// only the active tab's content per group. `slideshow` is never part of the |
| 56 | +// default build — StudioRightPanels opens it itself when the file is one. |
| 57 | +const DEFAULT_OPEN = PANEL_IDS.filter((id) => id !== "slideshow"); |
| 58 | +const DEFAULT_VISIBLE = ["preview", "timeline", "compositions", "design"]; |
| 59 | + |
| 60 | +describe("Dock on React 19", () => { |
| 61 | + it("mounts the default layout's ten panels, showing only each group's active tab", () => { |
| 62 | + const host = mount("p1"); |
| 63 | + for (const id of DEFAULT_VISIBLE) { |
| 64 | + expect(host.querySelector(`[data-testid="content-${id}"]`)).not.toBeNull(); |
| 65 | + } |
| 66 | + for (const id of DEFAULT_OPEN.filter((id) => !DEFAULT_VISIBLE.includes(id))) { |
| 67 | + expect(host.querySelector(`[data-testid="content-${id}"]`)).toBeNull(); |
| 68 | + } |
| 69 | + expect(host.querySelector('[data-testid="content-slideshow"]')).toBeNull(); |
| 70 | + expect(useDockLayoutStore.getState().openPanels).toEqual(new Set(DEFAULT_OPEN)); |
| 71 | + }); |
| 72 | + |
| 73 | + it("persists a layout change per project and reads it back through the schema", () => { |
| 74 | + mount("p1"); |
| 75 | + act(() => useDockLayoutStore.getState().closePanel("renders")); |
| 76 | + act(() => { |
| 77 | + vi.advanceTimersByTime(400); |
| 78 | + }); |
| 79 | + const stored = readStudioUiPreferences(undefined, "p1").dockLayout; |
| 80 | + const expected = DEFAULT_OPEN.filter((id) => id !== "renders"); |
| 81 | + expect(Object.keys(stored?.panels ?? {}).sort()).toEqual([...expected].sort()); |
| 82 | + expect(readStudioUiPreferences(undefined, "p2").dockLayout).toBeUndefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("closes a panel and reopens it from the store, becoming its group's visible tab", async () => { |
| 86 | + const host = mount("p1"); |
| 87 | + act(() => useDockLayoutStore.getState().closePanel("renders")); |
| 88 | + expect(useDockLayoutStore.getState().openPanels.has("renders")).toBe(false); |
| 89 | + expect(host.querySelector('[data-testid="content-renders"]')).toBeNull(); |
| 90 | + // dockview's own panel-active event dispatch resolves on a microtask, one |
| 91 | + // tick after the synchronous store call returns; `act(async ...)` is what |
| 92 | + // actually waits for it instead of asserting against pre-flush DOM. |
| 93 | + await act(async () => { |
| 94 | + useDockLayoutStore.getState().togglePanel("renders"); |
| 95 | + await Promise.resolve(); |
| 96 | + }); |
| 97 | + expect(useDockLayoutStore.getState().openPanels.has("renders")).toBe(true); |
| 98 | + expect(host.querySelector('[data-testid="content-renders"]')).not.toBeNull(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("reopens a closed panel as a tab of its zone's group, not a new group", () => { |
| 102 | + mount("p1"); |
| 103 | + act(() => useDockLayoutStore.getState().closePanel("compositions")); |
| 104 | + act(() => useDockLayoutStore.getState().togglePanel("compositions")); |
| 105 | + act(() => { |
| 106 | + vi.advanceTimersByTime(1000); |
| 107 | + }); |
| 108 | + const grid = JSON.stringify(readStudioUiPreferences(undefined, "p1").dockLayout?.grid); |
| 109 | + const groups = [...grid.matchAll(/"views":\[([^\]]*)\]/g)].map((m) => m[1]); |
| 110 | + const home = groups.find((views) => views.includes('"compositions"')); |
| 111 | + expect(home).toContain('"assets"'); |
| 112 | + }); |
| 113 | + |
| 114 | + it("reopens a side panel next to the preview when its whole column was closed", () => { |
| 115 | + mount("p1"); |
| 116 | + const { closePanel, togglePanel } = useDockLayoutStore.getState(); |
| 117 | + for (const id of ["compositions", "assets", "code", "catalog"] as const) |
| 118 | + act(() => closePanel(id)); |
| 119 | + act(() => togglePanel("compositions")); |
| 120 | + expect(useDockLayoutStore.getState().openPanels.has("compositions")).toBe(true); |
| 121 | + expect(useDockLayoutStore.getState().visiblePanels.has("compositions")).toBe(true); |
| 122 | + }); |
| 123 | + |
| 124 | + it("reopens the timeline as its own group, never as a tab of the preview", () => { |
| 125 | + mount("p1"); |
| 126 | + act(() => useDockLayoutStore.getState().closePanel("timeline")); |
| 127 | + act(() => useDockLayoutStore.getState().togglePanel("timeline")); |
| 128 | + act(() => { |
| 129 | + vi.advanceTimersByTime(1000); |
| 130 | + }); |
| 131 | + const grid = JSON.stringify(readStudioUiPreferences(undefined, "p1").dockLayout?.grid); |
| 132 | + const groups = [...grid.matchAll(/"views":\[([^\]]*)\]/g)].map((m) => m[1]); |
| 133 | + expect(groups.find((views) => views.includes('"timeline"'))).not.toContain('"preview"'); |
| 134 | + }); |
| 135 | + |
| 136 | + it("restores the stored layout on the next mount instead of rebuilding the default", () => { |
| 137 | + mount("p1"); |
| 138 | + act(() => useDockLayoutStore.getState().closePanel("renders")); |
| 139 | + act(() => { |
| 140 | + vi.advanceTimersByTime(1000); |
| 141 | + }); |
| 142 | + act(() => root?.unmount()); |
| 143 | + root = null; |
| 144 | + document.body.innerHTML = ""; |
| 145 | + |
| 146 | + mount("p1"); |
| 147 | + expect(useDockLayoutStore.getState().openPanels.has("renders")).toBe(false); |
| 148 | + }); |
| 149 | + |
| 150 | + it("falls back to the default layout when the stored one names an unknown panel", () => { |
| 151 | + localStorage.setItem( |
| 152 | + "hf-studio-ui-preferences:p1", |
| 153 | + JSON.stringify({ dockLayout: { grid: {}, panels: { nope: {} } } }), |
| 154 | + ); |
| 155 | + const host = mount("p1"); |
| 156 | + expect(host.querySelector('[data-testid="content-preview"]')).not.toBeNull(); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +describe("parseDockLayout", () => { |
| 161 | + it("rejects shapes that are not a dock layout", () => { |
| 162 | + expect(parseDockLayout(null)).toBeNull(); |
| 163 | + expect(parseDockLayout({ grid: {}, panels: {} })).toBeNull(); |
| 164 | + expect( |
| 165 | + parseDockLayout({ |
| 166 | + grid: { |
| 167 | + width: 1, |
| 168 | + height: 1, |
| 169 | + orientation: "HORIZONTAL", |
| 170 | + root: { type: "leaf", data: { views: ["ghost"] } }, |
| 171 | + }, |
| 172 | + panels: { ghost: { id: "ghost", contentComponent: "panel" } }, |
| 173 | + }), |
| 174 | + ).toBeNull(); |
| 175 | + }); |
| 176 | + |
| 177 | + const placed = (views: string[], panelIds: string[]) => ({ |
| 178 | + grid: { |
| 179 | + width: 1, |
| 180 | + height: 1, |
| 181 | + orientation: "HORIZONTAL", |
| 182 | + root: { type: "leaf", data: { views } }, |
| 183 | + }, |
| 184 | + panels: Object.fromEntries(panelIds.map((id) => [id, { id, contentComponent: "panel" }])), |
| 185 | + }); |
| 186 | + |
| 187 | + it("rejects a view that has no panel entry and a panel that no view places", () => { |
| 188 | + expect(parseDockLayout(placed(["preview", "design"], ["preview"]))).toBeNull(); |
| 189 | + expect(parseDockLayout(placed(["preview"], ["preview", "design"]))).toBeNull(); |
| 190 | + expect(parseDockLayout(placed(["preview", "design"], ["preview", "design"]))).not.toBeNull(); |
| 191 | + }); |
| 192 | +}); |
0 commit comments