Skip to content

Commit 068a993

Browse files
committed
refactor simplify
Signed-off-by: Adam Setch <[email protected]>
1 parent 2c332be commit 068a993

File tree

6 files changed

+78
-29
lines changed

6 files changed

+78
-29
lines changed

src/renderer/context/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
173173
useEffect(() => {
174174
setUseUnreadActiveIcon(settings.useUnreadActiveIcon);
175175
setUseAlternateIdleIcon(settings.useAlternateIdleIcon);
176-
setTrayIconColorAndTitle(notifications, settings);
176+
setTrayIconColorAndTitle(unreadCount, settings);
177177
}, [
178178
settings.showNotificationsCountInTray,
179179
settings.useUnreadActiveIcon,

src/renderer/routes/Accounts.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
getAccountUUID,
3535
refreshAccount,
3636
} from '../utils/auth/utils';
37-
import { updateTrayColor, updateTrayTitle } from '../utils/comms';
3837
import { getAuthMethodIcon, getPlatformIcon } from '../utils/icons';
3938
import {
4039
openAccountProfile,
@@ -57,8 +56,6 @@ export const AccountsRoute: FC = () => {
5756
(account: Account) => {
5857
logoutFromAccount(account);
5958
navigate(-1);
60-
updateTrayColor();
61-
updateTrayTitle();
6259
},
6360
[logoutFromAccount],
6461
);

src/renderer/utils/comms.test.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,11 @@ describe('renderer/utils/comms.ts', () => {
147147
expect(window.gitify.tray.updateColor).toHaveBeenCalledWith(5);
148148
});
149149

150-
it('updates tray icon color with default count', () => {
151-
updateTrayColor();
152-
153-
expect(window.gitify.tray.updateColor).toHaveBeenCalledTimes(1);
154-
});
155-
156150
it('updates tray title with provided value', () => {
157151
updateTrayTitle('gitify');
158152

159153
expect(window.gitify.tray.updateTitle).toHaveBeenCalledTimes(1);
160154
expect(window.gitify.tray.updateTitle).toHaveBeenCalledWith('gitify');
161155
});
162-
163-
it('updates tray title with default value', () => {
164-
updateTrayTitle();
165-
166-
expect(window.gitify.tray.updateTitle).toHaveBeenCalledTimes(1);
167-
});
168156
});
169157
});

src/renderer/utils/comms.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,21 @@ export function setKeyboardShortcut(keyboardShortcut: boolean): void {
5757
window.gitify.setKeyboardShortcut(keyboardShortcut);
5858
}
5959

60-
export function updateTrayColor(notificationsLength = 0): void {
60+
/**
61+
* Updates the tray icon color based on the number of unread notifications.
62+
*
63+
* Passing a negative number will set the error state color.
64+
*
65+
* @param notificationsLength The number of unread notifications
66+
*/
67+
export function updateTrayColor(notificationsLength: number): void {
6168
window.gitify.tray.updateColor(notificationsLength);
6269
}
63-
64-
export function updateTrayTitle(title = ''): void {
70+
/**
71+
* Updates the tray icon title.
72+
*
73+
* @param title The title to set on the tray icon
74+
*/
75+
export function updateTrayTitle(title: string): void {
6576
window.gitify.tray.updateTitle(title);
6677
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { mockSettings } from '../../__mocks__/state-mocks';
2+
import * as comms from '../comms';
3+
import { setTrayIconColorAndTitle } from './tray';
4+
5+
describe('renderer/utils/notifications/tray.ts', () => {
6+
const updateTrayColorSpy = jest.spyOn(comms, 'updateTrayColor');
7+
const updateTrayTitleSpy = jest.spyOn(comms, 'updateTrayTitle');
8+
9+
beforeEach(() => {
10+
jest.clearAllMocks();
11+
});
12+
13+
describe('setTrayIconColorAndTitle', () => {
14+
it('should update tray color and title when showNotificationsCountInTray is true and has unread notifications', () => {
15+
const settings = {
16+
...mockSettings,
17+
showNotificationsCountInTray: true,
18+
};
19+
20+
setTrayIconColorAndTitle(5, settings);
21+
22+
expect(updateTrayColorSpy).toHaveBeenCalledTimes(1);
23+
expect(updateTrayColorSpy).toHaveBeenCalledWith(5);
24+
expect(updateTrayTitleSpy).toHaveBeenCalledTimes(1);
25+
expect(updateTrayTitleSpy).toHaveBeenCalledWith('5');
26+
});
27+
28+
it('should update tray color and empty title when showNotificationsCountInTray is false and has unread notifications', () => {
29+
const settings = {
30+
...mockSettings,
31+
showNotificationsCountInTray: false,
32+
};
33+
34+
setTrayIconColorAndTitle(5, settings);
35+
36+
expect(updateTrayColorSpy).toHaveBeenCalledTimes(1);
37+
expect(updateTrayColorSpy).toHaveBeenCalledWith(5);
38+
expect(updateTrayTitleSpy).toHaveBeenCalledTimes(1);
39+
expect(updateTrayTitleSpy).toHaveBeenCalledWith('');
40+
});
41+
42+
it('should update tray with empty title when no unread notifications', () => {
43+
const settings = {
44+
...mockSettings,
45+
showNotificationsCountInTray: true,
46+
};
47+
48+
setTrayIconColorAndTitle(0, settings);
49+
50+
expect(updateTrayColorSpy).toHaveBeenCalledTimes(1);
51+
expect(updateTrayColorSpy).toHaveBeenCalledWith(0);
52+
expect(updateTrayTitleSpy).toHaveBeenCalledTimes(1);
53+
expect(updateTrayTitleSpy).toHaveBeenCalledWith('');
54+
});
55+
});
56+
});
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import type { AccountNotifications, SettingsState } from '../../types';
1+
import type { SettingsState } from '../../types';
22
import { updateTrayColor, updateTrayTitle } from '../comms';
3-
import { getUnreadNotificationCount } from './notifications';
43

54
/**
65
* Sets the tray icon color and title based on the number of unread notifications.
76
*
8-
* @param notifications
9-
* @param settings
7+
* @param unreadNotifications - The number of unread notifications
8+
* @param settings - The application settings
109
*/
1110
export function setTrayIconColorAndTitle(
12-
notifications: AccountNotifications[],
11+
unreadNotifications: number,
1312
settings: SettingsState,
1413
) {
15-
const totalUnreadNotifications = getUnreadNotificationCount(notifications);
16-
1714
let title = '';
18-
if (settings.showNotificationsCountInTray && totalUnreadNotifications > 0) {
19-
title = totalUnreadNotifications.toString();
15+
if (settings.showNotificationsCountInTray && unreadNotifications > 0) {
16+
title = unreadNotifications.toString();
2017
}
2118

22-
updateTrayColor(totalUnreadNotifications);
19+
updateTrayColor(unreadNotifications);
2320
updateTrayTitle(title);
2421
}

0 commit comments

Comments
 (0)