Skip to content

Commit a866f88

Browse files
committed
Merge branch 'main' into feat/codegen-discussions
Signed-off-by: Adam Setch <[email protected]>
2 parents e03f6ac + a901224 commit a866f88

File tree

2 files changed

+113
-89
lines changed

2 files changed

+113
-89
lines changed

src/renderer/utils/helpers.test.ts

Lines changed: 98 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import {
44
ChevronRightIcon,
55
} from '@primer/octicons-react';
66

7-
import type { AxiosResponse } from 'axios';
87
import type { ExecutionResult } from 'graphql';
98

10-
import { mockPersonalAccessTokenAccount } from '../__mocks__/account-mocks';
9+
import { createPartialMockNotification } from '../__mocks__/notifications-mocks';
1110
import type { Hostname, Link } from '../types';
1211
import type { SubjectType } from '../typesGitHub';
1312
import * as logger from '../utils/logger';
@@ -17,11 +16,11 @@ import {
1716
} from './api/__mocks__/response-mocks';
1817
import * as apiClient from './api/client';
1918
import type { FetchDiscussionByNumberQuery } from './api/graphql/generated/graphql';
20-
import * as apiRequests from './api/request';
2119
import {
2220
generateGitHubWebUrl,
2321
generateNotificationReferrerId,
2422
getChevronDetails,
23+
getDefaultURLForType,
2524
getPlatformFromHostname,
2625
isEnterpriseServerHost,
2726
} from './helpers';
@@ -77,7 +76,8 @@ describe('renderer/utils/helpers.ts', () => {
7776
'https://github.com/gitify-app/notifications-test/issues/785';
7877
const mockNotificationReferrer =
7978
'notification_referrer_id=MDE4Ok5vdGlmaWNhdGlvblRocmVhZDEzODY2MTA5NjoxMjM0NTY3ODk%3D';
80-
const apiRequestAuthSpy = jest.spyOn(apiRequests, 'apiRequestAuth');
79+
80+
const getHtmlUrlSpy = jest.spyOn(apiClient, 'getHtmlUrl');
8181

8282
afterEach(() => {
8383
jest.clearAllMocks();
@@ -92,23 +92,14 @@ describe('renderer/utils/helpers.ts', () => {
9292
type: 'Issue' as SubjectType,
9393
};
9494

95-
apiRequestAuthSpy.mockResolvedValue({
96-
data: {
97-
html_url: mockHtmlUrl,
98-
},
99-
} as AxiosResponse);
95+
getHtmlUrlSpy.mockResolvedValue(mockHtmlUrl);
10096

10197
const result = await generateGitHubWebUrl({
10298
...mockSingleNotification,
10399
subject: subject,
104100
});
105101

106-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
107-
expect(apiRequestAuthSpy).toHaveBeenCalledWith(
108-
subject.latest_comment_url,
109-
'GET',
110-
mockPersonalAccessTokenAccount.token,
111-
);
102+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(1);
112103
expect(result).toBe(`${mockHtmlUrl}?${mockNotificationReferrer}`);
113104
});
114105

@@ -120,23 +111,14 @@ describe('renderer/utils/helpers.ts', () => {
120111
type: 'Issue' as SubjectType,
121112
};
122113

123-
apiRequestAuthSpy.mockResolvedValue({
124-
data: {
125-
html_url: mockHtmlUrl,
126-
},
127-
} as AxiosResponse);
114+
getHtmlUrlSpy.mockResolvedValue(mockHtmlUrl);
128115

129116
const result = await generateGitHubWebUrl({
130117
...mockSingleNotification,
131118
subject: subject,
132119
});
133120

134-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
135-
expect(apiRequestAuthSpy).toHaveBeenCalledWith(
136-
subject.url,
137-
'GET',
138-
mockPersonalAccessTokenAccount.token,
139-
);
121+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(1);
140122
expect(result).toBe(`${mockHtmlUrl}?${mockNotificationReferrer}`);
141123
});
142124

@@ -154,7 +136,7 @@ describe('renderer/utils/helpers.ts', () => {
154136
subject: subject,
155137
});
156138

157-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
139+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
158140
expect(result).toBe(
159141
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Asuccess+branch%3Amain&${mockNotificationReferrer}`,
160142
);
@@ -173,7 +155,7 @@ describe('renderer/utils/helpers.ts', () => {
173155
subject: subject,
174156
});
175157

176-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
158+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
177159
expect(result).toBe(
178160
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Afailure+branch%3Amain&${mockNotificationReferrer}`,
179161
);
@@ -192,7 +174,7 @@ describe('renderer/utils/helpers.ts', () => {
192174
subject: subject,
193175
});
194176

195-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
177+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
196178
expect(result).toBe(
197179
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Afailure+branch%3Amain&${mockNotificationReferrer}`,
198180
);
@@ -211,7 +193,7 @@ describe('renderer/utils/helpers.ts', () => {
211193
subject: subject,
212194
});
213195

214-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
196+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
215197
expect(result).toBe(
216198
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Askipped+branch%3Amain&${mockNotificationReferrer}`,
217199
);
@@ -230,7 +212,7 @@ describe('renderer/utils/helpers.ts', () => {
230212
subject: subject,
231213
});
232214

233-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
215+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
234216
expect(result).toBe(
235217
`https://github.com/gitify-app/notifications-test/actions?${mockNotificationReferrer}`,
236218
);
@@ -249,7 +231,7 @@ describe('renderer/utils/helpers.ts', () => {
249231
subject: subject,
250232
});
251233

252-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
234+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
253235
expect(result).toBe(
254236
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+branch%3Amain&${mockNotificationReferrer}`,
255237
);
@@ -268,7 +250,7 @@ describe('renderer/utils/helpers.ts', () => {
268250
subject: subject,
269251
});
270252

271-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
253+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
272254
expect(result).toBe(
273255
`https://github.com/gitify-app/notifications-test/actions?${mockNotificationReferrer}`,
274256
);
@@ -403,7 +385,7 @@ describe('renderer/utils/helpers.ts', () => {
403385
subject: subject,
404386
});
405387

406-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
388+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
407389
expect(result).toBe(
408390
`https://github.com/gitify-app/notifications-test/invitations?${mockNotificationReferrer}`,
409391
);
@@ -422,7 +404,7 @@ describe('renderer/utils/helpers.ts', () => {
422404
subject: subject,
423405
});
424406

425-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
407+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
426408
expect(result).toBe(
427409
`https://github.com/gitify-app/notifications-test/security/dependabot?${mockNotificationReferrer}`,
428410
);
@@ -442,7 +424,7 @@ describe('renderer/utils/helpers.ts', () => {
442424
subject: subject,
443425
});
444426

445-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
427+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
446428
expect(result).toBe(
447429
`https://github.com/gitify-app/notifications-test/actions?query=is%3Awaiting&${mockNotificationReferrer}`,
448430
);
@@ -462,7 +444,7 @@ describe('renderer/utils/helpers.ts', () => {
462444
subject: subject,
463445
});
464446

465-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
447+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
466448
expect(result).toBe(
467449
`https://github.com/gitify-app/notifications-test/actions?${mockNotificationReferrer}`,
468450
);
@@ -481,7 +463,7 @@ describe('renderer/utils/helpers.ts', () => {
481463
subject: subject,
482464
});
483465

484-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
466+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
485467
expect(result).toBe(
486468
`https://github.com/gitify-app/notifications-test/actions?${mockNotificationReferrer}`,
487469
);
@@ -502,32 +484,13 @@ describe('renderer/utils/helpers.ts', () => {
502484
subject: subject,
503485
});
504486

505-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
487+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
506488
expect(result).toBe(
507489
`https://github.com/gitify-app/notifications-test/issues?${mockNotificationReferrer}`,
508490
);
509491
});
510492

