Open History to see the last commit
1import type { DesktopUpdateState } from "@t3tools/contracts";
2import { isValidElement, type MouseEvent, type ReactElement, type ReactNode } from "react";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5const testState = vi.hoisted(() => ({
6 addToast: vi.fn(),
7}));
8
9vi.mock("../ui/toast", () => ({
10 toastManager: { add: testState.addToast },
11}));
12
13import { SidebarUpdateReleaseNotes } from "./SidebarUpdateReleaseNotes";
14
15type AnchorElement = ReactElement<{
16 readonly children?: ReactNode;
17 readonly href?: string;
18 readonly onClick?: (event: MouseEvent<HTMLAnchorElement>) => void;
19}>;
20
21const baseState: DesktopUpdateState = {
22 enabled: true,
23 status: "available",
24 channel: "nightly",
25 currentVersion: "0.0.35",
26 hostArch: "arm64",
27 appArch: "arm64",
28 runningUnderArm64Translation: false,
29 availableVersion: "0.0.36-nightly.3",
30 downloadedVersion: null,
31 releaseNotes: [],
32 omittedReleaseCount: 0,
33 downloadPercent: null,
34 checkedAt: null,
35 message: null,
36 errorContext: null,
37 canRetry: false,
38};
39
40function collectAnchors(node: ReactNode, anchors: AnchorElement[] = []): AnchorElement[] {
41 if (Array.isArray(node)) {
42 for (const child of node) collectAnchors(child, anchors);
43 return anchors;
44 }
45 if (!isValidElement(node)) return anchors;
46
47 const element = node as ReactElement<{ readonly children?: ReactNode }>;
48 if (typeof element.type === "function") {
49 const render = element.type as (props: unknown) => ReactNode;
50 return collectAnchors(render(element.props), anchors);
51 }
52 if (element.type === "a") anchors.push(element as AnchorElement);
53 return collectAnchors(element.props.children, anchors);
54}
55
56function textContent(node: ReactNode): string {
57 if (typeof node === "string" || typeof node === "number") return String(node);
58 if (Array.isArray(node)) return node.map(textContent).join("");
59 if (!isValidElement(node)) return "";
60 const element = node as ReactElement<{ readonly children?: ReactNode }>;
61 return textContent(element.props.children);
62}
63
64function renderNotes(state: DesktopUpdateState, openExternal = vi.fn().mockResolvedValue(true)) {
65 return SidebarUpdateReleaseNotes({
66 shell: { openExternal },
67 state,
68 tooltip: "Update available",
69 });
70}
71
72describe("SidebarUpdateReleaseNotes", () => {
73 beforeEach(() => {
74 testState.addToast.mockReset();
75 });
76
77 afterEach(() => {
78 vi.unstubAllEnvs();
79 });
80
81 it("uses the configured repository for release links", () => {
82 vi.stubEnv("T3CODE_DESKTOP_UPDATE_REPOSITORY", "JustYannicc/t3code");
83 const anchors = collectAnchors(
84 renderNotes({
85 ...baseState,
86 releaseNotes: [{ version: "0.0.36-nightly.3", items: ["Change 3"], totalItems: 1 }],
87 omittedReleaseCount: 1,
88 }),
89 );
90
91 expect(anchors.map(({ props }) => props.href)).toEqual([
92 "https://github.com/JustYannicc/t3code/releases/tag/v0.0.36-nightly.3",
93 "https://github.com/JustYannicc/t3code/releases",
94 ]);
95 });
96
97 it("links each preview to its exact release and labels hidden changes", () => {
98 const anchors = collectAnchors(
99 renderNotes({
100 ...baseState,
101 releaseNotes: [
102 { version: "0.0.36-nightly.3", items: ["Change 3"], totalItems: 1 },
103 { version: "0.0.36-nightly.2", items: ["Change 2"], totalItems: 2 },
104 { version: "0.0.36-nightly.1", items: ["Change 1", "Earlier"], totalItems: 4 },
105 ],
106 }),
107 );
108
109 expect(anchors.map(({ props }) => props.href)).toEqual([
110 "https://github.com/pingdotgg/t3code/releases/tag/v0.0.36-nightly.3",
111 "https://github.com/pingdotgg/t3code/releases/tag/v0.0.36-nightly.2",
112 "https://github.com/pingdotgg/t3code/releases/tag/v0.0.36-nightly.1",
113 ]);
114 expect(anchors.map(({ props }) => textContent(props.children))).toEqual([
115 "View release on GitHub",
116 "1 more change on GitHub",
117 "2 more changes on GitHub",
118 ]);
119 });
120
121 it("links omitted releases to release history", () => {
122 const anchors = collectAnchors(
123 renderNotes({
124 ...baseState,
125 releaseNotes: [{ version: "0.0.36-nightly.3", items: ["Change 3"], totalItems: 1 }],
126 omittedReleaseCount: 1,
127 }),
128 );
129
130 expect(anchors.at(-1)?.props.href).toBe("https://github.com/pingdotgg/t3code/releases");
131 expect(textContent(anchors.at(-1)?.props.children)).toBe("1 older release on GitHub");
132 });
133
134 it("shows plural history text for multiple omitted releases", () => {
135 const anchors = collectAnchors(
136 renderNotes({
137 ...baseState,
138 releaseNotes: [{ version: "0.0.36-nightly.3", items: ["Change 3"], totalItems: 1 }],
139 omittedReleaseCount: 3,
140 }),
141 );
142
143 expect(textContent(anchors.at(-1)?.props.children)).toBe("3 older releases on GitHub");
144 });
145
146 it("reports a release link that fails to open", async () => {
147 const openExternal = vi.fn().mockResolvedValue(false);
148 const [anchor] = collectAnchors(
149 renderNotes(
150 {
151 ...baseState,
152 releaseNotes: [{ version: "0.0.36-nightly.3", items: ["Change 3"], totalItems: 1 }],
153 },
154 openExternal,
155 ),
156 );
157 const preventDefault = vi.fn();
158
159 anchor?.props.onClick?.({ preventDefault } as unknown as MouseEvent<HTMLAnchorElement>);
160
161 expect(preventDefault).toHaveBeenCalledOnce();
162 await vi.waitFor(() => {
163 expect(openExternal).toHaveBeenCalledWith(
164 "https://github.com/pingdotgg/t3code/releases/tag/v0.0.36-nightly.3",
165 );
166 expect(testState.addToast).toHaveBeenCalledWith({
167 type: "error",
168 title: "Unable to open release notes",
169 });
170 });
171 });
172});