|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { act } from "react"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | +import type { HfAudioFxChain } from "@hyperframes/core/audio-fx"; |
| 6 | +import { TimelineFxPopover } from "./TimelineFxPopover.js"; |
| 7 | + |
| 8 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 9 | + |
| 10 | +const EMPTY_CHAIN: HfAudioFxChain = { version: 1, nodes: [] }; |
| 11 | +const RECT = { left: 0, top: 0, right: 0, bottom: 0 } as DOMRect; |
| 12 | + |
| 13 | +function byTextButton(host: HTMLElement, text: string): HTMLButtonElement | undefined { |
| 14 | + return Array.from(host.querySelectorAll("button")).find((b) => b.textContent?.includes(text)); |
| 15 | +} |
| 16 | + |
| 17 | +function mount(overrides: Partial<Parameters<typeof TimelineFxPopover>[0]> = {}) { |
| 18 | + const onClose = vi.fn(); |
| 19 | + const onChainChange = vi.fn(); |
| 20 | + const onChainPreview = vi.fn(); |
| 21 | + const onOpenRack = vi.fn(); |
| 22 | + const host = document.createElement("div"); |
| 23 | + document.body.append(host); |
| 24 | + act(() => { |
| 25 | + createRoot(host).render( |
| 26 | + <TimelineFxPopover |
| 27 | + anchorRect={RECT} |
| 28 | + chain={EMPTY_CHAIN} |
| 29 | + onClose={onClose} |
| 30 | + onChainChange={onChainChange} |
| 31 | + onChainPreview={onChainPreview} |
| 32 | + onOpenRack={onOpenRack} |
| 33 | + {...overrides} |
| 34 | + />, |
| 35 | + ); |
| 36 | + }); |
| 37 | + return { host, onClose, onChainChange, onChainPreview, onOpenRack }; |
| 38 | +} |
| 39 | + |
| 40 | +afterEach(() => { |
| 41 | + document.body.innerHTML = ""; |
| 42 | +}); |
| 43 | + |
| 44 | +describe("TimelineFxPopover", () => { |
| 45 | + it("applies a preset with exactly one onChainChange write, and closes", () => { |
| 46 | + const { host, onChainChange, onClose } = mount(); |
| 47 | + const button = byTextButton(host, "Chipmunk"); |
| 48 | + expect(button).toBeDefined(); |
| 49 | + act(() => button?.click()); |
| 50 | + expect(onChainChange).toHaveBeenCalledTimes(1); |
| 51 | + const written = onChainChange.mock.calls[0]?.[0] as HfAudioFxChain; |
| 52 | + expect(written.nodes.length).toBeGreaterThan(0); |
| 53 | + expect(onClose).toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("auditions on hover (focus is the keyboard's hover) and reverts on leave", () => { |
| 57 | + const { host, onChainPreview } = mount(); |
| 58 | + const button = byTextButton(host, "Chipmunk"); |
| 59 | + expect(button).toBeDefined(); |
| 60 | + act(() => (button as HTMLButtonElement).focus()); |
| 61 | + expect(onChainPreview).toHaveBeenCalled(); |
| 62 | + const previewed = onChainPreview.mock.calls.at(-1)?.[0] as HfAudioFxChain; |
| 63 | + expect(previewed.nodes.length).toBeGreaterThan(0); |
| 64 | + onChainPreview.mockClear(); |
| 65 | + act(() => { |
| 66 | + button?.dispatchEvent(new FocusEvent("focusout", { bubbles: true })); |
| 67 | + }); |
| 68 | + expect(onChainPreview).toHaveBeenCalledWith(EMPTY_CHAIN); |
| 69 | + }); |
| 70 | + |
| 71 | + it("Escape closes without letting the keystroke propagate past the popover", () => { |
| 72 | + const { host, onClose } = mount(); |
| 73 | + const outer = vi.fn(); |
| 74 | + document.body.addEventListener("keydown", outer); |
| 75 | + const dialog = host.querySelector('[role="dialog"]'); |
| 76 | + expect(dialog).toBeTruthy(); |
| 77 | + act(() => { |
| 78 | + dialog?.dispatchEvent( |
| 79 | + new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }), |
| 80 | + ); |
| 81 | + }); |
| 82 | + expect(onClose).toHaveBeenCalled(); |
| 83 | + expect(outer).not.toHaveBeenCalled(); |
| 84 | + document.body.removeEventListener("keydown", outer); |
| 85 | + }); |
| 86 | + |
| 87 | + it("dismisses on an outside pointerdown", () => { |
| 88 | + const { onClose } = mount(); |
| 89 | + act(() => { |
| 90 | + document.body.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); |
| 91 | + }); |
| 92 | + expect(onClose).toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("does not dismiss on a pointerdown inside the popover", () => { |
| 96 | + const { host, onClose } = mount(); |
| 97 | + const dialog = host.querySelector('[role="dialog"]'); |
| 98 | + expect(dialog).toBeTruthy(); |
| 99 | + act(() => { |
| 100 | + dialog?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); |
| 101 | + }); |
| 102 | + expect(onClose).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("the footer opens the rack and closes the popover", () => { |
| 106 | + const { host, onOpenRack, onClose } = mount(); |
| 107 | + const openRack = byTextButton(host, "Open rack"); |
| 108 | + expect(openRack).toBeDefined(); |
| 109 | + act(() => openRack?.click()); |
| 110 | + expect(onOpenRack).toHaveBeenCalledTimes(1); |
| 111 | + expect(onClose).toHaveBeenCalled(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments