|
1 | 1 | import React from "react" |
2 | 2 |
|
3 | | -import { render, screen } from "@/utils/test-utils" |
| 3 | +import { fireEvent, render, screen } from "@/utils/test-utils" |
4 | 4 | import { EXTERNAL_LINKS } from "@/constants/externalLinks" |
| 5 | +import { vscode } from "@/utils/vscode" |
5 | 6 |
|
6 | 7 | import Announcement from "../Announcement" |
7 | 8 |
|
@@ -83,4 +84,61 @@ describe("Announcement", () => { |
83 | 84 | EXTERNAL_LINKS.MODELS, |
84 | 85 | ) |
85 | 86 | }) |
| 87 | + |
| 88 | + it("posts each announcement link to the extension host exactly once", () => { |
| 89 | + render(<Announcement hideAnnouncement={vi.fn()} />) |
| 90 | + |
| 91 | + fireEvent.click(screen.getByRole("link", { name: "https://zoocode.dev/models" })) |
| 92 | + fireEvent.click(screen.getByRole("link", { name: "GitHub" })) |
| 93 | + fireEvent.click(screen.getByRole("link", { name: "X" })) |
| 94 | + fireEvent.click(screen.getByRole("link", { name: "Discord" })) |
| 95 | + fireEvent.click(screen.getByRole("link", { name: "Reddit" })) |
| 96 | + |
| 97 | + expect(vscode.postMessage).toHaveBeenCalledTimes(5) |
| 98 | + expect(vscode.postMessage).toHaveBeenNthCalledWith(1, { type: "openExternal", url: EXTERNAL_LINKS.MODELS }) |
| 99 | + expect(vscode.postMessage).toHaveBeenNthCalledWith(2, { type: "openExternal", url: EXTERNAL_LINKS.GITHUB_REPO }) |
| 100 | + expect(vscode.postMessage).toHaveBeenNthCalledWith(3, { type: "openExternal", url: "https://x.com/ZooCodeDev" }) |
| 101 | + expect(vscode.postMessage).toHaveBeenNthCalledWith(4, { |
| 102 | + type: "openExternal", |
| 103 | + url: "https://discord.gg/VxfP4Vx3gX", |
| 104 | + }) |
| 105 | + expect(vscode.postMessage).toHaveBeenNthCalledWith(5, { |
| 106 | + type: "openExternal", |
| 107 | + url: "https://www.reddit.com/r/ZooCode/", |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + // VS Code's webview bootstrap intercepts clicks on any anchor with an href |
| 112 | + // at the document level and never checks defaultPrevented, so the |
| 113 | + // announcement links must stop propagation to avoid opening twice. |
| 114 | + it("keeps announcement link clicks from reaching the document level", () => { |
| 115 | + const documentClick = vi.fn() |
| 116 | + document.addEventListener("click", documentClick) |
| 117 | + |
| 118 | + try { |
| 119 | + render( |
| 120 | + <div> |
| 121 | + <a href="#control">control</a> |
| 122 | + <Announcement hideAnnouncement={vi.fn()} /> |
| 123 | + </div>, |
| 124 | + ) |
| 125 | + |
| 126 | + fireEvent.click(screen.getByRole("link", { name: "GitHub" })) |
| 127 | + expect(documentClick).not.toHaveBeenCalled() |
| 128 | + |
| 129 | + fireEvent.click(screen.getByText("control")) |
| 130 | + expect(documentClick).toHaveBeenCalledTimes(1) |
| 131 | + } finally { |
| 132 | + document.removeEventListener("click", documentClick) |
| 133 | + } |
| 134 | + }) |
| 135 | + |
| 136 | + it("hides the announcement when the dialog closes", () => { |
| 137 | + const hideAnnouncement = vi.fn() |
| 138 | + render(<Announcement hideAnnouncement={hideAnnouncement} />) |
| 139 | + |
| 140 | + fireEvent.keyDown(document.body, { key: "Escape" }) |
| 141 | + |
| 142 | + expect(hideAnnouncement).toHaveBeenCalledTimes(1) |
| 143 | + }) |
86 | 144 | }) |
0 commit comments