|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import React, { act } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { getPreviewState, iframeRef } from "../nle/PreviewOverlaysTestMocks"; |
| 7 | +import { PreviewOverlays } from "../nle/PreviewOverlays"; |
| 8 | +import { ConnectedDomEditOverlay } from "./ConnectedDomEditOverlay"; |
| 9 | + |
| 10 | +// Overlay prop -> session handler, as PreviewOverlays wired DomEditOverlay itself. |
| 11 | +const WIRING = { |
| 12 | + onCanvasMouseDown: "handlePreviewCanvasMouseDown", |
| 13 | + onCanvasPointerMove: "handlePreviewCanvasPointerMove", |
| 14 | + onCanvasPointerLeave: "handlePreviewCanvasPointerLeave", |
| 15 | + onSelectionChange: "applyDomSelection", |
| 16 | + onBlockedMove: "handleBlockedDomMove", |
| 17 | + onManualDragStart: "handleDomManualDragStart", |
| 18 | + onPathOffsetCommit: "handleDomPathOffsetCommit", |
| 19 | + onGroupPathOffsetCommit: "handleDomGroupPathOffsetCommit", |
| 20 | + onBoxSizeCommit: "handleDomBoxSizeCommit", |
| 21 | + onRotationCommit: "handleDomRotationCommit", |
| 22 | + onStyleCommit: "handleDomStyleCommit", |
| 23 | + onDeleteSelection: "handleDomEditElementDelete", |
| 24 | + onMarqueeSelect: "applyMarqueeSelection", |
| 25 | +} as const; |
| 26 | + |
| 27 | +const ctx = vi.hoisted(() => ({ |
| 28 | + overlayProps: null as Record<string, unknown> | null, |
| 29 | + actions: {} as Record<string, unknown>, |
| 30 | + selection: { |
| 31 | + domEditHoverSelection: { label: "hover" }, |
| 32 | + domEditSelection: { label: "selected" }, |
| 33 | + domEditGroupSelections: [{ label: "member" }], |
| 34 | + }, |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock("../../contexts/DomEditContext", () => ({ |
| 38 | + useDomEditSelectionContext: () => ctx.selection, |
| 39 | + useDomEditActionsContext: () => ctx.actions, |
| 40 | +})); |
| 41 | +vi.mock("./DomEditOverlay", () => ({ |
| 42 | + DomEditOverlay: (props: Record<string, unknown>) => { |
| 43 | + ctx.overlayProps = props; |
| 44 | + return null; |
| 45 | + }, |
| 46 | +})); |
| 47 | +vi.mock("./TopologyLens", () => ({ TopologyLens: () => null })); |
| 48 | +vi.mock("./MotionPathOverlay", () => ({ MotionPathOverlay: () => null })); |
| 49 | +vi.mock("./SnapToolbar", () => ({ SnapToolbar: () => null })); |
| 50 | +vi.mock("./GridOverlay", () => ({ GridOverlay: () => null })); |
| 51 | + |
| 52 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 53 | + |
| 54 | +let root: Root; |
| 55 | + |
| 56 | +function mount(node: React.ReactNode): Record<string, unknown> { |
| 57 | + ctx.actions = { |
| 58 | + previewIframeRef: iframeRef, |
| 59 | + handleDomZIndexReorderCommit: vi.fn(() => Promise.resolve(undefined)), |
| 60 | + }; |
| 61 | + for (const name of Object.values(WIRING)) ctx.actions[name] = vi.fn(); |
| 62 | + const host = document.createElement("div"); |
| 63 | + document.body.append(host); |
| 64 | + root = createRoot(host); |
| 65 | + act(() => root.render(node)); |
| 66 | + return ctx.overlayProps!; |
| 67 | +} |
| 68 | + |
| 69 | +afterEach(() => { |
| 70 | + act(() => root.unmount()); |
| 71 | + ctx.overlayProps = null; |
| 72 | + getPreviewState().isPlaying = false; |
| 73 | + document.body.replaceChildren(); |
| 74 | +}); |
| 75 | + |
| 76 | +describe("ConnectedDomEditOverlay", () => { |
| 77 | + it("is what PreviewOverlays renders, with every handler from the session", () => { |
| 78 | + const props = mount( |
| 79 | + <PreviewOverlays |
| 80 | + shouldShowMotionPath={false} |
| 81 | + shouldShowSelectedDomBounds={true} |
| 82 | + recordingState={"idle" as never} |
| 83 | + onToggleRecording={vi.fn()} |
| 84 | + />, |
| 85 | + ); |
| 86 | + // The overlay never read these, so the connected wrapper does not pass them. |
| 87 | + expect(props).not.toHaveProperty("recordingState"); |
| 88 | + expect(props).not.toHaveProperty("onToggleRecording"); |
| 89 | + for (const [prop, handler] of Object.entries(WIRING)) { |
| 90 | + expect(props[prop], prop).toBe(ctx.actions[handler]); |
| 91 | + } |
| 92 | + expect(props).toMatchObject({ |
| 93 | + iframeRef, |
| 94 | + activeCompositionPath: "index.html", |
| 95 | + hoverSelection: ctx.selection.domEditHoverSelection, |
| 96 | + selection: ctx.selection.domEditSelection, |
| 97 | + groupSelections: ctx.selection.domEditGroupSelections, |
| 98 | + allowCanvasMovement: true, |
| 99 | + canvasInput: undefined, |
| 100 | + onTextEditingChange: undefined, |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it("shows no hover box while playback runs", () => { |
| 105 | + getPreviewState().isPlaying = true; |
| 106 | + const props = mount( |
| 107 | + <PreviewOverlays shouldShowMotionPath={false} shouldShowSelectedDomBounds={true} />, |
| 108 | + ); |
| 109 | + expect(props.hoverSelection).toBeNull(); |
| 110 | + expect(props.selection).toBe(ctx.selection.domEditSelection); |
| 111 | + }); |
| 112 | + |
| 113 | + it("hides the selection and stops movement while a gesture records", () => { |
| 114 | + const props = mount( |
| 115 | + <PreviewOverlays |
| 116 | + shouldShowMotionPath={false} |
| 117 | + shouldShowSelectedDomBounds={false} |
| 118 | + isGestureRecording={true} |
| 119 | + />, |
| 120 | + ); |
| 121 | + expect(props).toMatchObject({ |
| 122 | + selection: null, |
| 123 | + groupSelections: [], |
| 124 | + allowCanvasMovement: false, |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("persists a z-order change through the session's reorder commit", () => { |
| 129 | + const props = mount( |
| 130 | + <PreviewOverlays shouldShowMotionPath={false} shouldShowSelectedDomBounds={true} />, |
| 131 | + ); |
| 132 | + const element = document.createElement("div"); |
| 133 | + element.id = "card"; |
| 134 | + const sel = { element, id: "card", selector: "#card", sourceFile: "index.html" }; |
| 135 | + const apply = props.onApplyZIndex as (...args: unknown[]) => void; |
| 136 | + act(() => apply(sel, [{ element, zIndex: 4 }], "bring-forward", null)); |
| 137 | + expect(ctx.actions.handleDomZIndexReorderCommit).toHaveBeenCalledWith( |
| 138 | + [expect.objectContaining({ element, zIndex: 4, id: "card", sourceFile: "index.html" })], |
| 139 | + expect.stringContaining("z-reorder:bring-forward"), |
| 140 | + "bring-forward", |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + it("passes a host's canvas input, text-editing listener and hover gate to the overlay", () => { |
| 145 | + const onTextEditingChange = vi.fn(); |
| 146 | + const props = mount( |
| 147 | + <ConnectedDomEditOverlay |
| 148 | + activeCompositionPath={null} |
| 149 | + showHoverSelection={false} |
| 150 | + shouldShowSelectedDomBounds={true} |
| 151 | + canvasInput="host" |
| 152 | + onTextEditingChange={onTextEditingChange} |
| 153 | + />, |
| 154 | + ); |
| 155 | + expect(props).toMatchObject({ |
| 156 | + canvasInput: "host", |
| 157 | + onTextEditingChange, |
| 158 | + hoverSelection: null, |
| 159 | + selection: ctx.selection.domEditSelection, |
| 160 | + iframeRef, |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments