diff --git a/src/context/App.tsx b/src/context/App.tsx index 5bd6ddad0..90cf29a1d 100644 --- a/src/context/App.tsx +++ b/src/context/App.tsx @@ -100,7 +100,7 @@ interface AppContextState { notifications: AccountNotifications[]; status: Status; - errorDetails: GitifyError; + globalError: GitifyError; fetchNotifications: () => Promise; markNotificationRead: (notification: Notification) => Promise; markNotificationDone: (notification: Notification) => Promise; @@ -122,7 +122,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => { const { fetchNotifications, notifications, - errorDetails, + globalError, status, markNotificationRead, markNotificationDone, @@ -311,7 +311,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => { notifications, status, - errorDetails, + globalError, fetchNotifications: fetchNotificationsWithAccounts, markNotificationRead: markNotificationReadWithAccounts, markNotificationDone: markNotificationDoneWithAccounts, diff --git a/src/hooks/useNotifications.test.ts b/src/hooks/useNotifications.test.ts index e4b3af7cc..730364ea7 100644 --- a/src/hooks/useNotifications.test.ts +++ b/src/hooks/useNotifications.test.ts @@ -252,10 +252,10 @@ describe('hooks/useNotifications.ts', () => { expect(result.current.notifications[0].notifications.length).toBe(6); }); - it('should fetch notifications with failures', async () => { + it('should fetch notifications with same failures', async () => { const code = AxiosError.ERR_BAD_REQUEST; - const status = 400; - const message = 'Oops! Something went wrong.'; + const status = 401; + const message = 'Bad credentials'; nock('https://api.github.com/') .get('/notifications?participating=false') @@ -293,7 +293,49 @@ describe('hooks/useNotifications.ts', () => { expect(result.current.status).toBe('error'); }); - expect(result.current.errorDetails).toBe(Errors.UNKNOWN); + expect(result.current.globalError).toBe(Errors.BAD_CREDENTIALS); + }); + + it('should fetch notifications with different failures', async () => { + const code = AxiosError.ERR_BAD_REQUEST; + + nock('https://api.github.com/') + .get('/notifications?participating=false') + .replyWithError({ + code, + response: { + status: 400, + data: { + message: 'Oops! Something went wrong.', + }, + }, + }); + + nock('https://github.gitify.io/api/v3/') + .get('/notifications?participating=false') + .replyWithError({ + code, + response: { + status: 401, + data: { + message: 'Bad credentials', + }, + }, + }); + + const { result } = renderHook(() => useNotifications()); + + act(() => { + result.current.fetchNotifications(mockState); + }); + + expect(result.current.status).toBe('loading'); + + await waitFor(() => { + expect(result.current.status).toBe('error'); + }); + + expect(result.current.globalError).toBeNull(); }); }); diff --git a/src/hooks/useNotifications.ts b/src/hooks/useNotifications.ts index 89579536a..76415d384 100644 --- a/src/hooks/useNotifications.ts +++ b/src/hooks/useNotifications.ts @@ -45,7 +45,7 @@ interface NotificationsState { notification: Notification, ) => Promise; status: Status; - errorDetails: GitifyError; + globalError: GitifyError; } export const useNotifications = (): NotificationsState => { @@ -75,9 +75,9 @@ export const useNotifications = (): NotificationsState => { } } - if (allAccountsHaveErrors && accountErrorsAreAllSame) { + if (allAccountsHaveErrors) { setStatus('error'); - setGlobalError(accountError); + setGlobalError(accountErrorsAreAllSame ? accountError : null); return; } @@ -236,7 +236,7 @@ export const useNotifications = (): NotificationsState => { return { status, - errorDetails: globalError, + globalError, notifications, fetchNotifications, diff --git a/src/routes/Notifications.test.tsx b/src/routes/Notifications.test.tsx index 6fb0edc39..207083b45 100644 --- a/src/routes/Notifications.test.tsx +++ b/src/routes/Notifications.test.tsx @@ -58,7 +58,7 @@ describe('routes/Notifications.tsx', () => { value={{ notifications: [], status: 'error', - errorDetails: Errors.BAD_CREDENTIALS, + globalError: Errors.BAD_CREDENTIALS, }} > @@ -74,7 +74,7 @@ describe('routes/Notifications.tsx', () => { value={{ notifications: [], status: 'error', - errorDetails: Errors.MISSING_SCOPES, + globalError: Errors.MISSING_SCOPES, }} > @@ -90,7 +90,7 @@ describe('routes/Notifications.tsx', () => { value={{ notifications: [], status: 'error', - errorDetails: Errors.RATE_LIMITED, + globalError: Errors.RATE_LIMITED, }} > @@ -106,7 +106,7 @@ describe('routes/Notifications.tsx', () => { value={{ notifications: [], status: 'error', - errorDetails: Errors.UNKNOWN, + globalError: Errors.UNKNOWN, }} > @@ -122,7 +122,7 @@ describe('routes/Notifications.tsx', () => { value={{ notifications: [], status: 'error', - errorDetails: null, + globalError: null, }} > diff --git a/src/routes/Notifications.tsx b/src/routes/Notifications.tsx index 5e98705d0..17543e543 100644 --- a/src/routes/Notifications.tsx +++ b/src/routes/Notifications.tsx @@ -4,20 +4,18 @@ import { AllRead } from '../components/AllRead'; import { Oops } from '../components/Oops'; import { AppContext } from '../context/App'; import { getAccountUUID } from '../utils/auth/utils'; -import { Errors } from '../utils/constants'; import { getNotificationCount } from '../utils/notifications'; export const NotificationsRoute: FC = () => { - const { notifications, status, errorDetails, settings } = - useContext(AppContext); + const { notifications, globalError, settings } = useContext(AppContext); const hasMultipleAccounts = useMemo( () => notifications.length > 1, [notifications], ); - const hasAccountError = useMemo( - () => notifications.some((account) => account.error !== null), + const hasNoAccountErrors = useMemo( + () => notifications.every((account) => account.error === null), [notifications], ); @@ -26,11 +24,11 @@ export const NotificationsRoute: FC = () => { [notifications], ); - if (status === 'error') { - return ; + if (globalError) { + return ; } - if (!hasNotifications && !hasAccountError) { + if (!hasNotifications && hasNoAccountErrors) { return ; } diff --git a/src/routes/__snapshots__/Notifications.test.tsx.snap b/src/routes/__snapshots__/Notifications.test.tsx.snap index 8bb99d300..5dbd73147 100644 --- a/src/routes/__snapshots__/Notifications.test.tsx.snap +++ b/src/routes/__snapshots__/Notifications.test.tsx.snap @@ -144,13 +144,13 @@ exports[`routes/Notifications.tsx should render itself & its children (error con "baseElement":

- Oops + AllRead

, "container":

- Oops + AllRead

, "debug": [Function],