-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathsystem.test.ts
More file actions
140 lines (113 loc) · 4.53 KB
/
system.test.ts
File metadata and controls
140 lines (113 loc) · 4.53 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
import type { Menubar } from '@gitify/menubar';
import { EVENTS } from '../../shared/events';
import { applyKeepWindowOnBlur } from '../lifecycle/window';
import { registerSystemHandlers } from './system';
vi.mock('../lifecycle/window', () => ({
applyKeepWindowOnBlur: vi.fn(),
}));
const onMock = vi.fn();
const handleMock = vi.fn();
vi.mock('electron', () => ({
ipcMain: {
on: (...args: unknown[]) => onMock(...args),
handle: (...args: unknown[]) => handleMock(...args),
} satisfies Pick<Electron.IpcMain, 'on' | 'handle'>,
app: {
setLoginItemSettings: vi.fn(),
} satisfies Pick<Electron.App, 'setLoginItemSettings'>,
shell: {
openExternal: vi.fn(),
} satisfies Pick<Electron.Shell, 'openExternal'>,
}));
describe('main/handlers/system.ts', () => {
let menubar: Menubar;
let setGlobalShortcutMock: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.clearAllMocks();
setGlobalShortcutMock = vi.fn().mockReturnValue(true);
menubar = {
showWindow: vi.fn(),
hideWindow: vi.fn(),
setGlobalShortcut: setGlobalShortcutMock,
window: {
isVisible: vi.fn().mockReturnValue(false),
},
} as unknown as Menubar;
});
function getKeyboardShortcutHandler() {
registerSystemHandlers(menubar);
const handleCall = handleMock.mock.calls.find((c) => c[0] === EVENTS.UPDATE_KEYBOARD_SHORTCUT);
if (!handleCall) {
throw new Error('UPDATE_KEYBOARD_SHORTCUT handler not registered');
}
return handleCall[1] as (
event: Electron.IpcMainInvokeEvent,
data: { enabled: boolean; keyboardShortcut: string },
) => { success: boolean };
}
describe('registerSystemHandlers', () => {
it('registers handlers without throwing', () => {
expect(() => registerSystemHandlers(menubar)).not.toThrow();
});
it('registers expected system IPC event handlers', () => {
registerSystemHandlers(menubar);
const onEvents = onMock.mock.calls.map((call: unknown[]) => call[0]);
const handleEvents = handleMock.mock.calls.map((call: unknown[]) => call[0]);
expect(onEvents).toContain(EVENTS.OPEN_EXTERNAL);
expect(onEvents).toContain(EVENTS.UPDATE_AUTO_LAUNCH);
expect(onEvents).toContain(EVENTS.UPDATE_KEEP_WINDOW_ON_BLUR);
expect(handleEvents).toContain(EVENTS.UPDATE_KEYBOARD_SHORTCUT);
});
});
describe('UPDATE_KEEP_WINDOW_ON_BLUR', () => {
it('forwards the value to applyKeepWindowOnBlur', () => {
registerSystemHandlers(menubar);
const handler = onMock.mock.calls.find(
(call: unknown[]) => call[0] === EVENTS.UPDATE_KEEP_WINDOW_ON_BLUR,
)?.[1];
handler?.({}, true);
expect(applyKeepWindowOnBlur).toHaveBeenCalledWith(menubar, true);
});
});
describe('UPDATE_KEYBOARD_SHORTCUT', () => {
it('delegates registration to mb.setGlobalShortcut when enabled', () => {
const handler = getKeyboardShortcutHandler();
const result = handler({} as Electron.IpcMainInvokeEvent, {
enabled: true,
keyboardShortcut: 'CommandOrControl+Shift+G',
});
expect(result).toEqual({ success: true });
expect(setGlobalShortcutMock).toHaveBeenCalledWith('CommandOrControl+Shift+G');
});
it('clears the shortcut when disabled', () => {
const handler = getKeyboardShortcutHandler();
handler({} as Electron.IpcMainInvokeEvent, {
enabled: true,
keyboardShortcut: 'CommandOrControl+Shift+A',
});
setGlobalShortcutMock.mockClear();
const result = handler({} as Electron.IpcMainInvokeEvent, {
enabled: false,
keyboardShortcut: 'CommandOrControl+Shift+A',
});
expect(result).toEqual({ success: true });
expect(setGlobalShortcutMock).toHaveBeenCalledWith(undefined);
});
it('returns success false and rolls back to the previous shortcut when the new registration fails', () => {
const handler = getKeyboardShortcutHandler();
handler({} as Electron.IpcMainInvokeEvent, {
enabled: true,
keyboardShortcut: 'CommandOrControl+Shift+A',
});
setGlobalShortcutMock.mockClear();
setGlobalShortcutMock.mockReturnValueOnce(false).mockReturnValue(true);
const result = handler({} as Electron.IpcMainInvokeEvent, {
enabled: true,
keyboardShortcut: 'CommandOrControl+Shift+B',
});
expect(result).toEqual({ success: false });
expect(setGlobalShortcutMock).toHaveBeenNthCalledWith(1, 'CommandOrControl+Shift+B');
expect(setGlobalShortcutMock).toHaveBeenNthCalledWith(2, 'CommandOrControl+Shift+A');
});
});
});