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

IssueDetailPanel.browser.test.tsx

162 lines5.1 KB

1import type { IssueDetail } from "@t3tools/contracts";
2import * as AsyncResult from "effect/reactivity/AsyncResult";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5​
6import { act, render } from "../../../test/render";
7​
8const url = "https://github.com/owner/repo/issues/7";
9const detail = vi.hoisted(() => ({
10 tracker: "github",
11 host: "github.com",
12 scope: "owner/repo",
13 key: "7",
14 displayKey: "#7",
15 title: "Something is broken",
16 url: "https://github.com/owner/repo/issues/7",
17 author: null,
18 state: { category: "open", name: "Open" },
19 createdAt: "2026-01-01T00:00:00.000Z",
20 updatedAt: "2026-01-01T00:00:00.000Z",
21 closedAt: null,
22 assignees: [],
23 labels: [],
24 commentCount: 0,
25 projectId: "project-1",
26 projectTitle: "Project",
27 writable: true,
28 capabilities: {
29 create: false,
30 edit: true,
31 comment: false,
32 labels: false,
33 assignees: false,
34 reactions: false,
35 templates: false,
36 linkedPullRequests: false,
37 events: false,
38 search: false,
39 },
40 viewerPermissions: {
41 setState: false,
42 comment: false,
43 edit: true,
44 labels: false,
45 assignees: false,
46 },
47 states: [],
48 viewer: null,
49 body: "",
50 comments: [],
51 commentsCursor: null,
52 events: [],
53 linkedPullRequests: [],
54})) as unknown as IssueDetail;
55​
56const state = vi.hoisted(() => ({ openLink: vi.fn(async () => {}) }));
57​
58vi.mock(import("~/browser/useOpenLink"), () => ({ useOpenLink: () => state.openLink }));
59vi.mock(import("./IssueThreadLinks"), () => ({ IssueThreadLinks: (() => null) as never }));
60vi.mock(import("~/state/issues"), async (original) => {
61 const { Atom } = await import("effect/reactivity");
62 const actual = await original();
63 const detailAtom = Atom.make(AsyncResult.success(detail));
64 const allowed = Atom.make(true);
65 return {
66 ...actual,
67 issueEnvironment: {
68 ...actual.issueEnvironment,
69 detail: (() => detailAtom) as never,
70 comment: { ...actual.issueEnvironment.comment, permissionAtom: () => allowed },
71 },
72 };
73});
74​
75import { RightPanelSheet } from "../RightPanelSheet";
76import { IssueDetailPanel } from "./IssueDetailPanel";
77​
78const reference = {
79 projectId: "project-1" as never,
80 tracker: "github" as never,
81 scope: "owner/repo",
82 key: "7",
83};
84​
85function setComments(comments: IssueDetail["comments"]) {
86 state.openLink.mockClear();
87 (detail as { comments: unknown }).comments = comments;
89​
90describe("IssueDetailPanel", () => {
91 it("opens the issue number through the link-target setting, not a popup", async () => {
92 setComments([]);
93 const threadRef = { environmentId: "env-1", threadId: "thread-1" } as never;
94 await using screen = await render(
95 <IssueDetailPanel
96 environmentId={"env-1" as never}
97 reference={reference}
98 threadRef={threadRef}
99 />,
100 );
101 const number = screen.getByRole("link", { name: "Open #7 on GitHub" });
102 const click = new MouseEvent("click", { bubbles: true, cancelable: true });
103 (await number.element()).dispatchEvent(click);
104​
105 expect(click.defaultPrevented).toBe(true);
106 expect(state.openLink).toHaveBeenCalledOnce();
107 expect(state.openLink).toHaveBeenCalledWith(url, { event: expect.anything() });
108 });
109​
110 it("opens a comment's timestamp through the link-target setting, not a popup", async () => {
111 const commentUrl = `${url}#issuecomment-1`;
112 setComments([
113 {
114 id: "1",
115 author: null,
116 body: "Same here",
117 createdAt: "2026-01-02T00:00:00.000Z",
118 url: commentUrl,
119 },
120 ]);
121 await using screen = await render(
122 <IssueDetailPanel
123 environmentId={"env-1" as never}
124 reference={reference}
125 threadRef={{ environmentId: "env-1", threadId: "thread-1" } as never}
126 />,
127 );
128 const link = await vi.waitFor(() => {
129 const found = screen.container.querySelector(`a[href="${commentUrl}"]`);
130 expect(found).not.toBeNull();
131 return found!;
132 });
133 const click = new MouseEvent("click", { bubbles: true, cancelable: true });
134 link.dispatchEvent(click);
135​
136 expect(click.defaultPrevented).toBe(true);
137 expect(state.openLink).toHaveBeenCalledOnce();
138 expect(state.openLink).toHaveBeenCalledWith(commentUrl, { event: expect.anything() });
139 });
140​
141 it("Escape cancels the title edit without closing the right panel sheet", async () => {
142 setComments([]);
143 const onCloseSheet = vi.fn();
144 await using _screen = await render(
145 <RightPanelSheet animationDurationMs={0} open onClose={onCloseSheet}>
146 <IssueDetailPanel environmentId={"env-1" as never} reference={reference} />
147 </RightPanelSheet>,
148 );
149 await act(() =>
150 (page.getByRole("button", { name: "Edit title" }).element() as HTMLElement).click(),
151 );
152 const input = page.getByRole("textbox", { name: "Issue title" });
153 await expect.element(input).toHaveFocus();
154​
155 await userEvent.keyboard("{Escape}");
156 await expect.element(input).not.toBeInTheDocument();
157 expect(onCloseSheet).not.toHaveBeenCalled();
158​
159 await userEvent.keyboard("{Escape}");
160 expect(onCloseSheet).toHaveBeenCalledTimes(1);
161 });
162});