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: mark as done on unsubscribe #1498

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -87,6 +87,7 @@ const mockNotificationSettings = {
groupBy: GroupBy.REPOSITORY,
participating: false,
markAsDoneOnOpen: false,
markAsDoneOnUnsubscribe: false,
delayNotificationState: false,
};

Expand Down
28 changes: 28 additions & 0 deletions src/components/settings/NotificationSettings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@ describe('routes/components/settings/NotificationSettings.tsx', () => {
expect(updateSetting).toHaveBeenCalledWith('markAsDoneOnOpen', false);
});

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

fireEvent.click(screen.getByLabelText('Mark as done on unsubscribe'), {
target: { checked: true },
});

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

it('should toggle the delayNotificationState checkbox', async () => {
await act(async () => {
render(
Expand Down
14 changes: 14 additions & 0 deletions src/components/settings/NotificationSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ export const NotificationSettings: FC = () => {
</div>
}
/>
<Checkbox
name="markAsDoneOnUnsubscribe"
label="Mark as done on unsubscribe"
checked={settings.markAsDoneOnUnsubscribe}
onChange={(evt) =>
updateSetting('markAsDoneOnUnsubscribe', evt.target.checked)
}
tooltip={
<div>
<strong>Mark as Done</strong> feature is supported in GitHub Cloud
and GitHub Enterprise Server 3.13 or later.
</div>
}
/>
<Checkbox
name="delayNotificationState"
label="Delay notification state"
Expand Down
2 changes: 2 additions & 0 deletions src/context/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ describe('context/App.tsx', () => {
theme: 'SYSTEM',
detailedNotifications: true,
markAsDoneOnOpen: false,
markAsDoneOnUnsubscribe: false,
showAccountHeader: false,
delayNotificationState: false,
showPills: true,
Expand Down Expand Up @@ -411,6 +412,7 @@ describe('context/App.tsx', () => {
theme: 'SYSTEM',
detailedNotifications: true,
markAsDoneOnOpen: false,
markAsDoneOnUnsubscribe: false,
showAccountHeader: false,
delayNotificationState: false,
showPills: true,
Expand Down
1 change: 1 addition & 0 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const defaultNotificationSettings = {
groupBy: GroupBy.REPOSITORY,
participating: false,
markAsDoneOnOpen: false,
markAsDoneOnUnsubscribe: false,
delayNotificationState: false,
};

Expand Down
35 changes: 34 additions & 1 deletion src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ describe('hooks/useNotifications.ts', () => {
describe('unsubscribeNotification', () => {
const id = 'notification-123';

it('should unsubscribe from a notification with success', async () => {
it('should unsubscribe from a notification with success - markAsDoneOnUnsubscribe = false', async () => {
// The unsubscribe endpoint call.
nock('https://api.github.com/')
.put(`/notifications/threads/${id}/subscription`)
Expand All @@ -453,6 +453,39 @@ describe('hooks/useNotifications.ts', () => {
expect(result.current.notifications.length).toBe(0);
});

it('should unsubscribe from a notification with success - markAsDoneOnUnsubscribe = true', async () => {
// The unsubscribe endpoint call.
nock('https://api.github.com/')
.put(`/notifications/threads/${id}/subscription`)
.reply(200);

// The mark done endpoint call.
nock('https://api.github.com/')
.delete(`/notifications/threads/${id}`)
.reply(200);

const { result } = renderHook(() => useNotifications());

act(() => {
result.current.unsubscribeNotification(
{
...mockState,
settings: {
...mockState.settings,
markAsDoneOnUnsubscribe: true,
},
},
mockSingleNotification,
);
});

await waitFor(() => {
expect(result.current.status).toBe('success');
});

expect(result.current.notifications.length).toBe(0);
});

it('should unsubscribe from a notification with failure', async () => {
// The unsubscribe endpoint call.
nock('https://api.github.com/')
Expand Down
7 changes: 6 additions & 1 deletion src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ export const useNotifications = (): NotificationsState => {
notification.account.hostname,
notification.account.token,
);
await markNotificationRead(state, notification);

if (state.settings.markAsDoneOnUnsubscribe) {
await markNotificationDone(state, notification);
} else {
await markNotificationRead(state, notification);
}
} catch (err) {
log.error(
'Error occurred while unsubscribing from notification thread',
Expand Down
43 changes: 43 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 @@ -78,6 +78,7 @@ interface NotificationSettingsState {
participating: boolean;
showNotifications: boolean;
markAsDoneOnOpen: boolean;
markAsDoneOnUnsubscribe: boolean;
delayNotificationState: boolean;
groupBy: GroupBy;
}
Expand Down