Skip to content
Draft
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
27 changes: 12 additions & 15 deletions src/renderer/__mocks__/notifications-mocks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import type { AccountNotifications, GitifyError } from '../types';
import type {
Notification,
StateType,
Subject,
SubjectType,
} from '../typesGitHub';
import type { Notification, StateType, SubjectType } from '../typesGitHub';
import {
mockEnterpriseNotifications,
mockGitHubNotifications,
Expand Down Expand Up @@ -36,18 +31,20 @@ export const mockSingleAccountNotifications: AccountNotifications[] = [
},
];

export function createSubjectMock(mocks: {
export function mockNotificationWithSubject(mocks: {
title?: string;
type?: SubjectType;
state?: StateType;
}): Subject {
}): Notification {
return {
title: mocks.title ?? 'Mock Subject',
type: mocks.type ?? ('Unknown' as SubjectType),
state: mocks.state ?? ('Unknown' as StateType),
url: null,
latest_comment_url: null,
};
subject: {
title: mocks.title ?? 'Mock Subject',
type: mocks.type ?? ('Unknown' as SubjectType),
state: mocks.state ?? ('Unknown' as StateType),
url: null,
latest_comment_url: null,
},
} as Notification;
}

export function mockAccountWithError(error: GitifyError): AccountNotifications {
Expand All @@ -58,7 +55,7 @@ export function mockAccountWithError(error: GitifyError): AccountNotifications {
};
}