511-
it('discussion - defaults when no urls present in notification', async () => {
512-
const subject = {
513-
title: 'generate github web url unit tests',
514-
url: null,
515-
latest_comment_url: null,
516-
type: 'Discussion' as SubjectType,
517-
};
518-
519-
const result = await generateGitHubWebUrl({
520-
...mockSingleNotification,
521-
subject: subject,
522-
});
523-
524-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
525-
expect(result).toBe(
526-
`https://github.com/gitify-app/notifications-test/discussions?${mockNotificationReferrer}`,
527-
);
528-
});
529-
530-
it('issues - defaults when no urls present in notification', async () => {
493+
it('pull requests - defaults when no urls present in notification', async () => {
531494
const subject = {
532495
title: 'generate github web url unit tests',
533496
url: null,
@@ -540,7 +503,7 @@ describe('renderer/utils/helpers.ts', () => {
540503
subject: subject,
541504
});
542505

543-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
506+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
544507
expect(result).toBe(
545508
`https://github.com/gitify-app/notifications-test/pulls?${mockNotificationReferrer}`,
546509
);
@@ -559,7 +522,7 @@ describe('renderer/utils/helpers.ts', () => {
559522
subject: subject,
560523
});
561524

562-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
525+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(0);
563526
expect(result).toBe(
564527
`https://github.com/gitify-app/notifications-test?${mockNotificationReferrer}`,
565528
);
@@ -573,32 +536,96 @@ describe('renderer/utils/helpers.ts', () => {
573536
const subject = {
574537
title: 'generate github web url unit tests',
575538
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/1' as Link,
576-
latest_comment_url:
577-
'https://api.github.com/repos/gitify-app/notifications-test/issues/comments/302888448' as Link,
539+
latest_comment_url: null as Link,
578540
type: 'Issue' as SubjectType,
579541
};
580542

581-
apiRequestAuthSpy.mockRejectedValue(new Error('Test error'));
543+
getHtmlUrlSpy.mockRejectedValue(new Error('Test error'));
582544

583545
const result = await generateGitHubWebUrl({
584546
...mockSingleNotification,
585547
subject: subject,
586548
});
587549

588-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
589-
expect(apiRequestAuthSpy).toHaveBeenCalledWith(
590-
subject.latest_comment_url,
591-
'GET',
592-
mockPersonalAccessTokenAccount.token,
593-
);
550+
expect(getHtmlUrlSpy).toHaveBeenCalledTimes(1);
594551
expect(result).toBe(
595552
`https://github.com/gitify-app/notifications-test/issues?${mockNotificationReferrer}`,
596553
);
597-
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(2);
554+
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);
598555
});
599556
});
600557
});
601558

