1import type { DesktopBridge } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { DESKTOP_PASTE_AS_TEXT_EVENT, installDesktopPasteAsText } from "./desktopPasteAsText";
4
5describe("desktop paste as text", () => {
6 it.each([false, true])("pastes with a mounted composer: %s", (hasComposer) => {
7 const target = new EventTarget();
8 let menuAction: ((action: string) => void) | undefined;
9 const order: string[] = [];
10 const bridge = {
11 onMenuAction: (listener) => {
12 menuAction = listener;
13 return () => {
14 menuAction = undefined;
15 };
16 },
17 pasteAsText: vi.fn(async () => {
18 order.push("paste");
19 }),
20 } satisfies Pick<DesktopBridge, "onMenuAction" | "pasteAsText">;
21 if (hasComposer)
22 target.addEventListener(DESKTOP_PASTE_AS_TEXT_EVENT, () => order.push("armed"));
23 const uninstall = installDesktopPasteAsText(bridge, target);
24 menuAction?.("open-settings");
25 expect(order).toEqual([]);
26 menuAction?.("paste-as-text");
27 expect(order).toEqual(hasComposer ? ["armed", "paste"] : ["paste"]);
28 uninstall?.();
29 menuAction?.("paste-as-text");
30 expect(bridge.pasteAsText).toHaveBeenCalledOnce();
31 });
32});