-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathtray.test.ts
More file actions
108 lines (83 loc) · 3 KB
/
tray.test.ts
File metadata and controls
108 lines (83 loc) · 3 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
import type { Menubar } from 'menubar';
import { EVENTS } from '../../shared/events';
import { TrayIcons } from '../icons';
import { registerTrayHandlers } from './tray';
const onMock = vi.fn();
vi.mock('electron', () => ({
ipcMain: {
on: (...args: unknown[]) => onMock(...args),
},
net: {
isOnline: vi.fn().mockReturnValue(true),
},
}));
describe('main/handlers/tray.ts', () => {
let menubar: Menubar;
beforeEach(() => {
menubar = {
tray: {
isDestroyed: vi.fn().mockReturnValue(false),
setImage: vi.fn(),
setTitle: vi.fn(),
},
} as unknown as Menubar;
});
describe('registerTrayHandlers', () => {
it('registers handlers without throwing', () => {
expect(() => registerTrayHandlers(menubar)).not.toThrow();
});
it('registers expected tray IPC event handlers', () => {
registerTrayHandlers(menubar);
const registeredEvents = onMock.mock.calls.map(
(call: unknown[]) => call[0],
);
expect(registeredEvents).toContain(EVENTS.USE_ALTERNATE_IDLE_ICON);
expect(registeredEvents).toContain(EVENTS.USE_UNREAD_ACTIVE_ICON);
expect(registeredEvents).toContain(EVENTS.UPDATE_ICON_COLOR);
expect(registeredEvents).toContain(EVENTS.UPDATE_ICON_TITLE);
});
});
it('skips tray updates when tray is destroyed', () => {
(menubar.tray.isDestroyed as ReturnType<typeof vi.fn>).mockReturnValue(
true,
);
registerTrayHandlers(menubar);
const updateColorHandler = onMock.mock.calls.find(
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
)?.[1];
updateColorHandler?.({}, 5);
expect(menubar.tray.setImage).not.toHaveBeenCalled();
});
it('sets idle icon when notifications count is 0', () => {
registerTrayHandlers(menubar);
const updateColorHandler = onMock.mock.calls.find(
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
)?.[1];
updateColorHandler?.({}, 0);
expect(menubar.tray.setImage).toHaveBeenCalledWith(TrayIcons.idle);
});
it('sets active icon when notifications count is positive', () => {
registerTrayHandlers(menubar);
const updateColorHandler = onMock.mock.calls.find(
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
)?.[1];
updateColorHandler?.({}, 3);
expect(menubar.tray.setImage).toHaveBeenCalledWith(TrayIcons.active);
});
it('sets error icon when notifications count is negative', () => {
registerTrayHandlers(menubar);
const updateColorHandler = onMock.mock.calls.find(
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_COLOR,
)?.[1];
updateColorHandler?.({}, -1);
expect(menubar.tray.setImage).toHaveBeenCalledWith(TrayIcons.error);
});
it('updates tray title', () => {
registerTrayHandlers(menubar);
const updateTitleHandler = onMock.mock.calls.find(
(call: unknown[]) => call[0] === EVENTS.UPDATE_ICON_TITLE,
)?.[1];
updateTitleHandler?.({}, '5');
expect(menubar.tray.setTitle).toHaveBeenCalledWith('5');
});
});