Skip to content

Commit df570ea

Browse files
committed
refactor: tests
Signed-off-by: Adam Setch <[email protected]>
1 parent 5f42503 commit df570ea

20 files changed

+495
-374
lines changed

src/renderer/__mocks__/notifications-mocks.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ export function createPartialMockNotification(
7171
hasRequiredScopes: true,
7272
},
7373
subject: subject as Subject,
74-
repository: repository as Repository,
74+
repository: {
75+
name: 'notifications-test',
76+
full_name: 'gitify-app/notifications-test',
77+
html_url: 'https://github.com/gitify-app/notifications-test',
78+
owner: {
79+
login: 'gitify-app',
80+
},
81+
...repository,
82+
} as Repository,
7583
};
7684

7785
return mockNotification as Notification;

src/renderer/utils/helpers.test.ts

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import type { ExecutionResult } from 'graphql';
88

99
import type { Hostname, Link } from '../types';
10-
import type { SubjectType } from '../typesGitHub';
10+
import type { Subject, SubjectType } from '../typesGitHub';
1111
import * as logger from '../utils/logger';
1212
import {
1313
mockDiscussionByNumberGraphQLResponse,
@@ -81,14 +81,35 @@ describe('renderer/utils/helpers.ts', () => {
8181
jest.clearAllMocks();
8282
});
8383

84+
it('Subject HTML URL: prefer if available from enrichment stage', async () => {
85+
const mockHtmlUrl = 'https://gitify.io/' as Link;
86+
87+
const subject = {
88+
title: 'generate github web url unit tests',
89+
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/1' as Link,
90+
latest_comment_url:
91+
'https://api.github.com/repos/gitify-app/notifications-test/issues/comments/302888448' as Link,
92+
type: 'Issue' as SubjectType,
93+
htmlUrl: mockHtmlUrl,
94+
} as Subject;
95+
96+
const result = await generateGitHubWebUrl({
97+
...mockSingleNotification,
98+
subject: subject,
99+
});
100+
101+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
102+
expect(result).toBe(`${mockHtmlUrl}?${mockNotificationReferrer}`);
103+
});
104+
84105
it('Subject Latest Comment Url: when not null, fetch latest comment html url', async () => {
85106
const subject = {
86107
title: 'generate github web url unit tests',
87108
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/1' as Link,
88109
latest_comment_url:
89110
'https://api.github.com/repos/gitify-app/notifications-test/issues/comments/302888448' as Link,
90111
type: 'Issue' as SubjectType,
91-
};
112+
} as Subject;
92113

93114
getHtmlUrlSpy.mockResolvedValue(mockHtmlUrl);
94115

@@ -107,7 +128,7 @@ describe('renderer/utils/helpers.ts', () => {
107128
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/1' as Link,
108129
latest_comment_url: null,
109130
type: 'Issue' as SubjectType,
110-
};
131+
} as Subject;
111132

112133
getHtmlUrlSpy.mockResolvedValue(mockHtmlUrl);
113134

@@ -127,7 +148,7 @@ describe('renderer/utils/helpers.ts', () => {
127148
url: null,
128149
latest_comment_url: null,
129150
type: 'CheckSuite' as SubjectType,
130-
};
151+
} as Subject;
131152

132153
const result = await generateGitHubWebUrl({
133154
...mockSingleNotification,
@@ -136,7 +157,7 @@ describe('renderer/utils/helpers.ts', () => {
136157

137158
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
138159
expect(result).toBe(
139-
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Asuccess+branch%3Amain&${mockNotificationReferrer}`,
160+
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3ASUCCESS+branch%3Amain&${mockNotificationReferrer}`,
140161
);
141162
});
142163

@@ -146,7 +167,7 @@ describe('renderer/utils/helpers.ts', () => {
146167
url: null,
147168
latest_comment_url: null,
148169
type: 'CheckSuite' as SubjectType,
149-
};
170+
} as Subject;
150171

151172
const result = await generateGitHubWebUrl({
152173
...mockSingleNotification,
@@ -155,7 +176,7 @@ describe('renderer/utils/helpers.ts', () => {
155176

156177
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
157178
expect(result).toBe(
158-
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Afailure+branch%3Amain&${mockNotificationReferrer}`,
179+
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3AFAILURE+branch%3Amain&${mockNotificationReferrer}`,
159180
);
160181
});
161182

@@ -165,7 +186,7 @@ describe('renderer/utils/helpers.ts', () => {
165186
url: null,
166187
latest_comment_url: null,
167188
type: 'CheckSuite' as SubjectType,
168-
};
189+
} as Subject;
169190

170191
const result = await generateGitHubWebUrl({
171192
...mockSingleNotification,
@@ -174,7 +195,7 @@ describe('renderer/utils/helpers.ts', () => {
174195

175196
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
176197
expect(result).toBe(
177-
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Afailure+branch%3Amain&${mockNotificationReferrer}`,
198+
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3AFAILURE+branch%3Amain&${mockNotificationReferrer}`,
178199
);
179200
});
180201

@@ -184,7 +205,7 @@ describe('renderer/utils/helpers.ts', () => {
184205
url: null,
185206
latest_comment_url: null,
186207
type: 'CheckSuite' as SubjectType,
187-
};
208+
} as Subject;
188209

189210
const result = await generateGitHubWebUrl({
190211
...mockSingleNotification,
@@ -193,7 +214,7 @@ describe('renderer/utils/helpers.ts', () => {
193214

194215
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
195216
expect(result).toBe(
196-
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Askipped+branch%3Amain&${mockNotificationReferrer}`,
217+
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3ASKIPPED+branch%3Amain&${mockNotificationReferrer}`,
197218
);
198219
});
199220

@@ -203,7 +224,7 @@ describe('renderer/utils/helpers.ts', () => {
203224
url: null,
204225
latest_comment_url: null,
205226
type: 'CheckSuite' as SubjectType,
206-
};
227+
} as Subject;
207228

208229
const result = await generateGitHubWebUrl({
209230
...mockSingleNotification,
@@ -222,7 +243,7 @@ describe('renderer/utils/helpers.ts', () => {
222243
url: null,
223244
latest_comment_url: null,
224245
type: 'CheckSuite' as SubjectType,
225-
};
246+
} as Subject;
226247

227248
const result = await generateGitHubWebUrl({
228249
...mockSingleNotification,
@@ -241,7 +262,7 @@ describe('renderer/utils/helpers.ts', () => {
241262
url: null,
242263
latest_comment_url: null,
243264
type: 'CheckSuite' as SubjectType,
244-
};
265+
} as Subject;
245266

246267
const result = await generateGitHubWebUrl({
247268
...mockSingleNotification,
@@ -267,7 +288,7 @@ describe('renderer/utils/helpers.ts', () => {
267288
url: null,
268289
latest_comment_url: null,
269290
type: 'Discussion' as SubjectType,
270-
};
291+
} as Subject;
271292

272293
fetchDiscussionByNumberSpy.mockResolvedValue({
273294
data: { repository: { discussion: null } },
@@ -294,7 +315,7 @@ describe('renderer/utils/helpers.ts', () => {
294315
url: null,
295316
latest_comment_url: null,
296317
type: 'Discussion' as SubjectType,
297-
};
318+
} as Subject;
298319

299320
fetchDiscussionByNumberSpy.mockResolvedValue({
300321
data: null,
@@ -323,7 +344,7 @@ describe('renderer/utils/helpers.ts', () => {
323344
url: null,
324345
latest_comment_url: null,
325346
type: 'Discussion' as SubjectType,
326-
};
347+
} as Subject;
327348

328349
fetchDiscussionByNumberSpy.mockResolvedValue({
329350
data: mockDiscussionByNumberGraphQLResponse,
@@ -350,7 +371,7 @@ describe('renderer/utils/helpers.ts', () => {
350371
url: null,
351372
latest_comment_url: null,
352373
type: 'Discussion' as SubjectType,
353-
};
374+
} as Subject;
354375

355376
fetchDiscussionByNumberSpy.mockRejectedValue(
356377
new Error('Something failed'),
@@ -376,7 +397,7 @@ describe('renderer/utils/helpers.ts', () => {
376397
url: null,
377398
latest_comment_url: null,
378399
type: 'RepositoryInvitation' as SubjectType,
379-
};
400+
} as Subject;
380401

381402
const result = await generateGitHubWebUrl({
382403
...mockSingleNotification,
@@ -415,7 +436,7 @@ describe('renderer/utils/helpers.ts', () => {
415436
url: null,
416437
latest_comment_url: null,
417438
type: 'WorkflowRun' as SubjectType,
418-
};
439+
} as Subject;
419440

420441
const result = await generateGitHubWebUrl({
421442
...mockSingleNotification,
@@ -424,7 +445,7 @@ describe('renderer/utils/helpers.ts', () => {
424445

425446
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
426447
expect(result).toBe(
427-
`https://github.com/gitify-app/notifications-test/actions?query=is%3Awaiting&${mockNotificationReferrer}`,
448+
`https://github.com/gitify-app/notifications-test/actions?query=is%3AWAITING&${mockNotificationReferrer}`,
428449
);
429450
});
430451

@@ -435,7 +456,7 @@ describe('renderer/utils/helpers.ts', () => {
435456
url: null,
436457
latest_comment_url: null,
437458
type: 'WorkflowRun' as SubjectType,
438-
};
459+
} as Subject;
439460

440461
const result = await generateGitHubWebUrl({
441462
...mockSingleNotification,
@@ -454,7 +475,7 @@ describe('renderer/utils/helpers.ts', () => {
454475
url: null,
455476
latest_comment_url: null,
456477
type: 'WorkflowRun' as SubjectType,
457-
};
478+
} as Subject;
458479

459480
const result = await generateGitHubWebUrl({
460481
...mockSingleNotification,
@@ -475,7 +496,7 @@ describe('renderer/utils/helpers.ts', () => {
475496
url: null,
476497
latest_comment_url: null,
477498
type: 'Issue' as SubjectType,
478-
};
499+
} as Subject;
479500

480501
const result = await generateGitHubWebUrl({
481502
...mockSingleNotification,
@@ -494,7 +515,7 @@ describe('renderer/utils/helpers.ts', () => {
494515
url: null,
495516
latest_comment_url: null,
496517
type: 'PullRequest' as SubjectType,
497-
};
518+
} as Subject;
498519

499520
const result = await generateGitHubWebUrl({
500521
...mockSingleNotification,
@@ -513,7 +534,7 @@ describe('renderer/utils/helpers.ts', () => {
513534
url: null,
514535
latest_comment_url: null,
515536
type: 'Commit' as SubjectType,
516-
};
537+
} as Subject;
517538

518539
const result = await generateGitHubWebUrl({
519540
...mockSingleNotification,
@@ -536,7 +557,7 @@ describe('renderer/utils/helpers.ts', () => {
536557
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/1' as Link,
537558
latest_comment_url: null as Link,
538559
type: 'Issue' as SubjectType,
539-
};
560+
} as Subject;
540561

541562
getHtmlUrlSpy.mockRejectedValue(new Error('Test error'));
542563

src/renderer/utils/helpers.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,32 @@ export function actionsURL(repositoryURL: string, filters: string[]): Link {
4747
export async function generateGitHubWebUrl(
4848
notification: Notification,
4949
): Promise<Link> {
50-
const url = new URL(notification.repository.html_url);
50+
const handler = createNotificationHandler(notification);
51+
const url = new URL(handler.defaultUrl(notification));
5152

52-
try {
53-
if (notification.subject.htmlUrl) {
54-
url.href = notification.subject.htmlUrl;
55-
} else if (notification.subject.latest_comment_url) {
56-
url.href = await getHtmlUrl(
57-
notification.subject.latest_comment_url,
58-
notification.account.token,
53+
if (notification.subject.htmlUrl) {
54+
url.href = notification.subject.htmlUrl;
55+
} else {
56+
try {
57+
if (notification.subject.latest_comment_url) {
58+
url.href = await getHtmlUrl(
59+
notification.subject.latest_comment_url,
60+
notification.account.token,
61+
);
62+
} else if (notification.subject.url) {
63+
url.href = await getHtmlUrl(
64+
notification.subject.url,
65+
notification.account.token,
66+
);
67+
}
68+
} catch (err) {
69+
rendererLogError(
70+
'generateGitHubWebUrl',
71+
'Failed to resolve specific notification html url for',
72+
err,
73+
notification,
5974
);
60-
} else if (notification.subject.url) {
61-
url.href = await getHtmlUrl(
62-
notification.subject.url,
63-
notification.account.token,
64-
);
65-
} else {
66-
const handler = createNotificationHandler(notification);
67-
handler.defaultUrl(notification);
6875
}
69-
} catch (err) {
70-
rendererLogError(
71-
'generateGitHubWebUrl',
72-
'Failed to resolve specific notification html url for',
73-
err,
74-
notification,
75-
);
7676
}
7777

7878
url.searchParams.set(

0 commit comments

Comments
 (0)