Skip to content

Commit c3783e8

Browse files
committed
refactor: notification handlers
Signed-off-by: Adam Setch <[email protected]>
1 parent 121305a commit c3783e8

File tree

10 files changed

+47
-12
lines changed

10 files changed

+47
-12
lines changed

src/renderer/__helpers__/jest.setup.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import '@testing-library/jest-dom';
22
import { TextDecoder, TextEncoder } from 'node:util';
33

4-
import axios from 'axios';
5-
6-
/**
7-
* axios will default to using the XHR adapter which can't be intercepted
8-
* by nock. So, configure axios to use the node adapter.
9-
*/
10-
axios.defaults.adapter = 'http';
11-
124
/**
135
* Prevent the following errors with jest:
146
* - ReferenceError: TextEncoder is not defined

src/renderer/utils/auth/utils.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ipcRenderer } from 'electron';
22

33
import type { AxiosPromise, AxiosResponse } from 'axios';
4+
import axios from 'axios';
45
import nock from 'nock';
56

67
import {
@@ -168,6 +169,10 @@ describe('renderer/utils/auth/utils.ts', () => {
168169
mockAuthState = {
169170
accounts: [],
170171
};
172+
173+
// axios will default to using the XHR adapter which can't be intercepted
174+
// by nock. So, configure axios to use the node adapter.
175+
axios.defaults.adapter = 'http';
171176
});
172177

173178
describe('should add GitHub Cloud account', () => {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios';
12
import nock from 'nock';
23

34
import { createSubjectMock } from '../../../__mocks__/notifications-mocks';
@@ -14,6 +15,12 @@ describe('renderer/utils/notifications/handlers/commit.ts', () => {
1415
const mockAuthor = partialMockUser('some-author');
1516
const mockCommenter = partialMockUser('some-commenter');
1617

18+
beforeEach(() => {
19+
// axios will default to using the XHR adapter which can't be intercepted
20+
// by nock. So, configure axios to use the node adapter.
21+
axios.defaults.adapter = 'http';
22+
});
23+
1724
it('get commit commenter', async () => {
1825
const mockNotification = partialMockNotification({
1926
title: 'This is a commit with comments',

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios';
12
import nock from 'nock';
23

34
import { createSubjectMock } from '../../../__mocks__/notifications-mocks';
@@ -34,6 +35,12 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
3435
...(partialRepository as Repository),
3536
};
3637

38+
beforeEach(() => {
39+
// axios will default to using the XHR adapter which can't be intercepted
40+
// by nock. So, configure axios to use the node adapter.
41+
axios.defaults.adapter = 'http';
42+
});
43+
3744
it('answered discussion state', async () => {
3845
nock('https://api.github.com')
3946
.post('/graphql')

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { workflowRunHandler } from './workflowRun';
1414

1515
export function createNotificationHandler(
1616
notification: Notification,
17-
): NotificationTypeHandler | null {
17+
): NotificationTypeHandler {
1818
switch (notification.subject.type) {
1919
case 'CheckSuite':
2020
return checkSuiteHandler;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios';
12
import nock from 'nock';
23

34
import { createSubjectMock } from '../../../__mocks__/notifications-mocks';
@@ -25,6 +26,10 @@ describe('renderer/utils/notifications/handlers/issue.ts', () => {
2526
latest_comment_url:
2627
'https://api.github.com/repos/gitify-app/notifications-test/issues/comments/302888448' as Link,
2728
});
29+
30+
// axios will default to using the XHR adapter which can't be intercepted
31+
// by nock. So, configure axios to use the node adapter.
32+
axios.defaults.adapter = 'http';
2833
});
2934

3035
it('open issue state', async () => {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios';
12
import nock from 'nock';
23

34
import { createSubjectMock } from '../../../__mocks__/notifications-mocks';
@@ -31,6 +32,12 @@ describe('renderer/utils/notifications/handlers/pullRequest.ts', () => {
3132
const mockAuthor = partialMockUser('some-author');
3233
const mockCommenter = partialMockUser('some-commenter');
3334

35+
beforeEach(() => {
36+
// axios will default to using the XHR adapter which can't be intercepted
37+
// by nock. So, configure axios to use the node adapter.
38+
axios.defaults.adapter = 'http';
39+
});
40+
3441
it('closed pull request state', async () => {
3542
nock('https://api.github.com')
3643
.get('/repos/gitify-app/notifications-test/pulls/1')

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios';
12
import nock from 'nock';
23

34
import { createSubjectMock } from '../../../__mocks__/notifications-mocks';
@@ -13,6 +14,12 @@ describe('renderer/utils/notifications/handlers/release.ts', () => {
1314
describe('enrich', () => {
1415
const mockAuthor = partialMockUser('some-author');
1516

17+
beforeEach(() => {
18+
// axios will default to using the XHR adapter which can't be intercepted
19+
// by nock. So, configure axios to use the node adapter.
20+
axios.defaults.adapter = 'http';
21+
});
22+
1623
it('release notification', async () => {
1724
const mockNotification = partialMockNotification({
1825
title: 'This is a mock release',

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios';
12
import nock from 'nock';
23

34
import * as logger from '../../../shared/logger';
@@ -9,6 +10,12 @@ import type { Repository } from '../../typesGitHub';
910
import { enrichNotification, getNotificationCount } from './notifications';
1011

1112
describe('renderer/utils/notifications/notifications.ts', () => {
13+
beforeEach(() => {
14+
// axios will default to using the XHR adapter which can't be intercepted
15+
// by nock. So, configure axios to use the node adapter.
16+
axios.defaults.adapter = 'http';
17+
});
18+
1219
afterEach(() => {
1320
jest.clearAllMocks();
1421
});

src/renderer/utils/notifications/notifications.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ export async function enrichNotification(
120120

121121
try {
122122
const handler = createNotificationHandler(notification);
123-
if (handler) {
124-
additionalSubjectDetails = await handler.enrich(notification, settings);
125-
}
123+
additionalSubjectDetails = await handler.enrich(notification, settings);
126124
} catch (err) {
127125
logError(
128126
'enrichNotification',

0 commit comments

Comments
 (0)