-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathmenu.test.ts
More file actions
322 lines (258 loc) · 10 KB
/
menu.test.ts
File metadata and controls
322 lines (258 loc) · 10 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { Menu, shell } from 'electron';
import { autoUpdater } from 'electron-updater';
import type { Menubar } from 'menubar';
import type { Mock } from 'vitest';
import { APPLICATION } from '../shared/constants';
import { isMacOS } from '../shared/platform';
import { resetApp } from './lifecycle/reset';
import MenuBuilder from './menu';
import { openLogsDirectory, takeScreenshot } from './utils';
// Track MenuItem instantiations for test assertions
const menuItemInstances: Array<{
label?: string;
enabled?: boolean;
visible?: boolean;
click?: () => void;
}> = [];
vi.mock('electron', () => {
class MockMenuItem {
constructor(opts: Record<string, unknown>) {
Object.assign(this, opts);
menuItemInstances.push(opts as (typeof menuItemInstances)[number]);
}
}
return {
Menu: {
buildFromTemplate: vi.fn(),
},
MenuItem: MockMenuItem,
shell: { openExternal: vi.fn() },
};
});
vi.mock('electron-updater', () => ({
autoUpdater: {
checkForUpdatesAndNotify: vi.fn(),
quitAndInstall: vi.fn(),
},
}));
vi.mock('./utils', () => ({
takeScreenshot: vi.fn(),
openLogsDirectory: vi.fn(),
}));
vi.mock('./lifecycle/reset', () => ({
resetApp: vi.fn(),
}));
vi.mock('../shared/platform', () => ({
isMacOS: vi.fn(),
}));
describe('main/menu.ts', () => {
let menubar: Menubar;
let menuBuilder: MenuBuilder;
/** Helper: find MenuItem config captured via our tracking array by label */
const getMenuItemConfigByLabel = (label: string) =>
menuItemInstances.find((item) => item.label === label) as
| {
label?: string;
enabled?: boolean;
visible?: boolean;
click?: () => void;
}
| undefined;
/** Lightweight type describing the (subset) of fields we inspect on template items */
type TemplateItem = {
label?: string;
role?: string;
accelerator?: string;
submenu?: TemplateItem[];
click?: () => void;
};
/** Helper: build menu & return template (first arg passed to buildFromTemplate) */
const buildAndGetTemplate = () => {
menuBuilder.buildMenu();
return (Menu.buildFromTemplate as Mock).mock.calls.slice(
-1,
)[0][0] as TemplateItem[];
};
beforeEach(() => {
vi.mocked(isMacOS).mockReturnValue(false);
menuItemInstances.length = 0; // Clear tracked instances
menubar = { app: { quit: vi.fn() } } as unknown as Menubar;
menuBuilder = new MenuBuilder(menubar);
});
describe('checkForUpdatesMenuItem', () => {
it('default menu configuration', () => {
const config = getMenuItemConfigByLabel('Check for updates');
expect(config).toBeDefined();
expect(config?.label).toBe('Check for updates');
expect(config?.enabled).toBe(true);
expect(config?.click).toEqual(expect.any(Function));
});
it('should enable menu item', () => {
menuBuilder.setCheckForUpdatesMenuEnabled(true);
// biome-ignore lint/complexity/useLiteralKeys: This is a test
expect(menuBuilder['checkForUpdatesMenuItem'].enabled).toBe(true);
});
it('should disable menu item', () => {
menuBuilder.setCheckForUpdatesMenuEnabled(false);
// biome-ignore lint/complexity/useLiteralKeys: This is a test
expect(menuBuilder['checkForUpdatesMenuItem'].enabled).toBe(false);
});
});
describe('noUpdateAvailableMenuItem', () => {
it('default menu configuration', () => {
const config = getMenuItemConfigByLabel('No updates available');
expect(config).toBeDefined();
expect(config?.label).toBe('No updates available');
expect(config?.enabled).toBe(false);
expect(config?.visible).toBe(false);
});
it('should show menu item', () => {
menuBuilder.setNoUpdateAvailableMenuVisibility(true);
// biome-ignore lint/complexity/useLiteralKeys: This is a test
expect(menuBuilder['noUpdateAvailableMenuItem'].visible).toBe(true);
});
it('should hide menu item', () => {
menuBuilder.setNoUpdateAvailableMenuVisibility(false);
// biome-ignore lint/complexity/useLiteralKeys: This is a test
expect(menuBuilder['noUpdateAvailableMenuItem'].visible).toBe(false);
});
});
describe('updateAvailableMenuItem', () => {
it('default menu configuration', () => {
const config = getMenuItemConfigByLabel('An update is available');
expect(config).toBeDefined();
expect(config?.label).toBe('An update is available');
expect(config?.enabled).toBe(false);
expect(config?.visible).toBe(false);
});
it('should show menu item', () => {
menuBuilder.setUpdateAvailableMenuVisibility(true);
// biome-ignore lint/complexity/useLiteralKeys: This is a test
expect(menuBuilder['updateAvailableMenuItem'].visible).toBe(true);
});
it('should hide menu item', () => {
menuBuilder.setUpdateAvailableMenuVisibility(false);
// biome-ignore lint/complexity/useLiteralKeys: This is a test
expect(menuBuilder['updateAvailableMenuItem'].visible).toBe(false);
});
});
describe('updateReadyForInstallMenuItem', () => {
it('default menu configuration', () => {
const config = getMenuItemConfigByLabel('Restart to install update');
expect(config).toBeDefined();
expect(config?.label).toBe('Restart to install update');
expect(config?.enabled).toBe(true);
expect(config?.visible).toBe(false);
expect(config?.click).toEqual(expect.any(Function));
});
it('should show menu item', () => {
menuBuilder.setUpdateReadyForInstallMenuVisibility(true);
// biome-ignore lint/complexity/useLiteralKeys: This is a test
expect(menuBuilder['updateReadyForInstallMenuItem'].visible).toBe(true);
});
it('should hide menu item', () => {
menuBuilder.setUpdateReadyForInstallMenuVisibility(false);
// biome-ignore lint/complexity/useLiteralKeys: This is a test
expect(menuBuilder['updateReadyForInstallMenuItem'].visible).toBe(false);
});
});
describe('click handlers', () => {
it('invokes autoUpdater.checkForUpdatesAndNotify when clicking "Check for updates"', () => {
const cfg = getMenuItemConfigByLabel('Check for updates');
expect(cfg).toBeDefined();
cfg?.click?.();
expect(autoUpdater.checkForUpdatesAndNotify).toHaveBeenCalled();
});
it('invokes autoUpdater.quitAndInstall when clicking "Restart to install update"', () => {
const cfg = getMenuItemConfigByLabel('Restart to install update');
expect(cfg).toBeDefined();
cfg?.click?.();
expect(autoUpdater.quitAndInstall).toHaveBeenCalled();
});
it('developer submenu click actions execute expected functions', () => {
const template = buildAndGetTemplate();
const devEntry = template.find(
(item) => item?.label === 'Developer',
) as TemplateItem;
expect(devEntry).toBeDefined();
const submenu = devEntry.submenu ?? [];
const clickByLabel = (label: string) =>
submenu.find((i) => i.label === label)?.click?.();
clickByLabel('Take Screenshot');
expect(takeScreenshot).toHaveBeenCalledWith(menubar);
clickByLabel('View Application Logs');
expect(openLogsDirectory).toHaveBeenCalled();
clickByLabel('Visit Repository');
expect(shell.openExternal).toHaveBeenCalledWith(
`https://github.com/${APPLICATION.REPO_SLUG}`,
);
clickByLabel(`Reset ${APPLICATION.NAME}`);
expect(resetApp).toHaveBeenCalledWith(menubar);
});
it('website menu item opens external URL', () => {
const template = buildAndGetTemplate();
const item = template.find((i) => i.label === 'Visit Website');
item?.click?.();
expect(shell.openExternal).toHaveBeenCalledWith(APPLICATION.WEBSITE);
});
it('quit menu item quits the app', () => {
const template = buildAndGetTemplate();
const item = template.find((i) => i.label === `Quit ${APPLICATION.NAME}`);
item?.click?.();
expect(menubar.app.quit).toHaveBeenCalled();
});
it('developer submenu includes expected static accelerators', () => {
const template = buildAndGetTemplate();
const devEntry = template.find(
(item) => item?.label === 'Developer',
) as TemplateItem;
const reloadItem = (devEntry.submenu ?? []).find(
(i) => i.role === 'reload',
);
expect(reloadItem?.accelerator).toBe('CommandOrControl+R');
});
});
describe('platform-specific accelerators', () => {
// We test the accelerator values using the actual menu template
// The isMacOS function is called during buildMenu, so we can verify the expected behavior
// by building the menu and inspecting the template
it('uses mac accelerator for toggleDevTools when on macOS', async () => {
vi.mocked(isMacOS).mockReturnValue(true);
menuItemInstances.length = 0;
(Menu.buildFromTemplate as Mock).mockClear();
const mb = new MenuBuilder({
app: { quit: vi.fn() },
} as unknown as Menubar);
mb.buildMenu();
const template = (Menu.buildFromTemplate as Mock).mock.calls.slice(
-1,
)[0][0] as TemplateItem[];
const devEntry = template.find(
(i) => i?.label === 'Developer',
) as TemplateItem;
const toggleItem = devEntry.submenu?.find(
(i) => i.role === 'toggleDevTools',
);
expect(toggleItem?.accelerator).toBe('Alt+Cmd+I');
});
it('uses non-mac accelerator for toggleDevTools otherwise', async () => {
vi.mocked(isMacOS).mockReturnValue(false);
menuItemInstances.length = 0;
(Menu.buildFromTemplate as Mock).mockClear();
const mb = new MenuBuilder({
app: { quit: vi.fn() },
} as unknown as Menubar);
mb.buildMenu();
const template = (Menu.buildFromTemplate as Mock).mock.calls.slice(
-1,
)[0][0] as TemplateItem[];
const devEntry = template.find(
(i) => i?.label === 'Developer',
) as TemplateItem;
const toggleItem = devEntry.submenu?.find(
(i) => i.role === 'toggleDevTools',
);
expect(toggleItem?.accelerator).toBe('Ctrl+Shift+I');
});
});
});