-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathutils.test.ts
More file actions
66 lines (60 loc) · 1.89 KB
/
utils.test.ts
File metadata and controls
66 lines (60 loc) · 1.89 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
import { EVENTS } from '../shared/events';
import { invokeMainEvent, onRendererEvent, sendMainEvent } from './utils';
vi.mock('electron', () => {
type Listener = (event: unknown, ...args: unknown[]) => void;
const listeners: Record<string, Listener[]> = {};
return {
ipcRenderer: {
send: vi.fn(),
invoke: vi.fn().mockResolvedValue('response'),
on: vi.fn((channel: string, listener: Listener) => {
if (!listeners[channel]) {
listeners[channel] = [];
}
listeners[channel].push(listener);
}),
__emit: (channel: string, ...args: unknown[]) => {
const list = listeners[channel] || [];
for (const l of list) {
l({}, ...args);
}
},
__listeners: listeners,
},
};
});
import { ipcRenderer } from 'electron';
describe('preload/utils', () => {
it('sendMainEvent forwards to ipcRenderer.send', () => {
sendMainEvent(EVENTS.WINDOW_SHOW);
expect(ipcRenderer.send).toHaveBeenCalledWith(
EVENTS.WINDOW_SHOW,
undefined,
);
});
it('invokeMainEvent forwards and resolves', async () => {
const result = await invokeMainEvent(EVENTS.VERSION, 'data');
expect(ipcRenderer.invoke).toHaveBeenCalledWith(EVENTS.VERSION, 'data');
expect(result).toBe('response');
});
it('onRendererEvent registers listener and receives emitted data', () => {
const handlerMock = vi.fn();
onRendererEvent(
EVENTS.UPDATE_ICON_TITLE,
handlerMock as unknown as (
e: Electron.IpcRendererEvent,
args: string,
) => void,
);
(
ipcRenderer as unknown as {
__emit: (channel: string, ...a: unknown[]) => void;
}
).__emit(EVENTS.UPDATE_ICON_TITLE, 'payload');
expect(ipcRenderer.on).toHaveBeenCalledWith(
EVENTS.UPDATE_ICON_TITLE,
handlerMock,
);
expect(handlerMock).toHaveBeenCalledWith({}, 'payload');
});
});