Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jul 29, 2024
1 parent bfbe9f2 commit 4abdb76
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ interface AppContextState {

notifications: AccountNotifications[];
status: Status;
errorDetails: GitifyError;
globalError: GitifyError;
fetchNotifications: () => Promise<void>;
markNotificationRead: (notification: Notification) => Promise<void>;
markNotificationDone: (notification: Notification) => Promise<void>;
Expand All @@ -122,7 +122,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
const {
fetchNotifications,
notifications,
errorDetails,
globalError,
status,
markNotificationRead,
markNotificationDone,
Expand Down Expand Up @@ -311,7 +311,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {

notifications,
status,
errorDetails,
globalError,
fetchNotifications: fetchNotificationsWithAccounts,
markNotificationRead: markNotificationReadWithAccounts,
markNotificationDone: markNotificationDoneWithAccounts,
Expand Down
50 changes: 46 additions & 4 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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();
});
});

Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface NotificationsState {
notification: Notification,
) => Promise<void>;
status: Status;
errorDetails: GitifyError;
globalError: GitifyError;
}

export const useNotifications = (): NotificationsState => {
Expand Down Expand Up @@ -75,9 +75,9 @@ export const useNotifications = (): NotificationsState => {
}
}

if (allAccountsHaveErrors && accountErrorsAreAllSame) {
if (allAccountsHaveErrors) {
setStatus('error');
setGlobalError(accountError);
setGlobalError(accountErrorsAreAllSame ? accountError : null);
return;
}

Expand Down Expand Up @@ -236,7 +236,7 @@ export const useNotifications = (): NotificationsState => {

return {
status,
errorDetails: globalError,
globalError,
notifications,

fetchNotifications,
Expand Down
10 changes: 5 additions & 5 deletions src/routes/Notifications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('routes/Notifications.tsx', () => {
value={{
notifications: [],
status: 'error',
errorDetails: Errors.BAD_CREDENTIALS,
globalError: Errors.BAD_CREDENTIALS,
}}
>
<NotificationsRoute />
Expand All @@ -74,7 +74,7 @@ describe('routes/Notifications.tsx', () => {
value={{
notifications: [],
status: 'error',
errorDetails: Errors.MISSING_SCOPES,
globalError: Errors.MISSING_SCOPES,
}}
>
<NotificationsRoute />
Expand All @@ -90,7 +90,7 @@ describe('routes/Notifications.tsx', () => {
value={{
notifications: [],
status: 'error',
errorDetails: Errors.RATE_LIMITED,
globalError: Errors.RATE_LIMITED,
}}
>
<NotificationsRoute />
Expand All @@ -106,7 +106,7 @@ describe('routes/Notifications.tsx', () => {
value={{
notifications: [],
status: 'error',
errorDetails: Errors.UNKNOWN,
globalError: Errors.UNKNOWN,
}}
>
<NotificationsRoute />
Expand All @@ -122,7 +122,7 @@ describe('routes/Notifications.tsx', () => {
value={{
notifications: [],
status: 'error',
errorDetails: null,
globalError: null,
}}
>
<NotificationsRoute />
Expand Down
14 changes: 6 additions & 8 deletions src/routes/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);

Expand All @@ -26,11 +24,11 @@ export const NotificationsRoute: FC = () => {
[notifications],
);

if (status === 'error') {
return <Oops error={errorDetails ?? Errors.UNKNOWN} />;
if (globalError) {
return <Oops error={globalError} />;
}

if (!hasNotifications && !hasAccountError) {
if (!hasNotifications && hasNoAccountErrors) {
return <AllRead />;
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/__snapshots__/Notifications.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4abdb76

Please sign in to comment.