Skip to content

Commit 67a9b5a

Browse files
committed
feat: add log method to ipc api
1 parent ae2d852 commit 67a9b5a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

electron/preload/__tests__/index.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { mockContextBridge, mockIpcRenderer } = vi.hoisted(() => {
77
};
88

99
const mockIpcRenderer = {
10+
send: vi.fn<IpcRenderer['send']>(),
1011
invoke: vi.fn<IpcRenderer['invoke']>(),
1112
on: vi.fn<IpcRenderer['on']>(),
1213
off: vi.fn<IpcRenderer['off']>(),
@@ -51,6 +52,7 @@ describe('index', () => {
5152
'api',
5253
expect.objectContaining({
5354
ping: expect.any(Function),
55+
log: expect.any(Function),
5456
saveAccount: expect.any(Function),
5557
removeAccount: expect.any(Function),
5658
saveCharacter: expect.any(Function),
@@ -71,6 +73,22 @@ describe('index', () => {
7173
mockIpcRenderer.invoke.mockResolvedValueOnce('pong');
7274
const result = await api.ping();
7375
expect(result).toBe('pong');
76+
expect(mockIpcRenderer.invoke).toHaveBeenCalledWith('ping');
77+
});
78+
});
79+
80+
describe('#log', async () => {
81+
it('sends log', async () => {
82+
type LogMessage = Parameters<AppAPI['log']>[0];
83+
const logMessage: LogMessage = {
84+
scope: 'test-scope',
85+
level: 'info',
86+
message: 'test-message',
87+
timestamp: new Date(),
88+
data: { test: 'data' },
89+
};
90+
api.log(logMessage);
91+
expect(mockIpcRenderer.send).toHaveBeenCalledWith('log', logMessage);
7492
});
7593
});
7694

electron/preload/index.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import type { IpcRendererEvent } from 'electron';
44
*/
55
declare const appAPI: {
66
ping: () => Promise<string>;
7+
/**
8+
* Logs a message to the main process.
9+
*/
10+
log: (options: {
11+
scope: string;
12+
level: 'error' | 'warn' | 'info' | 'debug' | 'trace';
13+
message: string;
14+
timestamp: Date;
15+
data?: Record<string, any>;
16+
}) => void;
717
/**
818
* Add or update credentials for a play.net account.
919
*/

electron/preload/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ const appAPI = {
1111
ping: async (): Promise<string> => {
1212
return ipcRenderer.invoke('ping');
1313
},
14+
/**
15+
* Logs a message to the main process.
16+
*/
17+
log: (options: {
18+
scope: string;
19+
level: 'error' | 'warn' | 'info' | 'debug' | 'trace';
20+
message: string;
21+
timestamp: Date;
22+
data?: Record<string, any>;
23+
}): void => {
24+
ipcRenderer.send('log', options);
25+
},
1426
/**
1527
* Add or update credentials for a play.net account.
1628
*/

0 commit comments

Comments
 (0)