Skip to content

Commit 463790c

Browse files
committed
refactor(handlers): use notification instead of subject
Signed-off-by: Adam Setch <[email protected]>
1 parent 86932c9 commit 463790c

13 files changed

+278
-379
lines changed

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

Lines changed: 34 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { mockNotificationWithSubject } from '../../../__mocks__/notifications-mocks';
22
import { partialMockNotification } from '../../../__mocks__/partial-mocks';
33
import { mockSettings } from '../../../__mocks__/state-mocks';
4-
import { checkSuiteHandler, getCheckSuiteAttributes } from './checkSuite';
4+
import type { StateType } from '../../../typesGitHub';
5+
import { createCheckSuiteHandler, getCheckSuiteAttributes } from './checkSuite';
56

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

14-
const result = await checkSuiteHandler.enrich(
15-
mockNotification,
16-
mockSettings,
17-
);
15+
const handler = createCheckSuiteHandler(mockNotification);
16+
const result = await handler.enrich(mockSettings);
1817

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

31-
const result = await checkSuiteHandler.enrich(
32-
mockNotification,
33-
mockSettings,
34-
);
30+
const handler = createCheckSuiteHandler(mockNotification);
31+
const result = await handler.enrich(mockSettings);
3532

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

48-
const result = await checkSuiteHandler.enrich(
49-
mockNotification,
50-
mockSettings,
51-
);
45+
const handler = createCheckSuiteHandler(mockNotification);
46+
const result = await handler.enrich(mockSettings);
5247

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

65-
const result = await checkSuiteHandler.enrich(
66-
mockNotification,
67-
mockSettings,
68-
);
60+
const handler = createCheckSuiteHandler(mockNotification);
61+
const result = await handler.enrich(mockSettings);
6962

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

82-
const result = await checkSuiteHandler.enrich(
83-
mockNotification,
84-
mockSettings,
85-
);
75+
const handler = createCheckSuiteHandler(mockNotification);
76+
const result = await handler.enrich(mockSettings);
8677

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

99-
const result = await checkSuiteHandler.enrich(
100-
mockNotification,
101-
mockSettings,
102-
);
90+
const handler = createCheckSuiteHandler(mockNotification);
91+
const result = await handler.enrich(mockSettings);
10392

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

116-
const result = await checkSuiteHandler.enrich(
117-
mockNotification,
118-
mockSettings,
119-
);
105+
const handler = createCheckSuiteHandler(mockNotification);
106+
const result = await handler.enrich(mockSettings);
120107

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

130-
const result = await checkSuiteHandler.enrich(
131-
mockNotification,
132-
mockSettings,
133-
);
117+
const handler = createCheckSuiteHandler(mockNotification);
118+
const result = await handler.enrich(mockSettings);
134119

135120
expect(result).toBeNull();
136121
});
137122
});
138123

