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

openIssueLink.test.ts

73 lines2.4 KB

1import { EnvironmentId, ProjectId, SourceControlProviderKind, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4const state = vi.hoisted(() => ({ openIssue: vi.fn() }));
5const environmentId = EnvironmentId.make("env");
6​
7vi.mock("@tanstack/react-router", () => ({ useNavigate: () => vi.fn() }));
8vi.mock("../components/issue/issueListPreferences", () => ({
9 readIssueListPreferences: () => ({}),
10}));
11vi.mock("../rightPanelStore", () => ({
12 ISSUES_PANEL_REF: { environmentId: "issues", threadId: "issues" },
13 PULL_REQUESTS_PANEL_REF: { environmentId: "pulls", threadId: "pulls" },
14 useRightPanelStore: { getState: () => ({ openIssue: state.openIssue }) },
15}));
16vi.mock("../state/entities", () => ({
17 useProjects: () => [
18 {
19 id: ProjectId.make("project"),
20 environmentId,
21 repositoryIdentity: {
22 canonicalKey: "github.com/owner/repo",
23 provider: SourceControlProviderKind.make("github"),
24 displayName: "owner/repo",
25 owner: "owner",
26 name: "repo",
27 },
28 },
29 ],
30 useServerConfigs: () =>
31 new Map([[environmentId, { environment: { capabilities: { issues: true } } }]]),
32}));
33​
34const { useOpenIssueLink } = await import("./openIssueLink");
35​
36const url = "https://github.com/owner/repo/issues/1";
37const threadRef = { environmentId, threadId: ThreadId.make("thread") };
38​
39function setup() {
40 state.openIssue.mockReset();
42​
43function click(modifiers: { altKey?: boolean; shiftKey?: boolean } = {}) {
44 return {
45 metaKey: false,
46 ctrlKey: false,
47 ...modifiers,
48 preventDefault: vi.fn(),
49 stopPropagation: vi.fn(),
50 };
52​
53describe("useOpenIssueLink", () => {
54 it("opens a plain click on a bound repository's issue beside the thread", () => {
55 setup();
56 const event = click();
57 expect(useOpenIssueLink(threadRef)(event, url)).toBe(true);
58 expect(event.preventDefault).toHaveBeenCalled();
59 expect(state.openIssue).toHaveBeenCalledWith(
60 threadRef,
61 expect.objectContaining({ tracker: "github", scope: "owner/repo", key: "1" }),
62 );
63 });
64​
65 it.each(["altKey", "shiftKey"] as const)("leaves %s clicks to the ordinary link", (modifier) => {
66 setup();
67 const event = click({ [modifier]: true });
68 expect(useOpenIssueLink(threadRef)(event, url)).toBe(false);
69 expect(event.preventDefault).not.toHaveBeenCalled();
70 expect(event.stopPropagation).not.toHaveBeenCalled();
71 expect(state.openIssue).not.toHaveBeenCalled();
72 });
73});