Skip to content

Commit eb6e10d

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

File tree

8 files changed

+117
-24
lines changed

8 files changed

+117
-24
lines changed

src/renderer/__mocks__/notifications-mocks.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AccountNotifications } from '../types';
1+
import type { AccountNotifications, GitifyError } from '../types';
22
import type { StateType, Subject, SubjectType } from '../typesGitHub';
33
import {
44
mockEnterpriseNotifications,
@@ -44,3 +44,11 @@ export function createSubjectMock(mocks: {
4444
latest_comment_url: null,
4545
};
4646
}
47+
48+
export function mockAccountWithError(error: GitifyError): AccountNotifications {
49+
return {
50+
account: mockGitHubCloudAccount,
51+
notifications: [],
52+
error,
53+
};
54+
}

src/renderer/context/App.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { mockSingleNotification } from '../utils/api/__mocks__/response-mocks';
99
import * as apiRequests from '../utils/api/request';
1010
import * as comms from '../utils/comms';
1111
import * as notifications from '../utils/notifications/notifications';
12-
import * as tray from '../utils/notifications/tray';
1312
import * as storage from '../utils/storage';
13+
import * as tray from '../utils/tray';
1414
import { AppContext, AppProvider } from './App';
1515
import { defaultSettings } from './defaults';
1616

src/renderer/context/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ import {
4949
setUseUnreadActiveIcon,
5050
} from '../utils/comms';
5151
import { getUnreadNotificationCount } from '../utils/notifications/notifications';
52-
import { setTrayIconColorAndTitle } from '../utils/notifications/tray';
5352
import { clearState, loadState, saveState } from '../utils/storage';
5453
import {
5554
DEFAULT_DAY_COLOR_SCHEME,
5655
DEFAULT_NIGHT_COLOR_SCHEME,
5756
mapThemeModeToColorMode,
5857
mapThemeModeToColorScheme,
5958
} from '../utils/theme';
59+
import { setTrayIconColorAndTitle } from '../utils/tray';
6060
import { zoomPercentageToLevel } from '../utils/zoom';
6161
import { defaultAuth, defaultFilters, defaultSettings } from './defaults';
6262

src/renderer/hooks/useNotifications.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import {
1414
markNotificationThreadAsRead,
1515
} from '../utils/api/client';
1616
import { updateTrayColor } from '../utils/comms';
17+
import {
18+
areAllAccountErrorsSame,
19+
doesAllAccountsHaveErrors,
20+
} from '../utils/errors';
1721
import { isMarkAsDoneFeatureSupported } from '../utils/features';
1822
import { rendererLogError } from '../utils/logger';
1923
import { raiseNativeNotification } from '../utils/notifications/native';
@@ -78,24 +82,14 @@ export const useNotifications = (): NotificationsState => {
7882

7983
// Set Global Error if all accounts have the same error
8084
const allAccountsHaveErrors =
81-
fetchedNotifications.length > 0 &&
82-
fetchedNotifications.every((account) => {
83-
return account.error !== null;
84-
});
85-
86-
let accountErrorsAreAllSame = true;
87-
const accountError = fetchedNotifications[0]?.error;
88-
89-
for (const fetchedNotification of fetchedNotifications) {
90-
if (accountError !== fetchedNotification.error) {
91-
accountErrorsAreAllSame = false;
92-
break;
93-
}
94-
}
85+
doesAllAccountsHaveErrors(fetchedNotifications);
86+
const allAccountErrorsAreSame =
87+
areAllAccountErrorsSame(fetchedNotifications);
9588

9689
if (allAccountsHaveErrors) {
90+
const accountError = fetchedNotifications[0].error;
9791
setStatus('error');
98-
setGlobalError(accountErrorsAreAllSame ? accountError : null);
92+
setGlobalError(allAccountErrorsAreSame ? accountError : null);
9993
updateTrayColor(-1);
10094
return;
10195
}

src/renderer/utils/errors.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { mockAccountWithError } from '../__mocks__/notifications-mocks';
2+
import type { AccountNotifications } from '../types';
3+
import {
4+
areAllAccountErrorsSame,
5+
doesAllAccountsHaveErrors,
6+
Errors,
7+
} from './errors';
8+
9+
describe('renderer/utils/errors.ts', () => {
10+
describe('doesAllAccountsHaveErrors', () => {
11+
it('returns false for empty list', () => {
12+
expect(doesAllAccountsHaveErrors([])).toBe(false);
13+
});
14+
15+
it('returns false when some accounts have no error', () => {
16+
const items: AccountNotifications[] = [
17+
mockAccountWithError(Errors.NETWORK),
18+
mockAccountWithError(null),
19+
];
20+
21+
expect(doesAllAccountsHaveErrors(items)).toBe(false);
22+
});
23+
24+
it('returns true when every account has an error', () => {
25+
const items: AccountNotifications[] = [
26+
mockAccountWithError(Errors.NETWORK),
27+
mockAccountWithError(Errors.RATE_LIMITED),
28+
];
29+
30+
expect(doesAllAccountsHaveErrors(items)).toBe(true);
31+
});
32+
});
33+
34+
describe('areAllAccountErrorsSame', () => {
35+
it('returns true for empty list', () => {
36+
expect(areAllAccountErrorsSame([])).toBe(true);
37+
});
38+
39+
it('returns true when all errors are identical object reference', () => {
40+
const err = Errors.NETWORK;
41+
const items: AccountNotifications[] = [
42+
mockAccountWithError(err),
43+
mockAccountWithError(err),
44+
];
45+
46+
expect(areAllAccountErrorsSame(items)).toBe(true);
47+
});
48+
49+
it('returns false when errors differ', () => {
50+
const items: AccountNotifications[] = [
51+
mockAccountWithError(Errors.NETWORK),
52+
mockAccountWithError(Errors.RATE_LIMITED),
53+
];
54+
55+
expect(areAllAccountErrorsSame(items)).toBe(false);
56+
});
57+
58+
it('returns false when one account has null error', () => {
59+
const items: AccountNotifications[] = [
60+
mockAccountWithError(Errors.NETWORK),
61+
mockAccountWithError(null),
62+
];
63+
64+
expect(areAllAccountErrorsSame(items)).toBe(false);
65+
});
66+
});
67+
});

src/renderer/utils/errors.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ErrorType, GitifyError } from '../types';
1+
import type { AccountNotifications, ErrorType, GitifyError } from '../types';
22

33
export const Errors: Record<ErrorType, GitifyError> = {
44
BAD_CREDENTIALS: {
@@ -30,3 +30,27 @@ export const Errors: Record<ErrorType, GitifyError> = {
3030
emojis: ['🤔', '🥲', '😳', '🫠', '🙃', '🙈'],
3131
},
3232
};
33+
34+
/**
35+
* Check if all accounts have errors
36+
*/
37+
export function doesAllAccountsHaveErrors(
38+
notifications: AccountNotifications[],
39+
) {
40+
return (
41+
notifications.length > 0 &&
42+
notifications.every((account) => account.error !== null)
43+
);
44+
}
45+
46+
/**
47+
* Check if all account errors are the same
48+
*/
49+
export function areAllAccountErrorsSame(notifications: AccountNotifications[]) {
50+
if (notifications.length === 0) {
51+
return true;
52+
}
53+
54+
const firstError = notifications[0].error;
55+
return notifications.every((account) => account.error === firstError);
56+
}

src/renderer/utils/notifications/tray.test.ts renamed to src/renderer/utils/tray.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { mockSettings } from '../../__mocks__/state-mocks';
2-
import * as comms from '../comms';
1+
import { mockSettings } from '../__mocks__/state-mocks';
2+
import * as comms from './comms';
33
import { setTrayIconColorAndTitle } from './tray';
44

5-
describe('renderer/utils/notifications/tray.ts', () => {
5+
describe('renderer/utils/tray.ts', () => {
66
const updateTrayColorSpy = jest.spyOn(comms, 'updateTrayColor');
77
const updateTrayTitleSpy = jest.spyOn(comms, 'updateTrayTitle');
88

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { SettingsState } from '../../types';
2-
import { updateTrayColor, updateTrayTitle } from '../comms';
1+
import type { SettingsState } from '../types';
2+
import { updateTrayColor, updateTrayTitle } from './comms';
33

44
/**
55
* Sets the tray icon color and title based on the number of unread notifications.

0 commit comments

Comments
 (0)