|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { act } from "react"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { usePlayerStore } from "../store/playerStore"; |
| 6 | +import { |
| 7 | + attachIframeWindow, |
| 8 | + makeAdapterWindow, |
| 9 | + renderTimelinePlayerHarness, |
| 10 | + resetPlayerStore, |
| 11 | +} from "./timelinePlayerTestHarness"; |
| 12 | + |
| 13 | +globalThis.IS_REACT_ACT_ENVIRONMENT = true; |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + vi.restoreAllMocks(); |
| 17 | + document.body.innerHTML = ""; |
| 18 | + resetPlayerStore(); |
| 19 | +}); |
| 20 | + |
| 21 | +describe("useTimelinePlayer tab hidden while playing", () => { |
| 22 | + it("pauses on the adapter's time, like any other pause", () => { |
| 23 | + const { api, root } = renderTimelinePlayerHarness(); |
| 24 | + const { adapter, win } = makeAdapterWindow(); |
| 25 | + attachIframeWindow(api, win); |
| 26 | + act(() => api.play()); |
| 27 | + adapter.seek(6); |
| 28 | + |
| 29 | + vi.spyOn(document, "hidden", "get").mockReturnValue(true); |
| 30 | + act(() => void document.dispatchEvent(new Event("visibilitychange"))); |
| 31 | + |
| 32 | + expect(usePlayerStore.getState().isPlaying).toBe(false); |
| 33 | + expect(usePlayerStore.getState().currentTime).toBe(6); |
| 34 | + act(() => root.unmount()); |
| 35 | + }); |
| 36 | + |
| 37 | + it("stops a reverse shuttle for good", async () => { |
| 38 | + const { api, root } = renderTimelinePlayerHarness(); |
| 39 | + const { adapter, win } = makeAdapterWindow(); |
| 40 | + attachIframeWindow(api, win); |
| 41 | + adapter.seek(20); |
| 42 | + act(() => void window.dispatchEvent(new KeyboardEvent("keydown", { code: "KeyJ", key: "j" }))); |
| 43 | + await act(() => new Promise((r) => setTimeout(r, 100))); |
| 44 | + expect(usePlayerStore.getState().isPlaying).toBe(true); |
| 45 | + |
| 46 | + vi.spyOn(document, "hidden", "get").mockReturnValue(true); |
| 47 | + act(() => void document.dispatchEvent(new Event("visibilitychange"))); |
| 48 | + const stoppedAt = adapter.getTime(); |
| 49 | + await act(() => new Promise((r) => setTimeout(r, 200))); |
| 50 | + |
| 51 | + expect(usePlayerStore.getState().isPlaying).toBe(false); |
| 52 | + expect(adapter.getTime()).toBe(stoppedAt); |
| 53 | + expect(usePlayerStore.getState().currentTime).toBe(stoppedAt); |
| 54 | + act(() => root.unmount()); |
| 55 | + }); |
| 56 | + |
| 57 | + it("does not carry the shuttle speed past the pause", () => { |
| 58 | + const { api, root } = renderTimelinePlayerHarness(); |
| 59 | + attachIframeWindow(api, makeAdapterWindow().win); |
| 60 | + const pressL = () => |
| 61 | + act( |
| 62 | + () => void window.dispatchEvent(new KeyboardEvent("keydown", { code: "KeyL", key: "l" })), |
| 63 | + ); |
| 64 | + pressL(); |
| 65 | + pressL(); |
| 66 | + expect(usePlayerStore.getState().playbackRate).toBe(2); |
| 67 | + |
| 68 | + vi.spyOn(document, "hidden", "get").mockReturnValue(true); |
| 69 | + act(() => void document.dispatchEvent(new Event("visibilitychange"))); |
| 70 | + pressL(); |
| 71 | + |
| 72 | + expect(usePlayerStore.getState().playbackRate).toBe(1); |
| 73 | + act(() => root.unmount()); |
| 74 | + }); |
| 75 | +}); |
0 commit comments