-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathevents.test.ts
More file actions
68 lines (51 loc) · 1.8 KB
/
events.test.ts
File metadata and controls
68 lines (51 loc) · 1.8 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
import { EVENTS } from '../shared/events';
const onMock = vi.fn();
const handleMock = vi.fn();
vi.mock('electron', () => ({
ipcMain: {
on: (...args: unknown[]) => onMock(...args),
handle: (...args: unknown[]) => handleMock(...args),
},
}));
import type { Menubar } from 'menubar';
import { handleMainEvent, onMainEvent, sendRendererEvent } from './events';
type MockMenubar = {
window: { webContents: { send: ReturnType<typeof vi.fn> } };
};
describe('main/events', () => {
it('onMainEvent registers ipcMain.on listener', () => {
const listenerMock = vi.fn();
onMainEvent(
EVENTS.WINDOW_SHOW,
listenerMock as unknown as (e: Electron.IpcMainEvent, d: unknown) => void,
);
expect(onMock).toHaveBeenCalledWith(EVENTS.WINDOW_SHOW, listenerMock);
});
it('handleMainEvent registers ipcMain.handle listener', () => {
const listenerMock = vi.fn();
handleMainEvent(
EVENTS.VERSION,
listenerMock as unknown as (
e: Electron.IpcMainInvokeEvent,
d: unknown,
) => void,
);
expect(handleMock).toHaveBeenCalledWith(EVENTS.VERSION, listenerMock);
});
it('sendRendererEvent forwards event to webContents with data', () => {
const sendMock = vi.fn();
const mb: MockMenubar = { window: { webContents: { send: sendMock } } };
sendRendererEvent(
mb as unknown as Menubar,
EVENTS.UPDATE_ICON_TITLE,
'title',
);
expect(sendMock).toHaveBeenCalledWith(EVENTS.UPDATE_ICON_TITLE, 'title');
});
it('sendRendererEvent forwards event without data', () => {
const sendMock = vi.fn();
const mb: MockMenubar = { window: { webContents: { send: sendMock } } };
sendRendererEvent(mb as unknown as Menubar, EVENTS.RESET_APP);
expect(sendMock).toHaveBeenCalledWith(EVENTS.RESET_APP, undefined);
});
});