Skip to content

Commit 261a85f

Browse files
authored
test: api client test cases (#2366)
tests: api client test cases Signed-off-by: Adam Setch <[email protected]>
1 parent eb45038 commit 261a85f

File tree

5 files changed

+131
-249
lines changed

5 files changed

+131
-249
lines changed

src/renderer/components/metrics/MetricGroup.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const MetricGroup: FC<IMetricGroup> = ({
6262
/>
6363
);
6464
})}
65+
6566
{notification.subject?.comments > 0 && (
6667
<MetricPill
6768
color={IconColor.GRAY}
@@ -70,6 +71,7 @@ export const MetricGroup: FC<IMetricGroup> = ({
7071
title={commentsPillDescription}
7172
/>
7273
)}
74+
7375
{notification.subject?.labels?.length > 0 && (
7476
<MetricPill
7577
color={IconColor.GRAY}
@@ -78,6 +80,7 @@ export const MetricGroup: FC<IMetricGroup> = ({
7880
title={labelsPillDescription}
7981
/>
8082
)}
83+
8184
{notification.subject.milestone && (
8285
<MetricPill
8386
color={
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const mockNoAuthHeaders = {
2+
Accept: 'application/json',
3+
'Cache-Control': '',
4+
'Content-Type': 'application/json',
5+
};
6+
7+
export const mockAuthHeaders = {
8+
Accept: 'application/json',
9+
Authorization: 'token decrypted',
10+
'Cache-Control': '',
11+
'Content-Type': 'application/json',
12+
};
13+
14+
export const mockNonCachedAuthHeaders = {
15+
Accept: 'application/json',
16+
Authorization: 'token decrypted',
17+
'Cache-Control': 'no-cache',
18+
'Content-Type': 'application/json',
19+
};

src/renderer/utils/api/client.test.ts

Lines changed: 61 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import axios, { type AxiosPromise, type AxiosResponse } from 'axios';
22

3-
import {
4-
mockGitHubCloudAccount,
5-
mockGitHubEnterpriseServerAccount,
6-
mockToken,
7-
} from '../../__mocks__/state-mocks';
3+
import { mockGitHubCloudAccount, mockToken } from '../../__mocks__/state-mocks';
84
import type { Hostname, Link, SettingsState, Token } from '../../types';
95
import * as logger from '../../utils/logger';
6+
import {
7+
mockAuthHeaders,
8+
mockNonCachedAuthHeaders,
9+
} from './__mocks__/request-mocks';
1010
import {
1111
getAuthenticatedUser,
1212
getHtmlUrl,
@@ -21,87 +21,39 @@ import * as apiRequests from './request';
2121
jest.mock('axios');
2222

2323
const mockGitHubHostname = 'github.com' as Hostname;
24-
const mockEnterpriseHostname = 'example.com' as Hostname;
2524
const mockThreadId = '1234';
2625

2726
describe('renderer/utils/api/client.ts', () => {
2827
afterEach(() => {
2928
jest.clearAllMocks();
3029
});
3130

32-
describe('getAuthenticatedUser', () => {
33-
it('should fetch authenticated user - github', async () => {
34-
await getAuthenticatedUser(mockGitHubHostname, mockToken);
35-
36-
expect(axios).toHaveBeenCalledWith({
37-
url: 'https://api.github.com/user',
38-
headers: {
39-
Accept: 'application/json',
40-
Authorization: 'token decrypted',
41-
'Cache-Control': '',
42-
'Content-Type': 'application/json',
43-
},
44-
method: 'GET',
45-
data: {},
46-
});
47-
});
48-
49-
it('should fetch authenticated user - enterprise', async () => {
50-
await getAuthenticatedUser(mockEnterpriseHostname, mockToken);
31+
it('getAuthenticatedUser - should fetch authenticated user', async () => {
32+
await getAuthenticatedUser(mockGitHubHostname, mockToken);
5133

52-
expect(axios).toHaveBeenCalledWith({
53-
url: 'https://example.com/api/v3/user',
54-
headers: {
55-
Accept: 'application/json',
56-
Authorization: 'token decrypted',
57-
'Cache-Control': '',
58-
'Content-Type': 'application/json',
59-
},
60-
method: 'GET',
61-
data: {},
62-
});
34+
expect(axios).toHaveBeenCalledWith({
35+
url: 'https://api.github.com/user',
36+
headers: mockAuthHeaders,
37+
method: 'GET',
38+
data: {},
6339
});
6440
});
6541

66-
describe('headNotifications', () => {
67-
it('should fetch notifications head - github', async () => {
68-
await headNotifications(mockGitHubHostname, mockToken);
69-
70-
expect(axios).toHaveBeenCalledWith({
71-
url: 'https://api.github.com/notifications',
72-
headers: {
73-
Accept: 'application/json',
74-
Authorization: 'token decrypted',
75-
'Cache-Control': 'no-cache',
76-
'Content-Type': 'application/json',
77-
},
78-
method: 'HEAD',
79-
data: {},
80-
});
81-
});
82-
83-
it('should fetch notifications head - enterprise', async () => {
84-
await headNotifications(mockEnterpriseHostname, mockToken);
42+
it('headNotifications - should fetch notifications head', async () => {
43+
await headNotifications(mockGitHubHostname, mockToken);
8544

86-
expect(axios).toHaveBeenCalledWith({
87-
url: 'https://example.com/api/v3/notifications',
88-
headers: {
89-
Accept: 'application/json',
90-
Authorization: 'token decrypted',
91-
'Cache-Control': 'no-cache',
92-
'Content-Type': 'application/json',
93-
},
94-
method: 'HEAD',
95-
data: {},
96-
});
45+
expect(axios).toHaveBeenCalledWith({
46+
url: 'https://api.github.com/notifications',
47+
headers: mockNonCachedAuthHeaders,
48+
method: 'HEAD',
49+
data: {},
9750
});
9851
});
9952

10053
describe('listNotificationsForAuthenticatedUser', () => {
101-
it('should list notifications for user - github cloud - fetchAllNotifications true', async () => {
54+
it('should list only participating notifications for user', async () => {
10255
const mockSettings: Partial<SettingsState> = {
10356
participating: true,
104-
fetchAllNotifications: true,
10557
};
10658

10759
await listNotificationsForAuthenticatedUser(
@@ -111,21 +63,15 @@ describe('renderer/utils/api/client.ts', () => {
11163

11264
expect(axios).toHaveBeenCalledWith({
11365
url: 'https://api.github.com/notifications?participating=true',
114-
headers: {
115-
Accept: 'application/json',
116-
Authorization: 'token decrypted',
117-
'Cache-Control': 'no-cache',
118-
'Content-Type': 'application/json',
119-
},
66+
headers: mockNonCachedAuthHeaders,
12067
method: 'GET',
12168
data: {},
12269
});
12370
});
12471

125-
it('should list notifications for user - github cloud - fetchAllNotifications false', async () => {
72+
it('should list participating and watching notifications for user', async () => {
12673
const mockSettings: Partial<SettingsState> = {
127-
participating: true,
128-
fetchAllNotifications: false,
74+
participating: false,
12975
};
13076

13177
await listNotificationsForAuthenticatedUser(
@@ -134,165 +80,56 @@ describe('renderer/utils/api/client.ts', () => {
13480
);
13581

13682
expect(axios).toHaveBeenCalledWith({
137-
url: 'https://api.github.com/notifications?participating=true',
138-
headers: {
139-
Accept: 'application/json',
140-
Authorization: 'token decrypted',
141-
'Cache-Control': 'no-cache',
142-
'Content-Type': 'application/json',
143-
},
144-
method: 'GET',
145-
data: {},
146-
});
147-
});
148-
149-
it('should list notifications for user - github enterprise server', async () => {
150-
const mockSettings: Partial<SettingsState> = {
151-
participating: true,
152-
};
153-
154-
await listNotificationsForAuthenticatedUser(
155-
mockGitHubEnterpriseServerAccount,
156-
mockSettings as SettingsState,
157-
);
158-
159-
expect(axios).toHaveBeenCalledWith({
160-
url: 'https://github.gitify.io/api/v3/notifications?participating=true',
161-
headers: {
162-
Accept: 'application/json',
163-
Authorization: 'token decrypted',
164-
'Cache-Control': 'no-cache',
165-
'Content-Type': 'application/json',
166-
},
83+
url: 'https://api.github.com/notifications?participating=false',
84+
headers: mockNonCachedAuthHeaders,
16785
method: 'GET',
16886
data: {},
16987
});
17088
});
17189
});
17290

173-
describe('markNotificationThreadAsRead', () => {
174-
it('should mark notification thread as read - github', async () => {
175-
await markNotificationThreadAsRead(
176-
mockThreadId,
177-
mockGitHubHostname,
178-
mockToken,
179-
);
180-
181-
expect(axios).toHaveBeenCalledWith({
182-
url: `https://api.github.com/notifications/threads/${mockThreadId}`,
183-
headers: {
184-
Accept: 'application/json',
185-
Authorization: 'token decrypted',
186-
'Cache-Control': '',
187-
'Content-Type': 'application/json',
188-
},
189-
method: 'PATCH',
190-
data: {},
191-
});
192-
});
193-
194-
it('should mark notification thread as read - enterprise', async () => {
195-
await markNotificationThreadAsRead(
196-
mockThreadId,
197-
mockEnterpriseHostname,
198-
mockToken,
199-
);
200-
201-
expect(axios).toHaveBeenCalledWith({
202-
url: `https://example.com/api/v3/notifications/threads/${mockThreadId}`,
203-
headers: {
204-
Accept: 'application/json',
205-
Authorization: 'token decrypted',
206-
'Cache-Control': '',
207-
'Content-Type': 'application/json',
208-
},
209-
method: 'PATCH',
210-
data: {},
211-
});
91+
it('markNotificationThreadAsRead - should mark notification thread as read', async () => {
92+
await markNotificationThreadAsRead(
93+
mockThreadId,
94+
mockGitHubHostname,
95+
mockToken,
96+
);
97+
98+
expect(axios).toHaveBeenCalledWith({
99+
url: `https://api.github.com/notifications/threads/${mockThreadId}`,
100+
headers: mockAuthHeaders,
101+
method: 'PATCH',
102+
data: {},
212103
});
213104
});
214105

215-
describe('markNotificationThreadAsDone', () => {
216-
it('should mark notification thread as done - github', async () => {
217-
await markNotificationThreadAsDone(
218-
mockThreadId,
219-
mockGitHubHostname,
220-
mockToken,
221-
);
222-
223-
expect(axios).toHaveBeenCalledWith({
224-
url: `https://api.github.com/notifications/threads/${mockThreadId}`,
225-
headers: {
226-
Accept: 'application/json',
227-
Authorization: 'token decrypted',
228-
'Cache-Control': '',
229-
'Content-Type': 'application/json',
230-
},
231-
method: 'DELETE',
232-
data: {},
233-
});
234-
});
235-
236-
it('should mark notification thread as done - enterprise', async () => {
237-
await markNotificationThreadAsDone(
238-
mockThreadId,
239-
mockEnterpriseHostname,
240-
mockToken,
241-
);
242-
243-
expect(axios).toHaveBeenCalledWith({
244-
url: `https://example.com/api/v3/notifications/threads/${mockThreadId}`,
245-
headers: {
246-
Accept: 'application/json',
247-
Authorization: 'token decrypted',
248-
'Cache-Control': '',
249-
'Content-Type': 'application/json',
250-
},
251-
method: 'DELETE',
252-
data: {},
253-
});
106+
it('markNotificationThreadAsDone - should mark notification thread as done', async () => {
107+
await markNotificationThreadAsDone(
108+
mockThreadId,
109+
mockGitHubHostname,
110+
mockToken,
111+
);
112+
113+
expect(axios).toHaveBeenCalledWith({
114+
url: `https://api.github.com/notifications/threads/${mockThreadId}`,
115+
headers: mockAuthHeaders,
116+
method: 'DELETE',
117+
data: {},
254118
});
255119
});
256120

257-
describe('ignoreNotificationThreadSubscription', () => {
258-
it('should ignore notification thread subscription - github', async () => {
259-
await ignoreNotificationThreadSubscription(
260-
mockThreadId,
261-
mockGitHubHostname,
262-
mockToken,
263-
);
264-
265-
expect(axios).toHaveBeenCalledWith({
266-
url: `https://api.github.com/notifications/threads/${mockThreadId}/subscription`,
267-
headers: {
268-
Accept: 'application/json',
269-
Authorization: 'token decrypted',
270-
'Cache-Control': '',
271-
'Content-Type': 'application/json',
272-
},
273-
method: 'PUT',
274-
data: { ignored: true },
275-
});
276-
});
277-
278-
it('should ignore notification thread subscription - enterprise', async () => {
279-
await ignoreNotificationThreadSubscription(
280-
mockThreadId,
281-
mockEnterpriseHostname,
282-
mockToken,
283-
);
284-
285-
expect(axios).toHaveBeenCalledWith({
286-
url: `https://example.com/api/v3/notifications/threads/${mockThreadId}/subscription`,
287-
headers: {
288-
Accept: 'application/json',
289-
Authorization: 'token decrypted',
290-
'Cache-Control': '',
291-
'Content-Type': 'application/json',
292-
},
293-
method: 'PUT',
294-
data: { ignored: true },
295-
});
121+
it('ignoreNotificationThreadSubscription - should ignore notification thread subscription', async () => {
122+
await ignoreNotificationThreadSubscription(
123+
mockThreadId,
124+
mockGitHubHostname,
125+
mockToken,
126+
);
127+
128+
expect(axios).toHaveBeenCalledWith({
129+
url: `https://api.github.com/notifications/threads/${mockThreadId}/subscription`,
130+
headers: mockAuthHeaders,
131+
method: 'PUT',
132+
data: { ignored: true },
296133
});
297134
});
298135

0 commit comments

Comments
 (0)