-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathindex.test.ts
More file actions
185 lines (150 loc) · 5.06 KB
/
index.test.ts
File metadata and controls
185 lines (150 loc) · 5.06 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
import { EVENTS } from '../shared/events';
import { api } from './index';
// Mocks shared modules used inside preload
const {
sendMainEventMock,
invokeMainEventMock,
onRendererEventMock,
logErrorMock,
exposeInMainWorldMock,
getZoomLevelMock,
setZoomLevelMock,
} = vi.hoisted(() => ({
sendMainEventMock: vi.fn(),
invokeMainEventMock: vi.fn(),
onRendererEventMock: vi.fn(),
logErrorMock: vi.fn(),
exposeInMainWorldMock: vi.fn(),
getZoomLevelMock: vi.fn(() => 1),
setZoomLevelMock: vi.fn((_level: number) => undefined),
}));
vi.mock('./utils', () => ({
sendMainEvent: (...args: unknown[]) => sendMainEventMock(...args),
invokeMainEvent: (...args: unknown[]) => invokeMainEventMock(...args),
invokeMainEventWithData: (...args: unknown[]) => invokeMainEventMock(...args),
onRendererEvent: (...args: unknown[]) => onRendererEventMock(...args),
}));
vi.mock('../shared/logger', () => ({
logError: (...args: unknown[]) => logErrorMock(...args),
}));
vi.mock('electron', () => ({
contextBridge: {
exposeInMainWorld: (key: string, value: unknown) =>
exposeInMainWorldMock(key, value),
},
webFrame: {
getZoomLevel: () => getZoomLevelMock(),
setZoomLevel: (level: number) => setZoomLevelMock(level),
},
}));
// Simple Notification stub
class MockNotification {
static instances: MockNotification[] = [];
public onclick: (() => void) | null = null;
constructor(
public title: string,
public options: { body: string; silent: boolean },
) {
MockNotification.instances.push(this);
}
triggerClick() {
this.onclick?.();
}
}
// Attach to global before importing preload
(global as unknown as { Notification: unknown }).Notification =
MockNotification;
interface TestApi {
tray: { updateColor: (n?: number) => void };
openExternalLink: (u: string, f: boolean) => void;
app: { version: () => Promise<string>; show?: () => void; hide?: () => void };
onSystemThemeUpdate: (cb: (t: string) => void) => void;
raiseNativeNotification: (t: string, b: string, u?: string) => unknown;
}
describe('preload/index', () => {
beforeEach(() => {
MockNotification.instances = [];
exposeInMainWorldMock('gitify', api);
});
const getExposedApi = (): TestApi => {
// API is always exposed via contextBridge
const [, api] = exposeInMainWorldMock.mock.calls[0];
return api as TestApi;
};
it('exposes api via contextBridge', async () => {
expect(exposeInMainWorldMock).toHaveBeenCalledTimes(1);
const [key, api] = exposeInMainWorldMock.mock.calls[0];
expect(key).toBe('gitify');
expect(api).toHaveProperty('openExternalLink');
expect(api).toHaveProperty('app');
expect(api).toHaveProperty('tray');
});
it('tray.updateColor sends correct events', async () => {
const api = getExposedApi();
api.tray.updateColor(-1);
expect(sendMainEventMock).toHaveBeenNthCalledWith(
1,
EVENTS.UPDATE_ICON_COLOR,
-1,
);
});
it('openExternalLink sends event with payload', async () => {
const api = getExposedApi();
api.openExternalLink('https://example.com', true);
expect(sendMainEventMock).toHaveBeenCalledWith(EVENTS.OPEN_EXTERNAL, {
url: 'https://example.com',
activate: true,
});
});
it('app.version returns dev in development', async () => {
const originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
const api = getExposedApi();
await expect(api.app.version()).resolves.toBe('dev');
process.env.NODE_ENV = originalEnv;
});
it('app.version prefixes production version', async () => {
const originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
invokeMainEventMock.mockResolvedValueOnce('1.2.3');
const api = getExposedApi();
await expect(api.app.version()).resolves.toBe('v1.2.3');
process.env.NODE_ENV = originalEnv;
});
it('onSystemThemeUpdate registers listener', async () => {
const api = getExposedApi();
const callback = vi.fn();
api.onSystemThemeUpdate(callback);
expect(onRendererEventMock).toHaveBeenCalledWith(
EVENTS.UPDATE_THEME,
expect.any(Function),
);
// Simulate event
const listener = onRendererEventMock.mock.calls[0][1];
listener({}, 'dark');
expect(callback).toHaveBeenCalledWith('dark');
});
it('raiseNativeNotification without url calls app.show', async () => {
const api = getExposedApi();
const notification = api.raiseNativeNotification(
'Title',
'Body',
) as MockNotification;
notification.triggerClick();
expect(sendMainEventMock).toHaveBeenCalledWith(EVENTS.WINDOW_SHOW);
});
it('raiseNativeNotification with url hides app then opens link', async () => {
const api = getExposedApi();
const notification = api.raiseNativeNotification(
'Title',
'Body',
'https://x',
) as MockNotification;
notification.triggerClick();
expect(sendMainEventMock).toHaveBeenCalledWith(EVENTS.WINDOW_HIDE);
expect(sendMainEventMock).toHaveBeenCalledWith(EVENTS.OPEN_EXTERNAL, {
url: 'https://x',
activate: true,
});
});
});