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

refactor: combine mark as fns to support a list of notifications #1560

Merged
merged 5 commits into from
Sep 28, 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
40 changes: 20 additions & 20 deletions src/components/NotificationRow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('components/NotificationRow.tsx', () => {

describe('notification interactions', () => {
it('should open a notification in the browser - click', () => {
const markNotificationRead = jest.fn();
const markNotificationsAsRead = jest.fn();

const props = {
notification: mockSingleNotification,
Expand All @@ -93,7 +93,7 @@ describe('components/NotificationRow.tsx', () => {
<AppContext.Provider
value={{
settings: { ...mockSettings, markAsDoneOnOpen: false },
markNotificationRead,
markNotificationsAsRead,
auth: mockAuth,
}}
>
Expand All @@ -103,11 +103,11 @@ describe('components/NotificationRow.tsx', () => {

fireEvent.click(screen.getByRole('main'));
expect(links.openNotification).toHaveBeenCalledTimes(1);
expect(markNotificationRead).toHaveBeenCalledTimes(1);
expect(markNotificationsAsRead).toHaveBeenCalledTimes(1);
});

it('should open a notification in the browser - delay notification setting enabled', () => {
const markNotificationRead = jest.fn();
const markNotificationsAsRead = jest.fn();

const props = {
notification: mockSingleNotification,
Expand All @@ -122,7 +122,7 @@ describe('components/NotificationRow.tsx', () => {
markAsDoneOnOpen: false,
delayNotificationState: true,
},
markNotificationRead,
markNotificationsAsRead,
auth: mockAuth,
}}
>
Expand All @@ -132,11 +132,11 @@ describe('components/NotificationRow.tsx', () => {

fireEvent.click(screen.getByRole('main'));
expect(links.openNotification).toHaveBeenCalledTimes(1);
expect(markNotificationRead).toHaveBeenCalledTimes(1);
expect(markNotificationsAsRead).toHaveBeenCalledTimes(1);
});

it('should open a notification in the browser - key down', () => {
const markNotificationRead = jest.fn();
const markNotificationsAsRead = jest.fn();

const props = {
notification: mockSingleNotification,
Expand All @@ -147,7 +147,7 @@ describe('components/NotificationRow.tsx', () => {
<AppContext.Provider
value={{
settings: { ...mockSettings, markAsDoneOnOpen: false },
markNotificationRead,
markNotificationsAsRead,
auth: mockAuth,
}}
>
Expand All @@ -157,11 +157,11 @@ describe('components/NotificationRow.tsx', () => {

fireEvent.click(screen.getByRole('main'));
expect(links.openNotification).toHaveBeenCalledTimes(1);
expect(markNotificationRead).toHaveBeenCalledTimes(1);
expect(markNotificationsAsRead).toHaveBeenCalledTimes(1);
});

it('should open a notification in browser & mark it as done', () => {
const markNotificationDone = jest.fn();
const markNotificationsAsDone = jest.fn();

const props = {
notification: mockSingleNotification,
Expand All @@ -172,7 +172,7 @@ describe('components/NotificationRow.tsx', () => {
<AppContext.Provider
value={{
settings: { ...mockSettings, markAsDoneOnOpen: true },
markNotificationDone,
markNotificationsAsDone,
auth: mockAuth,
}}
>
Expand All @@ -182,11 +182,11 @@ describe('components/NotificationRow.tsx', () => {

fireEvent.click(screen.getByRole('main'));
expect(links.openNotification).toHaveBeenCalledTimes(1);
expect(markNotificationDone).toHaveBeenCalledTimes(1);
expect(markNotificationsAsDone).toHaveBeenCalledTimes(1);
});

it('should mark a notification as read', () => {
const markNotificationRead = jest.fn();
it('should mark notifications as read', () => {
const markNotificationsAsRead = jest.fn();

const props = {
notification: mockSingleNotification,
Expand All @@ -197,19 +197,19 @@ describe('components/NotificationRow.tsx', () => {
<AppContext.Provider
value={{
settings: { ...mockSettings, markAsDoneOnOpen: false },
markNotificationRead,
markNotificationsAsRead,
}}
>
<NotificationRow {...props} />
</AppContext.Provider>,
);

fireEvent.click(screen.getByTitle('Mark as read'));
expect(markNotificationRead).toHaveBeenCalledTimes(1);
expect(markNotificationsAsRead).toHaveBeenCalledTimes(1);
});

it('should mark a notification as done', () => {
const markNotificationDone = jest.fn();
it('should mark notifications as done', () => {
const markNotificationsAsDone = jest.fn();

const props = {
notification: mockSingleNotification,
Expand All @@ -218,14 +218,14 @@ describe('components/NotificationRow.tsx', () => {

render(
<AppContext.Provider
value={{ settings: mockSettings, markNotificationDone }}
value={{ settings: mockSettings, markNotificationsAsDone }}
>
<NotificationRow {...props} />
</AppContext.Provider>,
);

fireEvent.click(screen.getByTitle('Mark as done'));
expect(markNotificationDone).toHaveBeenCalledTimes(1);
expect(markNotificationsAsDone).toHaveBeenCalledTimes(1);
});

it('should unsubscribe from a notification thread', () => {
Expand Down
19 changes: 12 additions & 7 deletions src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const NotificationRow: FC<INotificationRow> = ({
}: INotificationRow) => {
const {
settings,
markNotificationRead,
markNotificationDone,
markNotificationsAsRead,
markNotificationsAsDone,
unsubscribeNotification,
} = useContext(AppContext);
const [animateExit, setAnimateExit] = useState(false);
Expand All @@ -51,11 +51,16 @@ export const NotificationRow: FC<INotificationRow> = ({
openNotification(notification);

if (settings.markAsDoneOnOpen) {
markNotificationDone(notification);
markNotificationsAsDone([notification]);
} else {
markNotificationRead(notification);
markNotificationsAsRead([notification]);
}
}, [notification, markNotificationDone, markNotificationRead, settings]);
}, [
notification,
markNotificationsAsRead,
markNotificationsAsDone,
settings,
]);

const unsubscribeFromThread = (event: MouseEvent<HTMLElement>) => {
// Don't trigger onClick of parent element.
Expand Down Expand Up @@ -137,7 +142,7 @@ export const NotificationRow: FC<INotificationRow> = ({
onClick={() => {
setAnimateExit(!settings.delayNotificationState);
setShowAsRead(settings.delayNotificationState);
markNotificationDone(notification);
markNotificationsAsDone([notification]);
}}
/>
)}
Expand All @@ -148,7 +153,7 @@ export const NotificationRow: FC<INotificationRow> = ({
onClick={() => {
setAnimateExit(!settings.delayNotificationState);
setShowAsRead(settings.delayNotificationState);
markNotificationRead(notification);
markNotificationsAsRead([notification]);
}}
/>
<InteractionButton
Expand Down
21 changes: 9 additions & 12 deletions src/components/RepositoryNotifications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { act, fireEvent, render, screen } from '@testing-library/react';
import { mockGitHubCloudAccount, mockSettings } from '../__mocks__/state-mocks';
import { AppContext } from '../context/App';
import type { Link } from '../types';
import {
mockGitHubNotifications,
mockSingleNotification,
} from '../utils/api/__mocks__/response-mocks';
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
import * as comms from '../utils/comms';
import { RepositoryNotifications } from './RepositoryNotifications';

Expand All @@ -14,8 +11,8 @@ jest.mock('./NotificationRow', () => ({
}));

describe('components/RepositoryNotifications.tsx', () => {
const markRepoNotificationsRead = jest.fn();
const markRepoNotificationsDone = jest.fn();
const markNotificationsAsRead = jest.fn();
const markNotificationsAsDone = jest.fn();

const props = {
account: mockGitHubCloudAccount,
Expand Down Expand Up @@ -58,32 +55,32 @@ describe('components/RepositoryNotifications.tsx', () => {
it('should mark a repo as read', () => {
render(
<AppContext.Provider
value={{ settings: { ...mockSettings }, markRepoNotificationsRead }}
value={{ settings: { ...mockSettings }, markNotificationsAsRead }}
>
<RepositoryNotifications {...props} />
</AppContext.Provider>,
);

fireEvent.click(screen.getByTitle('Mark repository as read'));

expect(markRepoNotificationsRead).toHaveBeenCalledWith(
mockSingleNotification,
expect(markNotificationsAsRead).toHaveBeenCalledWith(
mockGitHubNotifications,
);
});

it('should mark a repo as done', () => {
render(
<AppContext.Provider
value={{ settings: { ...mockSettings }, markRepoNotificationsDone }}
value={{ settings: { ...mockSettings }, markNotificationsAsDone }}
>
<RepositoryNotifications {...props} />
</AppContext.Provider>,
);

fireEvent.click(screen.getByTitle('Mark repository as done'));

expect(markRepoNotificationsDone).toHaveBeenCalledWith(
mockSingleNotification,
expect(markNotificationsAsDone).toHaveBeenCalledWith(
mockGitHubNotifications,
);
});

Expand Down
6 changes: 3 additions & 3 deletions src/components/RepositoryNotifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const RepositoryNotifications: FC<IRepositoryNotifications> = ({
repoName,
repoNotifications,
}) => {
const { settings, markRepoNotificationsRead, markRepoNotificationsDone } =
const { settings, markNotificationsAsRead, markNotificationsAsDone } =
useContext(AppContext);
const [animateExit, setAnimateExit] = useState(false);
const [showAsRead, setShowAsRead] = useState(false);
Expand Down Expand Up @@ -91,7 +91,7 @@ export const RepositoryNotifications: FC<IRepositoryNotifications> = ({
event.stopPropagation();
setAnimateExit(!settings.delayNotificationState);
setShowAsRead(settings.delayNotificationState);
markRepoNotificationsDone(repoNotifications[0]);
markNotificationsAsDone(repoNotifications);
}}
/>
)}
Expand All @@ -104,7 +104,7 @@ export const RepositoryNotifications: FC<IRepositoryNotifications> = ({
event.stopPropagation();
setAnimateExit(!settings.delayNotificationState);
setShowAsRead(settings.delayNotificationState);
markRepoNotificationsRead(repoNotifications[0]);
markNotificationsAsRead(repoNotifications);
}}
/>
<InteractionButton
Expand Down
Loading