Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): use conditional requests and fetch all inbox notifications #1414

Merged
merged 18 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__mocks__/state-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const mockAppearanceSettings = {

const mockNotificationSettings = {
groupBy: GroupBy.REPOSITORY,
fetchAllNotifications: true,
participating: false,
markAsDoneOnOpen: false,
markAsDoneOnUnsubscribe: false,
Expand Down
26 changes: 26 additions & 0 deletions src/components/settings/NotificationSettings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ describe('routes/components/settings/NotificationSettings.tsx', () => {
expect(updateSetting).toHaveBeenCalledTimes(1);
expect(updateSetting).toHaveBeenCalledWith('groupBy', 'DATE');
});

it('should toggle the fetchAllNotifications checkbox', async () => {
await act(async () => {
render(
<AppContext.Provider
value={{
auth: mockAuth,
settings: mockSettings,
updateSetting,
}}
>
<MemoryRouter>
<NotificationSettings />
</MemoryRouter>
</AppContext.Provider>,
);
});

fireEvent.click(screen.getByLabelText('Fetch all notifications'), {
target: { checked: true },
});

expect(updateSetting).toHaveBeenCalledTimes(1);
expect(updateSetting).toHaveBeenCalledWith('fetchAllNotifications', false);
});

it('should toggle the showOnlyParticipating checkbox', async () => {
await act(async () => {
render(
Expand Down
20 changes: 20 additions & 0 deletions src/components/settings/NotificationSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ export const NotificationSettings: FC = () => {
updateSetting('groupBy', evt.target.value as GroupBy);
}}
/>
<Checkbox
name="fetchAllNotifications"
label="Fetch all notifications"
checked={settings.fetchAllNotifications}
onChange={(evt) =>
updateSetting('fetchAllNotifications', evt.target.checked)
}
tooltip={
<div>
<div className="pb-3">
When <em>checked</em>, Gitify will fetch <strong>all</strong>{' '}
notifications from your inbox.
</div>
<div>
When <em>unhecked</em>, Gitify will only fetch the first page of
setchy marked this conversation as resolved.
Show resolved Hide resolved
notifications (max 50 records pet GitHub account)
setchy marked this conversation as resolved.
Show resolved Hide resolved
</div>
</div>
}
/>
<Checkbox
name="showOnlyParticipating"
label="Show only participating"
Expand Down
1 change: 1 addition & 0 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const defaultAppearanceSettings = {

const defaultNotificationSettings = {
groupBy: GroupBy.REPOSITORY,
fetchAllNotifications: true,
participating: false,
markAsDoneOnOpen: false,
markAsDoneOnUnsubscribe: false,
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe('hooks/useNotifications.ts', () => {
});

expect(result.current.globalError).toBe(Errors.BAD_CREDENTIALS);
expect(logErrorSpy).toHaveBeenCalledTimes(2);
expect(logErrorSpy).toHaveBeenCalledTimes(4);
});

it('should fetch notifications with different failures', async () => {
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('hooks/useNotifications.ts', () => {
});

expect(result.current.globalError).toBeNull();
expect(logErrorSpy).toHaveBeenCalledTimes(2);
expect(logErrorSpy).toHaveBeenCalledTimes(4);
});
});

Expand Down
44 changes: 44 additions & 0 deletions src/routes/__snapshots__/Settings.test.tsx.snap

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

1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ interface AppearanceSettingsState {

interface NotificationSettingsState {
groupBy: GroupBy;
fetchAllNotifications: boolean;
participating: boolean;
markAsDoneOnOpen: boolean;
markAsDoneOnUnsubscribe: boolean;
Expand Down
33 changes: 21 additions & 12 deletions src/utils/api/__snapshots__/client.test.ts.snap

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

4 changes: 2 additions & 2 deletions src/utils/api/__snapshots__/request.test.ts.snap

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

33 changes: 29 additions & 4 deletions src/utils/api/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,32 @@ describe('utils/api/client.ts', () => {
});

describe('listNotificationsForAuthenticatedUser', () => {
const mockSettings: Partial<SettingsState> = {
participating: true,
};
it('should list notifications for user - github cloud - fetchAllNotifications true', async () => {
const mockSettings: Partial<SettingsState> = {
participating: true,
fetchAllNotifications: true,
};

await listNotificationsForAuthenticatedUser(
mockGitHubCloudAccount,
mockSettings as SettingsState,
);

expect(axios).toHaveBeenCalledWith({
url: 'https://api.github.com/notifications?participating=true',
method: 'GET',
data: {},
});

expect(axios.defaults.headers.common).toMatchSnapshot();
});

it('should list notifications for user - github cloud - fetchAllNotifications false', async () => {
const mockSettings: Partial<SettingsState> = {
participating: true,
fetchAllNotifications: false,
};

it('should list notifications for user - github cloud', async () => {
await listNotificationsForAuthenticatedUser(
mockGitHubCloudAccount,
mockSettings as SettingsState,
Expand All @@ -103,6 +124,10 @@ describe('utils/api/client.ts', () => {
});

it('should list notifications for user - github enterprise server', async () => {
const mockSettings: Partial<SettingsState> = {
participating: true,
};

await listNotificationsForAuthenticatedUser(
mockGitHubEnterpriseServerAccount,
mockSettings as SettingsState,
Expand Down
9 changes: 7 additions & 2 deletions src/utils/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ export function listNotificationsForAuthenticatedUser(
const url = getGitHubAPIBaseUrl(account.hostname);
url.pathname += 'notifications';
url.searchParams.append('participating', String(settings.participating));

return apiRequestAuth(url.toString() as Link, 'GET', account.token);
return apiRequestAuth(
url.toString() as Link,
'GET',
account.token,
{},
settings.fetchAllNotifications,
);
}

/**
Expand Down
Loading