Skip to content

Commit 18524e5

Browse files
committed
simplify state filtering
Signed-off-by: Adam Setch <[email protected]>
1 parent fe5ee69 commit 18524e5

File tree

3 files changed

+85
-93
lines changed

3 files changed

+85
-93
lines changed
Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { FilterStateType, GitifyNotificationState } from '../../../types';
12
import type { Notification } from '../../../typesGitHub';
23
import { stateFilter } from './state';
34

@@ -6,56 +7,52 @@ describe('renderer/utils/notifications/filters/state.ts', () => {
67
jest.clearAllMocks();
78
});
89

9-
it('can filter by notification states', () => {
10-
const mockPartialNotification = {
11-
subject: {
12-
state: 'OPEN',
13-
},
10+
describe('can filter by notification states', () => {
11+
const mockNotification = {
12+
subject: { state: 'OPEN' },
1413
} as Partial<Notification> as Notification;
1514

16-
// Open states
17-
mockPartialNotification.subject.state = 'OPEN';
18-
expect(
19-
stateFilter.filterNotification(mockPartialNotification, 'open'),
20-
).toBe(true);
21-
22-
mockPartialNotification.subject.state = 'REOPENED';
23-
expect(
24-
stateFilter.filterNotification(mockPartialNotification, 'open'),
25-
).toBe(true);
26-
27-
// Closed states
28-
mockPartialNotification.subject.state = 'CLOSED';
29-
expect(
30-
stateFilter.filterNotification(mockPartialNotification, 'closed'),
31-
).toBe(true);
32-
33-
mockPartialNotification.subject.state = 'COMPLETED';
34-
expect(
35-
stateFilter.filterNotification(mockPartialNotification, 'closed'),
36-
).toBe(true);
37-
38-
mockPartialNotification.subject.state = 'NOT_PLANNED';
39-
expect(
40-
stateFilter.filterNotification(mockPartialNotification, 'closed'),
41-
).toBe(true);
42-
43-
// Merged states
44-
mockPartialNotification.subject.state = 'MERGED';
45-
expect(
46-
stateFilter.filterNotification(mockPartialNotification, 'merged'),
47-
).toBe(true);
48-
49-
// Draft states
50-
mockPartialNotification.subject.state = 'DRAFT';
51-
expect(
52-
stateFilter.filterNotification(mockPartialNotification, 'draft'),
53-
).toBe(true);
54-
55-
// Other states
56-
mockPartialNotification.subject.state = 'OUTDATED';
57-
expect(
58-
stateFilter.filterNotification(mockPartialNotification, 'other'),
59-
).toBe(true);
15+
const cases = {
16+
OPEN: 'open',
17+
REOPENED: 'open',
18+
19+
CLOSED: 'closed',
20+
COMPLETED: 'closed',
21+
DUPLICATE: 'closed',
22+
NOT_PLANNED: 'closed',
23+
RESOLVED: 'closed',
24+
25+
MERGED: 'merged',
26+
DRAFT: 'draft',
27+
28+
// Discussion-specific
29+
ANSWERED: 'other',
30+
OUTDATED: 'other',
31+
32+
// Check suite / workflow states
33+
ACTION_REQUIRED: 'other',
34+
CANCELLED: 'other',
35+
FAILURE: 'other',
36+
IN_PROGRESS: 'other',
37+
PENDING: 'other',
38+
QUEUED: 'other',
39+
REQUESTED: 'other',
40+
SKIPPED: 'other',
41+
STALE: 'other',
42+
SUCCESS: 'other',
43+
TIMED_OUT: 'other',
44+
WAITING: 'other',
45+
} satisfies Record<GitifyNotificationState, FilterStateType>;
46+
47+
it.each(
48+
Object.entries(cases) as Array<
49+
[GitifyNotificationState, FilterStateType]
50+
>,
51+
)('filter notification with state %s as %s', (notificationState, expectedFilter) => {
52+
mockNotification.subject.state = notificationState;
53+
expect(
54+
stateFilter.filterNotification(mockNotification, expectedFilter),
55+
).toBe(true);
56+
});
6057
});
6158
});

src/renderer/utils/notifications/filters/state.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const STATE_TYPE_DETAILS: Record<FilterStateType, TypeDetails> = {
2121
},
2222
closed: {
2323
title: 'Closed',
24-
description: 'Closed, completed or not planned',
24+
description: 'Closed, completed, duplicate, resolved or not planned',
2525
},
2626
other: {
2727
title: 'Other',
@@ -64,32 +64,29 @@ export const stateFilter: Filter<FilterStateType> = {
6464
notification: Notification,
6565
stateType: FilterStateType,
6666
): boolean {
67-
const allOpenStates: GitifyNotificationState[] = ['OPEN', 'REOPENED'];
68-
const allClosedStates: GitifyNotificationState[] = [
69-
'CLOSED',
70-
'COMPLETED',
71-
'NOT_PLANNED',
72-
];
73-
const allMergedStates: GitifyNotificationState[] = ['MERGED'];
74-
const allDraftStates: GitifyNotificationState[] = ['DRAFT'];
75-
const allFilterableStates = [
76-
...allOpenStates,
77-
...allClosedStates,
78-
...allMergedStates,
79-
...allDraftStates,
80-
];
81-
82-
switch (stateType) {
83-
case 'open':
84-
return allOpenStates.includes(notification.subject?.state);
85-
case 'closed':
86-
return allClosedStates.includes(notification.subject?.state);
87-
case 'merged':
88-
return allMergedStates.includes(notification.subject?.state);
89-
case 'draft':
90-
return allDraftStates.includes(notification.subject?.state);
91-
default:
92-
return !allFilterableStates.includes(notification.subject?.state);
93-
}
67+
const mapped = mapStateToFilter(notification.subject?.state);
68+
return stateType === mapped;
9469
},
9570
};
71+
72+
function mapStateToFilter(
73+
state: GitifyNotificationState | null | undefined,
74+
): FilterStateType {
75+
switch (state) {
76+
case 'OPEN':
77+
case 'REOPENED':
78+
return 'open';
79+
case 'CLOSED':
80+
case 'COMPLETED':
81+
case 'DUPLICATE':
82+
case 'NOT_PLANNED':
83+
case 'RESOLVED':
84+
return 'closed';
85+
case 'MERGED':
86+
return 'merged';
87+
case 'DRAFT':
88+
return 'draft';
89+
default:
90+
return 'other';
91+
}
92+
}

src/renderer/utils/notifications/handlers/index.test.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,26 @@ import { releaseHandler } from './release';
1111
import { repositoryDependabotAlertsThreadHandler } from './repositoryDependabotAlertsThread';
1212
import { repositoryInvitationHandler } from './repositoryInvitation';
1313
import { repositoryVulnerabilityAlertHandler } from './repositoryVulnerabilityAlert';
14+
import type { NotificationTypeHandler } from './types';
1415
import { workflowRunHandler } from './workflowRun';
1516

1617
describe('renderer/utils/notifications/handlers/index.ts', () => {
1718
describe('createNotificationHandler', () => {
18-
const cases: Array<[SubjectType, object]> = [
19-
['CheckSuite', checkSuiteHandler],
20-
['Commit', commitHandler],
21-
['Discussion', discussionHandler],
22-
['Issue', issueHandler],
23-
['PullRequest', pullRequestHandler],
24-
['Release', releaseHandler],
25-
[
26-
'RepositoryDependabotAlertsThread',
27-
repositoryDependabotAlertsThreadHandler,
28-
],
29-
['RepositoryInvitation', repositoryInvitationHandler],
30-
['RepositoryVulnerabilityAlert', repositoryVulnerabilityAlertHandler],
31-
['WorkflowRun', workflowRunHandler],
32-
];
19+
const cases = {
20+
CheckSuite: checkSuiteHandler,
21+
Commit: commitHandler,
22+
Discussion: discussionHandler,
23+
Issue: issueHandler,
24+
PullRequest: pullRequestHandler,
25+
Release: releaseHandler,
26+
RepositoryDependabotAlertsThread: repositoryDependabotAlertsThreadHandler,
27+
RepositoryInvitation: repositoryInvitationHandler,
28+
RepositoryVulnerabilityAlert: repositoryVulnerabilityAlertHandler,
29+
WorkflowRun: workflowRunHandler,
30+
} satisfies Record<SubjectType, NotificationTypeHandler>;
3331

3432
it.each(
35-
cases,
33+
Object.entries(cases) as Array<[SubjectType, NotificationTypeHandler]>,
3634
)('returns expected handler instance for %s', (type, expected) => {
3735
const notification = createPartialMockNotification({ type });
3836
const handler = createNotificationHandler(notification);

0 commit comments

Comments
 (0)