559+
describe('getDefaultURLForType', () => {
560+
const mockUrl = 'https://github.com/gitify-app/notifications-test' as Link;
561+
562+
it('discussions', () => {
563+
const mockNotification = createPartialMockNotification(
564+
{ type: 'Discussion' },
565+
{ html_url: mockUrl },
566+
);
567+
568+
expect(getDefaultURLForType(mockNotification)).toEqual(
569+
'https://github.com/gitify-app/notifications-test/discussions',
570+
);
571+
});
572+
573+
it('issues', () => {
574+
const mockNotification = createPartialMockNotification(
575+
{ type: 'Issue' },
576+
{ html_url: mockUrl },
577+
);
578+
579+
expect(getDefaultURLForType(mockNotification)).toEqual(
580+
'https://github.com/gitify-app/notifications-test/issues',
581+
);
582+
});
583+
584+
it('pull requests', () => {
585+
const mockNotification = createPartialMockNotification(
586+
{ type: 'PullRequest' },
587+
{ html_url: mockUrl },
588+
);
589+
590+
expect(getDefaultURLForType(mockNotification)).toEqual(
591+
'https://github.com/gitify-app/notifications-test/pulls',
592+
);
593+
});
594+
595+
it('repository invitation', () => {
596+
const mockNotification = createPartialMockNotification(
597+
{ type: 'RepositoryInvitation' },
598+
{ html_url: mockUrl },
599+
);
600+
601+
expect(getDefaultURLForType(mockNotification)).toEqual(
602+
'https://github.com/gitify-app/notifications-test/invitations',
603+
);
604+
});
605+
606+
it('repository dependabot alert thread', () => {
607+
const mockNotification = createPartialMockNotification(
608+
{ type: 'RepositoryDependabotAlertsThread' },
609+
{ html_url: mockUrl },
610+
);
611+
612+
expect(getDefaultURLForType(mockNotification)).toEqual(
613+
'https://github.com/gitify-app/notifications-test/security/dependabot',
614+
);
615+
});
616+
617+
it('default web urls', () => {
618+
const mockNotification = createPartialMockNotification(
619+
{ type: 'Commit' },
620+
{ html_url: mockUrl },
621+
);
622+
623+
expect(getDefaultURLForType(mockNotification)).toEqual(
624+
'https://github.com/gitify-app/notifications-test',
625+
);
626+
});
627+
});
628+
602629
describe('getChevronDetails', () => {
603630
it('should return correct chevron details', () => {
604631
expect(getChevronDetails(true, true, 'account')).toEqual({

0 commit comments

Comments
 (0)