139-
it('iconType', () => {
140-
expect(
141-
checkSuiteHandler.iconType(
142-
mockNotificationWithSubject({ type: 'CheckSuite', state: null }),
143-
).displayName,
144-
).toBe('RocketIcon');
145-
146-
expect(
147-
checkSuiteHandler.iconType(
148-
mockNotificationWithSubject({
149-
type: 'CheckSuite',
150-
state: 'cancelled',
151-
}),
152-
).displayName,
153-
).toBe('StopIcon');
154-
155-
expect(
156-
checkSuiteHandler.iconType(
157-
mockNotificationWithSubject({
158-
type: 'CheckSuite',
159-
state: 'failure',
160-
}),
161-
).displayName,
162-
).toBe('XIcon');
163-
164-
expect(
165-
checkSuiteHandler.iconType(
166-
mockNotificationWithSubject({
167-
type: 'CheckSuite',
168-
state: 'skipped',
169-
}),
170-
).displayName,
171-
).toBe('SkipIcon');
172-
173-
expect(
174-
checkSuiteHandler.iconType(
175-
mockNotificationWithSubject({
176-
type: 'CheckSuite',
177-
state: 'success',
178-
}),
179-
).displayName,
180-
).toBe('CheckIcon');
124+
describe('iconType', () => {
125+
const cases: Array<[StateType, string]> = [
126+
[null, 'RocketIcon'],
127+
['cancelled', 'StopIcon'],
128+
['failure', 'XIcon'],
129+
['skipped', 'SkipIcon'],
130+
['success', 'CheckIcon'],
131+
];
132+
133+
it.each(cases)('returns expected icon for %s', (state, expectedIcon) => {
134+
const handler = createCheckSuiteHandler(
135+
mockNotificationWithSubject({ type: 'CheckSuite', state }),
136+
);
137+
138+
expect(handler.iconType().displayName).toBe(expectedIcon);
139+
});
181140
});
182141

183142
describe('getCheckSuiteState', () => {

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../../../__mocks__/partial-mocks';
99
import { mockSettings } from '../../../__mocks__/state-mocks';
1010
import type { Link } from '../../../types';
11-
import { commitHandler } from './commit';
11+
import { createCommitHandler } from './commit';
1212

1313
describe('renderer/utils/notifications/handlers/commit.ts', () => {
1414
describe('enrich', () => {
@@ -40,7 +40,8 @@ describe('renderer/utils/notifications/handlers/commit.ts', () => {
4040
.get('/repos/gitify-app/notifications-test/comments/141012658')
4141
.reply(200, { user: mockCommenter });
4242

43-
const result = await commitHandler.enrich(mockNotification, mockSettings);
43+
const handler = createCommitHandler(mockNotification);
44+
const result = await handler.enrich(mockSettings);
4445

4546
expect(result).toEqual({
4647
state: null,
@@ -67,7 +68,8 @@ describe('renderer/utils/notifications/handlers/commit.ts', () => {
6768
)
6869
.reply(200, { author: mockAuthor });
6970

70-
const result = await commitHandler.enrich(mockNotification, mockSettings);
71+
const handler = createCommitHandler(mockNotification);
72+
const result = await handler.enrich(mockSettings);
7173

7274
expect(result).toEqual({
7375
state: null,
@@ -88,7 +90,8 @@ describe('renderer/utils/notifications/handlers/commit.ts', () => {
8890
latest_comment_url: null,
8991
});
9092

91-
const result = await commitHandler.enrich(mockNotification, {
93+
const handler = createCommitHandler(mockNotification);
94+
const result = await handler.enrich({
9295
...mockSettings,
9396
filterStates: ['closed'],
9497
});
@@ -98,9 +101,12 @@ describe('renderer/utils/notifications/handlers/commit.ts', () => {
98101
});
99102

100103
it('iconType', () => {
101-
expect(
102-
commitHandler.iconType(mockNotificationWithSubject({ type: 'Commit' }))
103-
.displayName,
104-
).toBe('GitCommitIcon');
104+
const handler = createCommitHandler(
105+
mockNotificationWithSubject({
106+
type: 'Commit',
107+
}),
108+
);
109+
110+
expect(handler.iconType().displayName).toBe('GitCommitIcon');
105111
});
106112
});

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { partialMockNotification } from '../../../__mocks__/partial-mocks';
33
import { mockSettings } from '../../../__mocks__/state-mocks';
44
import { IconColor } from '../../../types';
55
import type { StateType } from '../../../typesGitHub';
6-
import { defaultHandler } from './default';
6+
import { createDefaultHandler } from './default';
77

88
describe('renderer/utils/notifications/handlers/default.ts', () => {
99
describe('enrich', () => {
@@ -14,19 +14,17 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
1414
type: 'RepositoryInvitation',
1515
});
1616

17-
const result = await defaultHandler.enrich(
18-
mockNotification,
19-
mockSettings,
20-
);
17+
const handler = createDefaultHandler(mockNotification);
18+
const result = await handler.enrich(mockSettings);
2119

2220
expect(result).toBeNull();
2321
});
2422
});
2523

2624
it('iconType', () => {
27-
expect(
28-
defaultHandler.iconType(mockNotificationWithSubject({})).displayName,
29-
).toBe('QuestionIcon');
25+
const handler = createDefaultHandler(mockNotificationWithSubject({}));
26+
27+
expect(handler.iconType().displayName).toBe('QuestionIcon');
3028
});
3129

3230
describe('iconColor', () => {
@@ -50,8 +48,11 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
5048
];
5149

5250
it.each(cases)('returns correct color for state %s', (state, expected) => {
53-
const subject = mockNotificationWithSubject({ state });
54-
expect(defaultHandler.iconColor(subject)).toBe(expected);
51+
const handler = createDefaultHandler(
52+
mockNotificationWithSubject({ state }),
53+
);
54+
55+
expect(handler.iconColor()).toBe(expected);
5556
});
5657
});
5758

@@ -63,9 +64,9 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
6364
state: 'open',
6465
});
6566

66-
expect(defaultHandler.formattedNotificationType(notification)).toBe(
67-
'Open Pull Request',
68-
);
67+
const handler = createDefaultHandler(notification);
68+
69+
expect(handler.formattedNotificationType()).toBe('Open Pull Request');
6970
});
7071

7172
it('handles missing state (null) gracefully', () => {
@@ -75,9 +76,9 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
7576
state: null,
7677
});
7778

78-
expect(defaultHandler.formattedNotificationType(notification)).toBe(
79-
'Issue',
80-
);
79+
const handler = createDefaultHandler(notification);
80+
81+
expect(handler.formattedNotificationType()).toBe('Issue');
8182
});
8283
});
8384

@@ -89,9 +90,10 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
8990
state: 'open',
9091
});
9192
notification.subject.number = 42;
92-
expect(defaultHandler.formattedNotificationNumber(notification)).toBe(
93-
'#42',
94-
);
93+
94+
const handler = createDefaultHandler(notification);
95+
96+
expect(handler.formattedNotificationNumber()).toBe('#42');
9597
});
9698

9799
it('returns empty string when number absent', () => {
@@ -100,7 +102,10 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
100102
type: 'Issue',
101103
state: 'open',
102104
});
103-
expect(defaultHandler.formattedNotificationNumber(notification)).toBe('');
105+
106+
const handler = createDefaultHandler(notification);
107+
108+
expect(handler.formattedNotificationNumber()).toBe('');
104109
});
105110
});
106111

@@ -112,9 +117,10 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
112117
state: 'open',
113118
});
114119
notification.subject.number = 101;
115-
expect(defaultHandler.formattedNotificationTitle(notification)).toBe(
116-
'Fix bug [#101]',
117-
);
120+
121+
const handler = createDefaultHandler(notification);
122+
123+
expect(handler.formattedNotificationTitle()).toBe('Fix bug [#101]');
118124
});
119125

120126
it('returns title unchanged when number missing', () => {
@@ -123,9 +129,10 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
123129
type: 'Issue',
124130
state: 'open',
125131
});
126-
expect(defaultHandler.formattedNotificationTitle(notification)).toBe(
127-
'Improve docs',
128-
);
132+
133+
const handler = createDefaultHandler(notification);
134+
135+
expect(handler.formattedNotificationTitle()).toBe('Improve docs');
129136
});
130137
});
131138
});

0 commit comments

Comments
 (0)