export function createMockNotificationForRepoName(
export function mockNotificationWithRepoName(
id: string,
repoFullName: string | null,
): Notification {
Expand Down
10 changes: 5 additions & 5 deletions src/renderer/components/notifications/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export const NotificationRow: FC<NotificationRowProps> = ({
};

const handler = createNotificationHandler(notification);
const NotificationIcon = handler.iconType(notification.subject);
const iconColor = handler.iconColor(notification.subject);
const notificationType = handler.formattedNotificationType(notification);
const notificationNumber = handler.formattedNotificationNumber(notification);
const notificationTitle = handler.formattedNotificationTitle(notification);
const NotificationIcon = handler.iconType();
const iconColor = handler.iconColor();
const notificationType = handler.formattedNotificationType();
const notificationNumber = handler.formattedNotificationNumber();
const notificationTitle = handler.formattedNotificationTitle();

const groupByDate = settings.groupBy === GroupBy.DATE;

Expand Down
52 changes: 26 additions & 26 deletions src/renderer/utils/notifications/group.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMockNotificationForRepoName } from '../../__mocks__/notifications-mocks';
import { mockNotificationWithRepoName } from '../../__mocks__/notifications-mocks';
import { mockSettings } from '../../__mocks__/state-mocks';
import { GroupBy } from '../../types';
import type { Notification } from '../../typesGitHub';
Expand All @@ -24,9 +24,9 @@ describe('renderer/utils/notifications/group.ts', () => {
describe('groupNotificationsByRepository', () => {
it('groups notifications by repository full_name', () => {
const notifications: Notification[] = [
createMockNotificationForRepoName('1', 'owner/repo-a'),
createMockNotificationForRepoName('2', 'owner/repo-b'),
createMockNotificationForRepoName('3', 'owner/repo-a'),
mockNotificationWithRepoName('1', 'owner/repo-a'),
mockNotificationWithRepoName('2', 'owner/repo-b'),
mockNotificationWithRepoName('3', 'owner/repo-a'),
];

const result = groupNotificationsByRepository(notifications);
Expand All @@ -38,10 +38,10 @@ describe('renderer/utils/notifications/group.ts', () => {

it('preserves first-seen repository order', () => {
const notifications: Notification[] = [
createMockNotificationForRepoName('1', 'owner/repo-c'),
createMockNotificationForRepoName('2', 'owner/repo-a'),
createMockNotificationForRepoName('3', 'owner/repo-b'),
createMockNotificationForRepoName('4', 'owner/repo-a'),
mockNotificationWithRepoName('1', 'owner/repo-c'),
mockNotificationWithRepoName('2', 'owner/repo-a'),
mockNotificationWithRepoName('3', 'owner/repo-b'),
mockNotificationWithRepoName('4', 'owner/repo-a'),
];

const result = groupNotificationsByRepository(notifications);
Expand All @@ -52,9 +52,9 @@ describe('renderer/utils/notifications/group.ts', () => {

it('skips notifications without repository data', () => {
const notifications: Notification[] = [
createMockNotificationForRepoName('1', 'owner/repo-a'),
createMockNotificationForRepoName('2', null),
createMockNotificationForRepoName('3', 'owner/repo-a'),
mockNotificationWithRepoName('1', 'owner/repo-a'),
mockNotificationWithRepoName('2', null),
mockNotificationWithRepoName('3', 'owner/repo-a'),
];

const result = groupNotificationsByRepository(notifications);
Expand All @@ -73,8 +73,8 @@ describe('renderer/utils/notifications/group.ts', () => {

it('returns empty map when all notifications lack repository data', () => {
const notifications: Notification[] = [
createMockNotificationForRepoName('1', null),
createMockNotificationForRepoName('2', null),
mockNotificationWithRepoName('1', null),
mockNotificationWithRepoName('2', null),
];

const result = groupNotificationsByRepository(notifications);
Expand All @@ -87,10 +87,10 @@ describe('renderer/utils/notifications/group.ts', () => {
it('returns repository-grouped order when groupBy is REPOSITORY', () => {
const settings = { ...mockSettings, groupBy: GroupBy.REPOSITORY };
const notifications: Notification[] = [
createMockNotificationForRepoName('1', 'owner/repo-b'),
createMockNotificationForRepoName('2', 'owner/repo-a'),
createMockNotificationForRepoName('3', 'owner/repo-b'),
createMockNotificationForRepoName('4', 'owner/repo-a'),
mockNotificationWithRepoName('1', 'owner/repo-b'),
mockNotificationWithRepoName('2', 'owner/repo-a'),
mockNotificationWithRepoName('3', 'owner/repo-b'),
mockNotificationWithRepoName('4', 'owner/repo-a'),
];

const result = getFlattenedNotificationsByRepo(notifications, settings);
Expand All @@ -102,9 +102,9 @@ describe('renderer/utils/notifications/group.ts', () => {
it('returns natural account order when groupBy is DATE', () => {
const settings = { ...mockSettings, groupBy: GroupBy.DATE };
const notifications: Notification[] = [
createMockNotificationForRepoName('1', 'owner/repo-b'),
createMockNotificationForRepoName('2', 'owner/repo-a'),
createMockNotificationForRepoName('3', 'owner/repo-b'),
mockNotificationWithRepoName('1', 'owner/repo-b'),
mockNotificationWithRepoName('2', 'owner/repo-a'),
mockNotificationWithRepoName('3', 'owner/repo-b'),
];

const result = getFlattenedNotificationsByRepo(notifications, settings);
Expand All @@ -125,9 +125,9 @@ describe('renderer/utils/notifications/group.ts', () => {
it('handles notifications without repository data when grouped', () => {
const settings = { ...mockSettings, groupBy: GroupBy.REPOSITORY };
const notifications: Notification[] = [
createMockNotificationForRepoName('1', 'owner/repo-a'),
createMockNotificationForRepoName('2', null),
createMockNotificationForRepoName('3', 'owner/repo-a'),
mockNotificationWithRepoName('1', 'owner/repo-a'),
mockNotificationWithRepoName('2', null),
mockNotificationWithRepoName('3', 'owner/repo-a'),
];

const result = getFlattenedNotificationsByRepo(notifications, settings);
Expand All @@ -139,9 +139,9 @@ describe('renderer/utils/notifications/group.ts', () => {
it('preserves notifications without repository data when not grouped', () => {
const settings = { ...mockSettings, groupBy: GroupBy.DATE };
const notifications: Notification[] = [
createMockNotificationForRepoName('1', 'owner/repo-a'),
createMockNotificationForRepoName('2', null),
createMockNotificationForRepoName('3', 'owner/repo-a'),
mockNotificationWithRepoName('1', 'owner/repo-a'),
mockNotificationWithRepoName('2', null),
mockNotificationWithRepoName('3', 'owner/repo-a'),
];

const result = getFlattenedNotificationsByRepo(notifications, settings);
Expand Down
111 changes: 35 additions & 76 deletions src/renderer/utils/notifications/handlers/checkSuite.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createSubjectMock } from '../../../__mocks__/notifications-mocks';
import { mockNotificationWithSubject } from '../../../__mocks__/notifications-mocks';
import { partialMockNotification } from '../../../__mocks__/partial-mocks';
import { mockSettings } from '../../../__mocks__/state-mocks';
import { checkSuiteHandler, getCheckSuiteAttributes } from './checkSuite';
import type { StateType } from '../../../typesGitHub';
import { createCheckSuiteHandler, getCheckSuiteAttributes } from './checkSuite';

describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
describe('enrich', () => {
Expand All @@ -11,10 +12,8 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
type: 'CheckSuite',
});

const result = await checkSuiteHandler.enrich(
mockNotification,
mockSettings,
);
const handler = createCheckSuiteHandler(mockNotification);
const result = await handler.enrich(mockSettings);

expect(result).toEqual({
state: 'cancelled',
Expand All @@ -28,10 +27,8 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
type: 'CheckSuite',
});

const result = await checkSuiteHandler.enrich(
mockNotification,
mockSettings,
);
const handler = createCheckSuiteHandler(mockNotification);
const result = await handler.enrich(mockSettings);

expect(result).toEqual({
state: 'failure',
Expand All @@ -45,10 +42,8 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
type: 'CheckSuite',
});

const result = await checkSuiteHandler.enrich(
mockNotification,
mockSettings,
);
const handler = createCheckSuiteHandler(mockNotification);
const result = await handler.enrich(mockSettings);

expect(result).toEqual({
state: 'failure',
Expand All @@ -62,10 +57,8 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
type: 'CheckSuite',
});

const result = await checkSuiteHandler.enrich(
mockNotification,
mockSettings,
);
const handler = createCheckSuiteHandler(mockNotification);
const result = await handler.enrich(mockSettings);

expect(result).toEqual({
state: 'failure',
Expand All @@ -79,10 +72,8 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
type: 'CheckSuite',
});

const result = await checkSuiteHandler.enrich(
mockNotification,
mockSettings,
);
const handler = createCheckSuiteHandler(mockNotification);
const result = await handler.enrich(mockSettings);

expect(result).toEqual({
state: 'skipped',
Expand All @@ -96,10 +87,8 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
type: 'CheckSuite',
});

const result = await checkSuiteHandler.enrich(
mockNotification,
mockSettings,
);
const handler = createCheckSuiteHandler(mockNotification);
const result = await handler.enrich(mockSettings);

expect(result).toEqual({
state: 'success',
Expand All @@ -113,10 +102,8 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
type: 'CheckSuite',
});

const result = await checkSuiteHandler.enrich(
mockNotification,
mockSettings,
);
const handler = createCheckSuiteHandler(mockNotification);
const result = await handler.enrich(mockSettings);

expect(result).toBeNull();
});
Expand All @@ -127,57 +114,29 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
type: 'CheckSuite',
});

const result = await checkSuiteHandler.enrich(
mockNotification,
mockSettings,
);
const handler = createCheckSuiteHandler(mockNotification);
const result = await handler.enrich(mockSettings);

expect(result).toBeNull();
});
});

it('iconType', () => {
expect(
checkSuiteHandler.iconType(
createSubjectMock({ type: 'CheckSuite', state: null }),
).displayName,
).toBe('RocketIcon');

expect(
checkSuiteHandler.iconType(
createSubjectMock({
type: 'CheckSuite',
state: 'cancelled',
}),
).displayName,
).toBe('StopIcon');

expect(
checkSuiteHandler.iconType(
createSubjectMock({
type: 'CheckSuite',
state: 'failure',
}),
).displayName,
).toBe('XIcon');

expect(
checkSuiteHandler.iconType(
createSubjectMock({
type: 'CheckSuite',
state: 'skipped',
}),
).displayName,
).toBe('SkipIcon');

expect(
checkSuiteHandler.iconType(
createSubjectMock({
type: 'CheckSuite',
state: 'success',
}),
).displayName,
).toBe('CheckIcon');
describe('iconType', () => {
const cases: Array<[StateType, string]> = [
[null, 'RocketIcon'],
['cancelled', 'StopIcon'],
['failure', 'XIcon'],
['skipped', 'SkipIcon'],
['success', 'CheckIcon'],
];

it.each(cases)('returns expected icon for %s', (state, expectedIcon) => {
const handler = createCheckSuiteHandler(
mockNotificationWithSubject({ type: 'CheckSuite', state }),
);

expect(handler.iconType().displayName).toBe(expectedIcon);
});
});

describe('getCheckSuiteState', () => {
Expand Down
Loading
Loading