|
1 | 1 | // @vitest-environment happy-dom |
2 | 2 | import { act } from "react"; |
3 | | -import { afterEach, expect, it, vi } from "vitest"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { PARITY_MODES as MODES } from "../../../tests/parityModes"; |
4 | 5 | import { usePlayerStore } from "../store/playerStore"; |
5 | 6 | import { |
6 | 7 | detachedFakeIframe, |
@@ -37,21 +38,21 @@ const RELOADED = { |
37 | 38 | clips: [clip("a", "p", 0), clip("dropped", "video", 1)], |
38 | 39 | }; |
39 | 40 |
|
40 | | -/** A reloaded film whose runtime holds RELOADED: on its window, and over its editing port once sealed. */ |
41 | | -function reloadedFilm() { |
| 41 | +/** A reloaded film whose runtime holds `manifest`: on its window, and over its editing port once sealed. */ |
| 42 | +function reloadedFilm(film = FILM, manifest: typeof RELOADED = RELOADED) { |
42 | 43 | const { win } = makeAdapterWindow({ duration: 2 }); |
43 | 44 | const frame = win as unknown as Record<string, unknown>; |
44 | 45 | const iframe = detachedFakeIframe(frame); |
45 | 46 | Object.defineProperty(iframe, "isConnected", { value: true }); |
46 | | - iframe.contentDocument!.body.innerHTML = FILM; |
47 | | - frame.__clipManifest = RELOADED; |
| 47 | + iframe.contentDocument!.body.innerHTML = film; |
| 48 | + frame.__clipManifest = manifest; |
48 | 49 | const markup = { |
49 | | - html: `<!doctype html><html><body>${FILM}</body></html>`, |
| 50 | + html: `<!doctype html><html><body>${film}</body></html>`, |
50 | 51 | baseURI: "http://localhost/", |
51 | 52 | }; |
52 | 53 | closers.push( |
53 | 54 | serveEditingRequests(frame, (kind) => |
54 | | - kind === "runtime-snapshot" ? { clipManifest: RELOADED, clipParents: [] } : markup, |
| 55 | + kind === "runtime-snapshot" ? { clipManifest: manifest, clipParents: [] } : markup, |
55 | 56 | ), |
56 | 57 | ); |
57 | 58 | // A cross-origin window: reading its document or globals throws. |
@@ -108,3 +109,58 @@ it.each(["scriptable", "sealed"] as const)( |
108 | 109 | act(() => root.unmount()); |
109 | 110 | }, |
110 | 111 | ); |
| 112 | + |
| 113 | +const ONE_CLIP = `<div data-composition-id="main"><p id="a"></p></div>`; |
| 114 | +// Clip "a" as either mode derives it from ONE_CLIP, less the fields that name it on its page. |
| 115 | +const A_BY_POSITION = { |
| 116 | + id: "a", |
| 117 | + label: "a", |
| 118 | + kind: "element", |
| 119 | + tag: "p", |
| 120 | + start: 0, |
| 121 | + duration: 2, |
| 122 | + track: 0, |
| 123 | + authoredTrack: 0, |
| 124 | + stackingContextId: null, |
| 125 | + compositionScope: "main", |
| 126 | + zIndex: 0, |
| 127 | +}; |
| 128 | + |
| 129 | +describe.each(MODES)("a %s preview", (mode) => { |
| 130 | + // fails without the fix: a clip list read again that changed only a clip's ids counted as unchanged and was dropped. |
| 131 | + it("gives a clip its ids when its list is read again with the same clips", async () => { |
| 132 | + const { getApi, root } = renderTimelinePlayerHarness(); |
| 133 | + const live = makeFakeIframe(makeAdapterWindow({ duration: 2 }).win); |
| 134 | + act(() => { |
| 135 | + getApi().iframeRef.current = live; |
| 136 | + getApi().onIframeLoad(); |
| 137 | + }); |
| 138 | + // As a film read before its page loaded leaves it: the clip, by position only. |
| 139 | + act(() => usePlayerStore.getState().setElements([A_BY_POSITION])); |
| 140 | + act(() => getApi().refreshPlayer()); |
| 141 | + const gen = getApi().previewSlots.find((slot) => slot.role === "shadow")!.gen; |
| 142 | + const shadow = reloadedFilm(ONE_CLIP, { ...RELOADED, clips: [clip("a", "p", 0)] }); |
| 143 | + act(() => { |
| 144 | + getApi().setShadowIframeNode(shadow.iframe); |
| 145 | + getApi().onShadowIframeLoad(gen); |
| 146 | + }); |
| 147 | + if (mode === "sealed") shadow.seal(); |
| 148 | + act(() => getApi().onShadowReadyChange(gen, true)); |
| 149 | + act(() => { |
| 150 | + window.dispatchEvent( |
| 151 | + new MessageEvent("message", { |
| 152 | + source: shadow.frame as unknown as Window, |
| 153 | + origin: window.location.origin, |
| 154 | + data: { source: "hf-preview", type: "state", isPlaying: false }, |
| 155 | + }), |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + await vi.waitFor(() => |
| 160 | + expect(usePlayerStore.getState().elements).toEqual([ |
| 161 | + { ...A_BY_POSITION, key: "index.html#a", domId: "a", selector: "#a" }, |
| 162 | + ]), |
| 163 | + ); |
| 164 | + act(() => root.unmount()); |
| 165 | + }); |
| 166 | +}); |
0 commit comments