-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathcheckForUpdate.test.ts
More file actions
181 lines (153 loc) · 5.6 KB
/
checkForUpdate.test.ts
File metadata and controls
181 lines (153 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from "vitest";
vi.mock("sonner", () => {
const fn = vi.fn();
const toast = Object.assign(fn, {
success: vi.fn(),
error: vi.fn(),
});
return { toast };
});
const localStorageStore: Record<string, string> = {};
Object.defineProperty(globalThis, "localStorage", {
value: {
getItem: (k: string) => localStorageStore[k] ?? null,
setItem: (k: string, v: string) => {
localStorageStore[k] = v;
},
removeItem: (k: string) => {
delete localStorageStore[k];
},
clear: () => {
for (const k of Object.keys(localStorageStore)) delete localStorageStore[k];
},
},
configurable: true,
});
const openExternalUrl = vi.fn().mockResolvedValue({ success: true });
Object.defineProperty(window, "electronAPI", {
value: { openExternalUrl },
configurable: true,
writable: true,
});
import { toast } from "sonner";
import { forceCheckForUpdate, isNewer, maybeShowUpdateToast } from "./checkForUpdate";
const toastMock = toast as unknown as Mock;
const toastSuccess = toast.success as Mock;
const toastError = toast.error as Mock;
function mockReleaseResponse(tagName: string, htmlUrl = "https://example.com/release") {
(global.fetch as Mock).mockResolvedValueOnce({
ok: true,
json: async () => ({ tag_name: tagName, html_url: htmlUrl }),
});
}
function resetUpdateCheckMocks() {
localStorage.clear();
global.fetch = vi.fn() as unknown as typeof fetch;
toastMock.mockClear();
toastSuccess.mockClear();
toastError.mockClear();
openExternalUrl.mockClear();
}
describe("isNewer", () => {
it("detects a higher minor version", () => {
expect(isNewer("1.3.0", "1.2.0")).toBe(true);
});
it("compares numerically rather than lexicographically", () => {
expect(isNewer("1.10.0", "1.2.0")).toBe(true);
});
it("returns false for equal versions", () => {
expect(isNewer("1.2.0", "1.2.0")).toBe(false);
});
it("returns false when current is newer", () => {
expect(isNewer("1.2.0", "1.3.0")).toBe(false);
});
it("rejects pre-release tags", () => {
expect(isNewer("1.3.0-beta.1", "1.2.0")).toBe(false);
});
});
describe("maybeShowUpdateToast", () => {
beforeEach(resetUpdateCheckMocks);
afterEach(() => {
vi.restoreAllMocks();
});
it("fires the update toast when a newer release exists", async () => {
mockReleaseResponse("v1.3.0");
await maybeShowUpdateToast("1.2.0");
expect(toastMock).toHaveBeenCalledTimes(1);
const [title, options] = toastMock.mock.calls[0];
expect(title).toBe("OpenScreen 1.3.0 is available");
expect(options.action.label).toBe("Download");
expect(options.cancel.label).toBe("Don't remind again");
expect(options.closeButton).toBe(true);
});
it("skips entirely when checks are disabled", async () => {
localStorage.setItem("openscreen_update_checks_disabled", "true");
await maybeShowUpdateToast("1.2.0");
expect(global.fetch).not.toHaveBeenCalled();
expect(toastMock).not.toHaveBeenCalled();
});
it("does not show toast when this version was dismissed", async () => {
localStorage.setItem("openscreen_dismissed_update_version", "1.3.0");
mockReleaseResponse("v1.3.0");
await maybeShowUpdateToast("1.2.0");
expect(global.fetch).toHaveBeenCalled();
expect(toastMock).not.toHaveBeenCalled();
});
it("stays silent on fetch failure", async () => {
(global.fetch as Mock).mockRejectedValueOnce(new Error("network down"));
await maybeShowUpdateToast("1.2.0");
expect(toastMock).not.toHaveBeenCalled();
expect(toastError).not.toHaveBeenCalled();
});
it("download action opens the release URL", async () => {
mockReleaseResponse("v1.3.0", "https://example.com/release/1.3.0");
await maybeShowUpdateToast("1.2.0");
const [, options] = toastMock.mock.calls[0];
await options.action.onClick();
expect(openExternalUrl).toHaveBeenCalledWith("https://example.com/release/1.3.0");
});
it("cancel action persists checks-disabled flag", async () => {
mockReleaseResponse("v1.3.0");
await maybeShowUpdateToast("1.2.0");
const [, options] = toastMock.mock.calls[0];
options.cancel.onClick();
expect(localStorage.getItem("openscreen_update_checks_disabled")).toBe("true");
});
it("close button dismissal persists per-version cache", async () => {
mockReleaseResponse("v1.3.0");
await maybeShowUpdateToast("1.2.0");
const [, options] = toastMock.mock.calls[0];
options.onDismiss();
expect(localStorage.getItem("openscreen_dismissed_update_version")).toBe("1.3.0");
});
});
describe("forceCheckForUpdate", () => {
beforeEach(resetUpdateCheckMocks);
afterEach(() => {
vi.restoreAllMocks();
});
it("bypasses checks-disabled and dismissed-version gates", async () => {
localStorage.setItem("openscreen_update_checks_disabled", "true");
localStorage.setItem("openscreen_dismissed_update_version", "1.3.0");
mockReleaseResponse("v1.3.0");
await forceCheckForUpdate("1.2.0");
expect(toastMock).toHaveBeenCalledTimes(1);
});
it("shows up-to-date success toast when current matches latest", async () => {
mockReleaseResponse("v1.2.0");
await forceCheckForUpdate("1.2.0");
expect(toastSuccess).toHaveBeenCalledTimes(1);
expect(toastSuccess.mock.calls[0][0]).toBe("OpenScreen is up to date");
});
it("shows up-to-date success toast when current is newer than latest", async () => {
mockReleaseResponse("v1.2.0");
await forceCheckForUpdate("1.3.0");
expect(toastSuccess).toHaveBeenCalledTimes(1);
});
it("shows error toast on fetch failure", async () => {
(global.fetch as Mock).mockRejectedValueOnce(new Error("offline"));
await forceCheckForUpdate("1.2.0");
expect(toastError).toHaveBeenCalledTimes(1);
expect(toastError.mock.calls[0][0]).toBe("Couldn't check for updates");
});
});