Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

externalLinkContextMenu.test.ts

147 lines4.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import { resolveExternalWebLinkHost, showExternalLinkContextMenu } from "./externalLinkContextMenu";
4​
5function createHarness(selection: "open-in-preview" | "open-external" | "copy-link" | null) {
6 const showContextMenu = vi.fn().mockResolvedValue(selection);
7 const openInPreview = vi.fn().mockResolvedValue(undefined);
8 const openExternal = vi.fn().mockResolvedValue(undefined);
9 const copyLink = vi.fn().mockResolvedValue(undefined);
10 const reportFailure = vi.fn();
11​
12 return {
13 showContextMenu,
14 openInPreview,
15 openExternal,
16 copyLink,
17 reportFailure,
18 };
20​
21describe("external chat link context menu", () => {
22 it("offers both open actions and Copy Link", async () => {
23 const harness = createHarness(null);
24​
25 await showExternalLinkContextMenu({
26 href: "https://example.com/docs?topic=menus#copy",
27 position: { x: 12, y: 24 },
28 ...harness,
29 });
30​
31 expect(harness.showContextMenu).toHaveBeenCalledWith(
32 [
33 { id: "open-in-preview", label: "Open in integrated browser" },
34 { id: "open-external", label: "Open in system browser" },
35 { id: "copy-link", label: "Copy Link" },
36 ],
37 { x: 12, y: 24 },
38 );
39 expect(harness.openInPreview).not.toHaveBeenCalled();
40 expect(harness.openExternal).not.toHaveBeenCalled();
41 expect(harness.copyLink).not.toHaveBeenCalled();
42 });
43​
44 it("still offers the link's own actions where the integrated browser cannot be opened", async () => {
45 const harness = createHarness(null);
46​
47 await showExternalLinkContextMenu({
48 href: "https://github.com/pingdotgg/t3code/pull/6169",
49 canOpenInPreview: false,
50 position: { x: 4, y: 8 },
51 ...harness,
52 });
53​
54 expect(harness.showContextMenu).toHaveBeenCalledWith(
55 [
56 { id: "open-external", label: "Open in system browser" },
57 { id: "copy-link", label: "Copy Link" },
58 ],
59 { x: 4, y: 8 },
60 );
61 });
62​
63 it("copies the exact destination without opening it", async () => {
64 const harness = createHarness("copy-link");
65 const href = "https://example.com/docs?topic=menus#copy";
66​
67 await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
68​
69 expect(harness.copyLink).toHaveBeenCalledWith(href);
70 expect(harness.openInPreview).not.toHaveBeenCalled();
71 expect(harness.openExternal).not.toHaveBeenCalled();
72 });
73​
74 it.each([
75 ["open-in-preview" as const, "openInPreview" as const],
76 ["open-external" as const, "openExternal" as const],
77 ])("preserves the %s action", async (selection, expectedCallback) => {
78 const harness = createHarness(selection);
79 const href = "https://example.com/docs";
80​
81 await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
82​
83 expect(harness[expectedCallback]).toHaveBeenCalledWith(href);
84 expect(harness.copyLink).not.toHaveBeenCalled();
85 });
86​
87 it("reports the selected action when it fails", async () => {
88 const harness = createHarness("copy-link");
89 const cause = new Error("clipboard denied");
90 harness.copyLink.mockRejectedValue(cause);
91​
92 await showExternalLinkContextMenu({
93 href: "https://example.com/docs",
94 position: { x: 1, y: 2 },
95 ...harness,
96 });
97​
98 expect(harness.reportFailure).toHaveBeenCalledWith("copy-link", cause);
99 });
100​
101 it("reports the menu operation when the native menu cannot be shown", async () => {
102 const harness = createHarness(null);
103 const cause = new Error("menu unavailable");
104 harness.showContextMenu.mockRejectedValue(cause);
105​
106 await showExternalLinkContextMenu({
107 href: "https://example.com/docs",
108 position: { x: 1, y: 2 },
109 ...harness,
110 });
111​
112 expect(harness.reportFailure).toHaveBeenCalledWith("show-link-context-menu", cause);
113 expect(harness.openInPreview).not.toHaveBeenCalled();
114 expect(harness.openExternal).not.toHaveBeenCalled();
115 expect(harness.copyLink).not.toHaveBeenCalled();
116 });
117​
118 it.each([
119 ["open-in-preview" as const, "openInPreview" as const, "open-link-in-preview"],
120 ["open-external" as const, "openExternal" as const, "open-link-external"],
121 ])("reports a failed %s action", async (selection, callback, operation) => {
122 const harness = createHarness(selection);
123 const cause = new Error("open failed");
124 harness[callback].mockRejectedValue(cause);
125​
126 await showExternalLinkContextMenu({
127 href: "https://example.com/docs",
128 position: { x: 1, y: 2 },
129 ...harness,
130 });
131​
132 expect(harness.reportFailure).toHaveBeenCalledWith(operation, cause);
133 });
134​
135 it.each([
136 ["https://example.com", "example.com"],
137 ["http://localhost:3000/path", "localhost"],
138 ["#details", null],
139 ["mailto:hello@example.com", null],
140 ["file:///tmp/example.txt", null],
141 ["javascript:void(0)", null],
142 ["not a URL", null],
143 [undefined, null],
144 ])("resolves the external web-link host for %s as %s", (href, expected) => {
145 expect(resolveExternalWebLinkHost(href)).toBe(expected);
146 });
147});