Skip to content

Commit b9ca474

Browse files
committed
refactor: test suites
Signed-off-by: Adam Setch <[email protected]>
1 parent 8ff3676 commit b9ca474

File tree

3 files changed

+56
-76
lines changed

3 files changed

+56
-76
lines changed

src/renderer/context/App.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ describe('renderer/context/App.tsx', () => {
208208
});
209209

210210
describe('authentication methods', () => {
211-
const mockApiRequestAuth = jest.spyOn(apiRequests, 'apiRequestAuth');
211+
const apiRequestAuthSpy = jest.spyOn(apiRequests, 'apiRequestAuth');
212212
const mockFetchNotifications = jest.fn();
213213

214214
beforeEach(() => {
@@ -222,7 +222,7 @@ describe('renderer/context/App.tsx', () => {
222222
});
223223

224224
it('should call loginWithPersonalAccessToken', async () => {
225-
mockApiRequestAuth.mockResolvedValueOnce(null);
225+
apiRequestAuthSpy.mockResolvedValueOnce(null);
226226

227227
const TestComponent = () => {
228228
const { loginWithPersonalAccessToken } = useContext(AppContext);
@@ -254,8 +254,8 @@ describe('renderer/context/App.tsx', () => {
254254
expect(mockFetchNotifications).toHaveBeenCalledTimes(1),
255255
);
256256

257-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(1);
258-
expect(mockApiRequestAuth).toHaveBeenCalledWith(
257+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
258+
expect(apiRequestAuthSpy).toHaveBeenCalledWith(
259259
'https://api.github.com/notifications',
260260
'HEAD',
261261
'encrypted',
@@ -312,7 +312,7 @@ describe('renderer/context/App.tsx', () => {
312312
});
313313

314314
it('should call updateSetting and set auto launch(openAtStartup)', async () => {
315-
const mockSetAutoLaunch = jest.spyOn(comms, 'setAutoLaunch');
315+
const setAutoLaunchSpy = jest.spyOn(comms, 'setAutoLaunch');
316316
const mockSaveState = jest
317317
.spyOn(storage, 'saveState')
318318
.mockImplementation(jest.fn());
@@ -340,7 +340,7 @@ describe('renderer/context/App.tsx', () => {
340340
fireEvent.click(getByText('Test Case'));
341341
});
342342

343-
expect(mockSetAutoLaunch).toHaveBeenCalledWith(true);
343+
expect(setAutoLaunchSpy).toHaveBeenCalledWith(true);
344344

345345
expect(mockSaveState).toHaveBeenCalledWith({
346346
auth: {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AxiosPromise, AxiosResponse } from 'axios';
1+
import type { AxiosResponse } from 'axios';
22
import axios from 'axios';
33
import nock from 'nock';
44

@@ -115,11 +115,11 @@ describe('renderer/utils/auth/utils.ts', () => {
115115
const apiRequestSpy = jest.spyOn(apiRequests, 'apiRequest');
116116

117117
it('should get a token', async () => {
118-
const requestPromise = new Promise((resolve) =>
119-
resolve({ data: { access_token: 'this-is-a-token' } } as AxiosResponse),
120-
) as AxiosPromise;
121-
122-
apiRequestSpy.mockResolvedValueOnce(requestPromise);
118+
apiRequestSpy.mockResolvedValueOnce(
119+
Promise.resolve({
120+
data: { access_token: 'this-is-a-token' },
121+
} as AxiosResponse),
122+
);
123123

124124
const res = await authUtils.getToken(authCode);
125125

src/renderer/utils/helpers.test.ts

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

7-
import type { AxiosPromise, AxiosResponse } from 'axios';
7+
import type { AxiosResponse } from 'axios';
88

99
import { mockPersonalAccessTokenAccount } from '../__mocks__/account-mocks';
1010
import type { Hostname, Link } from '../types';
@@ -74,7 +74,7 @@ describe('renderer/utils/helpers.ts', () => {
7474
'https://github.com/gitify-app/notifications-test/issues/785';
7575
const mockNotificationReferrer =
7676
'notification_referrer_id=MDE4Ok5vdGlmaWNhdGlvblRocmVhZDEzODY2MTA5NjoxMjM0NTY3ODk%3D';
77-
const mockApiRequestAuth = jest.spyOn(apiRequests, 'apiRequestAuth');
77+
const apiRequestAuthSpy = jest.spyOn(apiRequests, 'apiRequestAuth');
7878

7979
afterEach(() => {
8080
jest.clearAllMocks();
@@ -89,23 +89,19 @@ describe('renderer/utils/helpers.ts', () => {
8989
type: 'Issue' as SubjectType,
9090
};
9191

92-
const requestPromise = new Promise((resolve) =>
93-
resolve({
94-
data: {
95-
html_url: mockHtmlUrl,
96-
},
97-
} as AxiosResponse),
98-
) as AxiosPromise;
99-
100-
mockApiRequestAuth.mockResolvedValue(requestPromise);
92+
apiRequestAuthSpy.mockResolvedValue({
93+
data: {
94+
html_url: mockHtmlUrl,
95+
},
96+
} as AxiosResponse);
10197

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

107-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(1);
108-
expect(mockApiRequestAuth).toHaveBeenCalledWith(
103+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
104+
expect(apiRequestAuthSpy).toHaveBeenCalledWith(
109105
subject.latest_comment_url,
110106
'GET',
111107
mockPersonalAccessTokenAccount.token,
@@ -121,23 +117,19 @@ describe('renderer/utils/helpers.ts', () => {
121117
type: 'Issue' as SubjectType,
122118
};
123119

124-
const requestPromise = new Promise((resolve) =>
125-
resolve({
126-
data: {
127-
html_url: mockHtmlUrl,
128-
},
129-
} as AxiosResponse),
130-
) as AxiosPromise;
131-
132-
mockApiRequestAuth.mockResolvedValue(requestPromise);
120+
apiRequestAuthSpy.mockResolvedValue({
121+
data: {
122+
html_url: mockHtmlUrl,
123+
},
124+
} as AxiosResponse);
133125

134126
const result = await generateGitHubWebUrl({
135127
...mockSingleNotification,
136128
subject: subject,
137129
});
138130

139-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(1);
140-
expect(mockApiRequestAuth).toHaveBeenCalledWith(
131+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
132+
expect(apiRequestAuthSpy).toHaveBeenCalledWith(
141133
subject.url,
142134
'GET',
143135
mockPersonalAccessTokenAccount.token,
@@ -159,7 +151,7 @@ describe('renderer/utils/helpers.ts', () => {
159151
subject: subject,
160152
});
161153

162-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
154+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
163155
expect(result).toBe(
164156
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Asuccess+branch%3Amain&${mockNotificationReferrer}`,
165157
);
@@ -178,7 +170,7 @@ describe('renderer/utils/helpers.ts', () => {
178170
subject: subject,
179171
});
180172

181-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
173+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
182174
expect(result).toBe(
183175
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Afailure+branch%3Amain&${mockNotificationReferrer}`,
184176
);
@@ -197,7 +189,7 @@ describe('renderer/utils/helpers.ts', () => {
197189
subject: subject,
198190
});
199191

200-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
192+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
201193
expect(result).toBe(
202194
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Afailure+branch%3Amain&${mockNotificationReferrer}`,
203195
);
@@ -216,7 +208,7 @@ describe('renderer/utils/helpers.ts', () => {
216208
subject: subject,
217209
});
218210

219-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
211+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
220212
expect(result).toBe(
221213
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+is%3Askipped+branch%3Amain&${mockNotificationReferrer}`,
222214
);
@@ -235,7 +227,7 @@ describe('renderer/utils/helpers.ts', () => {
235227
subject: subject,
236228
});
237229

238-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
230+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
239231
expect(result).toBe(
240232
`https://github.com/gitify-app/notifications-test/actions?${mockNotificationReferrer}`,
241233
);
@@ -254,7 +246,7 @@ describe('renderer/utils/helpers.ts', () => {
254246
subject: subject,
255247
});
256248

257-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
249+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
258250
expect(result).toBe(
259251
`https://github.com/gitify-app/notifications-test/actions?query=workflow%3A%22Demo%22+branch%3Amain&${mockNotificationReferrer}`,
260252
);
@@ -273,7 +265,7 @@ describe('renderer/utils/helpers.ts', () => {
273265
subject: subject,
274266
});
275267

276-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
268+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
277269
expect(result).toBe(
278270
`https://github.com/gitify-app/notifications-test/actions?${mockNotificationReferrer}`,
279271
);
@@ -289,20 +281,16 @@ describe('renderer/utils/helpers.ts', () => {
289281
type: 'Discussion' as SubjectType,
290282
};
291283

292-
const requestPromise = new Promise((resolve) =>
293-
resolve({
294-
data: { data: { search: { nodes: [] } } },
295-
} as AxiosResponse),
296-
) as AxiosPromise;
297-
298-
mockApiRequestAuth.mockResolvedValue(requestPromise);
284+
apiRequestAuthSpy.mockResolvedValue({
285+
data: { data: { search: { nodes: [] } } },
286+
} as AxiosResponse);
299287

300288
const result = await generateGitHubWebUrl({
301289
...mockSingleNotification,
302290
subject: subject,
303291
});
304292

305-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(1);
293+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
306294
expect(result).toBe(
307295
`${mockSingleNotification.repository.html_url}/discussions?${mockNotificationReferrer}`,
308296
);
@@ -316,22 +304,18 @@ describe('renderer/utils/helpers.ts', () => {
316304
type: 'Discussion' as SubjectType,
317305
};
318306

319-
const requestPromise = new Promise((resolve) =>
320-
resolve({
321-
data: {
322-
...mockGraphQLResponse,
323-
},
324-
} as AxiosResponse),
325-
) as AxiosPromise;
326-
327-
mockApiRequestAuth.mockResolvedValue(requestPromise);
307+
apiRequestAuthSpy.mockResolvedValue({
308+
data: {
309+
...mockGraphQLResponse,
310+
},
311+
} as AxiosResponse);
328312

329313
const result = await generateGitHubWebUrl({
330314
...mockSingleNotification,
331315
subject: subject,
332316
});
333317

334-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(1);
318+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
335319
expect(result).toBe(
336320
`https://github.com/gitify-app/notifications-test/discussions/612?${mockNotificationReferrer}#discussioncomment-2300902`,
337321
);
@@ -349,18 +333,14 @@ describe('renderer/utils/helpers.ts', () => {
349333
type: 'Discussion' as SubjectType,
350334
};
351335

352-
const requestPromise = new Promise((resolve) =>
353-
resolve(null as AxiosResponse),
354-
) as AxiosPromise;
355-
356-
mockApiRequestAuth.mockResolvedValue(requestPromise);
336+
apiRequestAuthSpy.mockResolvedValue(null as AxiosResponse);
357337

358338
const result = await generateGitHubWebUrl({
359339
...mockSingleNotification,
360340
subject: subject,
361341
});
362342

363-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(1);
343+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
364344
expect(result).toBe(
365345
`https://github.com/gitify-app/notifications-test/discussions?${mockNotificationReferrer}`,
366346
);
@@ -382,7 +362,7 @@ describe('renderer/utils/helpers.ts', () => {
382362
subject: subject,
383363
});
384364

385-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
365+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
386366
expect(result).toBe(
387367
`https://github.com/gitify-app/notifications-test/invitations?${mockNotificationReferrer}`,
388368
);
@@ -401,7 +381,7 @@ describe('renderer/utils/helpers.ts', () => {
401381
subject: subject,
402382
});
403383

404-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
384+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
405385
expect(result).toBe(
406386
`https://github.com/gitify-app/notifications-test/security/dependabot?${mockNotificationReferrer}`,
407387
);
@@ -421,7 +401,7 @@ describe('renderer/utils/helpers.ts', () => {
421401
subject: subject,
422402
});
423403

424-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
404+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
425405
expect(result).toBe(
426406
`https://github.com/gitify-app/notifications-test/actions?query=is%3Awaiting&${mockNotificationReferrer}`,
427407
);
@@ -441,7 +421,7 @@ describe('renderer/utils/helpers.ts', () => {
441421
subject: subject,
442422
});
443423

444-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
424+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
445425
expect(result).toBe(
446426
`https://github.com/gitify-app/notifications-test/actions?${mockNotificationReferrer}`,
447427
);
@@ -460,7 +440,7 @@ describe('renderer/utils/helpers.ts', () => {
460440
subject: subject,
461441
});
462442

463-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
443+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
464444
expect(result).toBe(
465445
`https://github.com/gitify-app/notifications-test/actions?${mockNotificationReferrer}`,
466446
);
@@ -481,7 +461,7 @@ describe('renderer/utils/helpers.ts', () => {
481461
subject: subject,
482462
});
483463

484-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(0);
464+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
485465
expect(result).toBe(
486466
`${mockSingleNotification.repository.html_url}?${mockNotificationReferrer}`,
487467
);
@@ -502,15 +482,15 @@ describe('renderer/utils/helpers.ts', () => {
502482

503483
const mockError = new Error('Test error');
504484

505-
mockApiRequestAuth.mockRejectedValue(mockError);
485+
apiRequestAuthSpy.mockRejectedValue(mockError);
506486

507487
const result = await generateGitHubWebUrl({
508488
...mockSingleNotification,
509489
subject: subject,
510490
});
511491

512-
expect(mockApiRequestAuth).toHaveBeenCalledTimes(1);
513-
expect(mockApiRequestAuth).toHaveBeenCalledWith(
492+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
493+
expect(apiRequestAuthSpy).toHaveBeenCalledWith(
514494
subject.latest_comment_url,
515495
'GET',
516496
mockPersonalAccessTokenAccount.token,

0 commit comments

Comments
 (0)