|
| 1 | +// @vitest-environment happy-dom |
| 2 | +// Mounts the timeline by package name with no Studio shell, as a host app does. |
| 3 | +import { act } from "react"; |
| 4 | +import { expect, it, vi } from "vitest"; |
| 5 | +import { Timeline, usePlayerStore, type TimelineZIndexReorderCommit } from "@hyperframes/studio"; |
| 6 | +import { installReactActEnvironment, mountReactHarness } from "./hooks/domSelectionTestHarness"; |
| 7 | + |
| 8 | +installReactActEnvironment(); |
| 9 | +// happy-dom has no layout; the timeline only renders clips inside a sized viewport. |
| 10 | +for (const [prop, px] of [ |
| 11 | + ["clientWidth", 1000], |
| 12 | + ["clientHeight", 400], |
| 13 | + ["offsetWidth", 1000], |
| 14 | + ["offsetHeight", 400], |
| 15 | +] as const) { |
| 16 | + vi.spyOn(HTMLElement.prototype, prop, "get").mockReturnValue(px); |
| 17 | +} |
| 18 | + |
| 19 | +const settle = () => act(async () => new Promise((r) => setTimeout(r, 50))); |
| 20 | +const pointer = (type: string, clientY: number) => |
| 21 | + new PointerEvent(type, { bubbles: true, button: 0, pointerId: 1, clientX: 150, clientY }); |
| 22 | + |
| 23 | +it("restacks the host's preview when a lane move puts a clip below another", async () => { |
| 24 | + const iframe = document.createElement("iframe"); |
| 25 | + document.body.appendChild(iframe); |
| 26 | + const doc = iframe.contentDocument!; |
| 27 | + // No authored z: DOM order paints tag over title, which matches the rows until the move. |
| 28 | + doc.body.innerHTML = '<div id="title"></div><div id="tag"></div><div id="note"></div>'; |
| 29 | + const clip = (id: string, start: number, duration: number, track: number) => |
| 30 | + ({ id, key: id, domId: id, tag: "div", start, duration, track }) as const; |
| 31 | + usePlayerStore.setState({ |
| 32 | + duration: 10, |
| 33 | + timelineReady: true, |
| 34 | + elements: [clip("title", 0, 4, 1), clip("tag", 0, 4, 0), clip("note", 6, 2, 2)], |
| 35 | + }); |
| 36 | + const commit = vi.fn<TimelineZIndexReorderCommit>(async (entries) => { |
| 37 | + for (const e of entries) e.element.style.zIndex = String(e.zIndex); |
| 38 | + }); |
| 39 | + const root = mountReactHarness( |
| 40 | + <Timeline |
| 41 | + previewIframeRef={{ current: iframe }} |
| 42 | + onZIndexReorder={commit} |
| 43 | + onMoveElement={async () => {}} |
| 44 | + onMoveElements={async () => {}} |
| 45 | + />, |
| 46 | + ); |
| 47 | + await settle(); |
| 48 | + const tagClip = document.querySelector<HTMLElement>('.timeline-clip[aria-label^="tag,"]')!; |
| 49 | + vi.spyOn(tagClip, "getBoundingClientRect").mockReturnValue(new DOMRect(80, 77, 360, 42)); |
| 50 | + |
| 51 | + // Drag tag from the top row to the bottom row, clear of note in time. |
| 52 | + act(() => void tagClip.dispatchEvent(pointer("pointerdown", 98))); |
| 53 | + act(() => { |
| 54 | + window.dispatchEvent(pointer("pointermove", 140)); |
| 55 | + window.dispatchEvent(pointer("pointermove", 194)); |
| 56 | + }); |
| 57 | + await act(async () => void window.dispatchEvent(pointer("pointerup", 194))); |
| 58 | + await settle(); |
| 59 | + |
| 60 | + expect(usePlayerStore.getState().elements.find((e) => e.id === "tag")?.track).toBe(2); |
| 61 | + expect(commit).toHaveBeenCalledTimes(1); |
| 62 | + expect(commit.mock.calls[0]![0].every((e) => e.element.ownerDocument === doc)).toBe(true); |
| 63 | + const z = (id: string) => Number(doc.getElementById(id)!.style.zIndex || 0); |
| 64 | + expect(z("title")).toBeGreaterThan(z("tag")); |
| 65 | + await act(async () => root.unmount()); |
| 66 | + usePlayerStore.setState({ elements: [], timelineReady: false }); |
| 67 | +}); |
0 commit comments