Skip to content

Write HTML. Render video. Built for agents.

test(studio): wait for the off-canvas indicator instead of a fixed number of frames (#4458)

Miguel Ángel2623b666c27c
diff --git a/packages/studio/src/components/editor/offCanvasIndicatorRefresh.test.tsx b/packages/studio/src/components/editor/offCanvasIndicatorRefresh.test.tsx
index 8e75289046..8ed4611c03 100644
--- a/packages/studio/src/components/editor/offCanvasIndicatorRefresh.test.tsx
+++ b/packages/studio/src/components/editor/offCanvasIndicatorRefresh.test.tsx
@@ -2,7 +2,7 @@
import React, { act } from "react";
import { createRoot, type Root } from "react-dom/client";
-import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { DomEditOverlay } from "./DomEditOverlay";
import { RECOMPUTE_INTERVAL_MS } from "./offCanvasIndicatorRefresh";
@@ -59,6 +59,30 @@ async function flushAnimationFrames(): Promise<void> {
});
}
+// The refresh waits on MutationObserver delivery and then on its own throttle, so no fixed
+// number of frames is guaranteed to be enough on a loaded runner. Flush until the indicator
+// reaches the state under test.
+async function untilIndicator(host: HTMLElement, check: (indicator: Element | null) => void) {
+ await vi.waitFor(
+ async () => {
+ await act(async () => {
+ await flushAnimationFrames();
+ });
+ check(host.querySelector(INDICATOR));
+ },
+ { timeout: 4000, interval: 0 },
+ );
+}
+
+// Delivers each batch of mutation records `ms` late, as a busy runner can.
+function lateObserver(Base: typeof MutationObserver, ms: number): typeof MutationObserver {
+ return class extends Base {
+ constructor(callback: MutationCallback) {
+ super((records, observer) => setTimeout(() => callback(records, observer), ms));
+ }
+ };
+}
+
interface OverlayHarness {
host: HTMLElement;
movedElement: HTMLElement;
@@ -68,7 +92,7 @@ interface OverlayHarness {
// Mount DomEditOverlay over an iframe whose #headline sits at `initialLeft`, with a
// getBoundingClientRect stub that reads the element's live inline geometry (so a
// style mutation moves it) and reports the composition/overlay as 800x450 at origin.
-function mountOverlayWithHeadline(initialLeft: number): OverlayHarness {
+function mountOverlayWithHeadline(initialLeft: number, observerDelayMs = 0): OverlayHarness {
const originalGetBoundingClientRect = Element.prototype.getBoundingClientRect;
const host = document.createElement("div");
document.body.append(host);
@@ -85,6 +109,9 @@ function mountOverlayWithHeadline(initialLeft: number): OverlayHarness {
`;
const movedElement = doc.getElementById("headline");
if (!movedElement) throw new Error("Expected test element");
+ const view = doc.defaultView?.MutationObserver ? doc.defaultView : globalThis;
+ const RealObserver = view.MutationObserver;
+ if (observerDelayMs > 0) view.MutationObserver = lateObserver(RealObserver, observerDelayMs);
Element.prototype.getBoundingClientRect = function (): DOMRect {
if (this === movedElement) {
@@ -125,6 +152,7 @@ function mountOverlayWithHeadline(initialLeft: number): OverlayHarness {
cleanup: () => {
act(() => root.unmount());
Element.prototype.getBoundingClientRect = originalGetBoundingClientRect;
+ view.MutationObserver = RealObserver;
iframe.remove();
host.remove();
},
@@ -132,51 +160,40 @@ function mountOverlayWithHeadline(initialLeft: number): OverlayHarness {
}
describe("off-canvas indicator refresh", () => {
- it("removes the indicator when an off-canvas element moves in-canvas (off->on)", async () => {
- const h = mountOverlayWithHeadline(760);
- try {
- await act(async () => {
- await flushAnimationFrames();
- });
- expect(h.host.querySelector(INDICATOR)).toBeTruthy();
-
- act(() => {
- h.movedElement.style.left = "120px";
- });
- await act(async () => {
- await Promise.resolve();
- await flushAnimationFrames();
- });
-
- expect(h.host.querySelector(INDICATOR)).toBeNull();
- } finally {
- h.cleanup();
- }
- });
+ for (const [label, delayMs] of [
+ ["", 0],
+ [" when mutation delivery lags past a fixed wait", 300],
+ ] as const) {
+ it(`removes the indicator when an off-canvas element moves in-canvas (off->on)${label}`, async () => {
+ const h = mountOverlayWithHeadline(760, delayMs);
+ try {
+ await untilIndicator(h.host, (indicator) => expect(indicator).toBeTruthy());
+
+ act(() => {
+ h.movedElement.style.left = "120px";
+ });
+ await untilIndicator(h.host, (indicator) => expect(indicator).toBeNull());
+ } finally {
+ h.cleanup();
+ }
+ });
+ }
it("tracks the indicator to the new position when it stays off-canvas (off->off)", async () => {
const h = mountOverlayWithHeadline(760);
try {
- await act(async () => {
- await flushAnimationFrames();
- });
+ await untilIndicator(h.host, (indicator) => expect(indicator).toBeTruthy());
const before = h.host.querySelector(INDICATOR);
- expect(before).toBeTruthy();
const leftBefore = (before!.parentElement as HTMLElement).style.left;
// Move further off-canvas (still outside the 800px-wide composition).
act(() => {
h.movedElement.style.left = "1200px";
});
- await act(async () => {
- await Promise.resolve();
- await flushAnimationFrames();
+ await untilIndicator(h.host, (indicator) => {
+ expect(indicator).toBeTruthy();
+ expect((indicator!.parentElement as HTMLElement).style.left).not.toEqual(leftBefore);
});
-
- const after = h.host.querySelector(INDICATOR);
- expect(after).toBeTruthy();
- const leftAfter = (after!.parentElement as HTMLElement).style.left;
- expect(leftAfter).not.toEqual(leftBefore);
} finally {
h.cleanup();
}