Skip to content

Commit d9c895c

Browse files
committed
test: fix test infrastructure after merge with main
1 parent 4b5a844 commit d9c895c

File tree

13 files changed

+76
-95
lines changed

13 files changed

+76
-95
lines changed

src/hooks/useNotifications.test.ts

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { vi } from 'vitest';
33
// Mock to use axios instead of Tauri HTTP plugin
44
vi.mock('../utils/environment', () => ({ isTauriEnvironment: () => false }));
55

6+
// Mock decryptValue to return 'decrypted' for consistent test expectations
7+
vi.mock('../utils/comms', () => ({
8+
decryptValue: vi.fn().mockResolvedValue('decrypted'),
9+
}));
10+
611
import { act, renderHook, waitFor } from '@testing-library/react';
712

8-
import axios, { AxiosError } from 'axios';
13+
import axios from 'axios';
914
import nock from 'nock';
1015

1116
import { mockAuth, mockSettings, mockState } from '../__mocks__/state-mocks';
@@ -27,6 +32,14 @@ describe('renderer/hooks/useNotifications.ts', () => {
2732

2833
// Reset mock notification state between tests since it's mutated
2934
mockSingleNotification.unread = true;
35+
36+
// Disable real network connections to catch unmocked requests
37+
nock.disableNetConnect();
38+
});
39+
40+
afterEach(() => {
41+
nock.cleanAll();
42+
nock.enableNetConnect();
3043
});
3144

3245
const id = mockSingleNotification.id;
@@ -120,33 +133,16 @@ describe('renderer/hooks/useNotifications.ts', () => {
120133
});
121134

122135
it('should fetch notifications with same failures', async () => {
123-
const code = AxiosError.ERR_BAD_REQUEST;
124136
const status = 401;
125137
const message = 'Bad credentials';
126138

127-
nock('https://api.github.com/')
139+
nock('https://api.github.com')
128140
.get('/notifications?participating=false')
129-
.replyWithError({
130-
code,
131-
response: {
132-
status,
133-
data: {
134-
message,
135-
},
136-
},
137-
});
141+
.reply(status, { message });
138142

139-
nock('https://github.gitify.io/api/v3/')
143+
nock('https://github.gitify.io/api/v3')
140144
.get('/notifications?participating=false')
141-
.replyWithError({
142-
code,
143-
response: {
144-
status,
145-
data: {
146-
message,
147-
},
148-
},
149-
});
145+
.reply(status, { message });
150146

151147
const { result } = renderHook(() => useNotifications());
152148

@@ -165,31 +161,13 @@ describe('renderer/hooks/useNotifications.ts', () => {
165161
});
166162

167163
it('should fetch notifications with different failures', async () => {
168-
const code = AxiosError.ERR_BAD_REQUEST;
169-
170-
nock('https://api.github.com/')
164+
nock('https://api.github.com')
171165
.get('/notifications?participating=false')
172-
.replyWithError({
173-
code,
174-
response: {
175-
status: 400,
176-
data: {
177-
message: 'Oops! Something went wrong.',
178-
},
179-
},
180-
});
166+
.reply(400, { message: 'Oops! Something went wrong.' });
181167

182-
nock('https://github.gitify.io/api/v3/')
168+
nock('https://github.gitify.io/api/v3')
183169
.get('/notifications?participating=false')
184-
.replyWithError({
185-
code,
186-
response: {
187-
status: 401,
188-
data: {
189-
message: 'Bad credentials',
190-
},
191-
},
192-
});
170+
.reply(401, { message: 'Bad credentials' });
193171

194172
const { result } = renderHook(() => useNotifications());
195173

@@ -203,14 +181,14 @@ describe('renderer/hooks/useNotifications.ts', () => {
203181
expect(result.current.status).toBe('error');
204182
});
205183

206-
expect(result.current.globalError).toBeNull();
184+
expect(result.current.globalError).toBeUndefined();
207185
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(4);
208186
});
209187
});
210188

211189
describe('markNotificationsAsRead', () => {
212190
it('should mark notifications as read with success', async () => {
213-
nock('https://api.github.com/')
191+
nock('https://api.github.com')
214192
.patch(`/notifications/threads/${id}`)
215193
.reply(200);
216194

@@ -230,7 +208,7 @@ describe('renderer/hooks/useNotifications.ts', () => {
230208
});
231209

232210
it('should mark notifications as read with failure', async () => {
233-
nock('https://api.github.com/')
211+
nock('https://api.github.com')
234212
.patch(`/notifications/threads/${id}`)
235213
.reply(400);
236214

@@ -243,17 +221,16 @@ describe('renderer/hooks/useNotifications.ts', () => {
243221
});
244222

245223
await waitFor(() => {
246-
expect(result.current.status).toBe('success');
224+
expect(result.current.status).toBe('error');
247225
});
248226

249-
expect(result.current.notifications.length).toBe(0);
250227
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);
251228
});
252229
});
253230

254231
describe('markNotificationsAsDone', () => {
255232
it('should mark notifications as done with success', async () => {
256-
nock('https://api.github.com/')
233+
nock('https://api.github.com')
257234
.delete(`/notifications/threads/${id}`)
258235
.reply(200);
259236

@@ -273,7 +250,7 @@ describe('renderer/hooks/useNotifications.ts', () => {
273250
});
274251

275252
it('should mark notifications as done with failure', async () => {
276-
nock('https://api.github.com/')
253+
nock('https://api.github.com')
277254
.delete(`/notifications/threads/${id}`)
278255
.reply(400);
279256

@@ -286,25 +263,22 @@ describe('renderer/hooks/useNotifications.ts', () => {
286263
});
287264

288265
await waitFor(() => {
289-
expect(result.current.status).toBe('success');
266+
expect(result.current.status).toBe('error');
290267
});
291268

292-
expect(result.current.notifications.length).toBe(0);
293269
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);
294270
});
295271
});
296272

297273
describe('unsubscribeNotification', () => {
298-
const id = 'notification-123';
299-
300274
it('should unsubscribe from a notification with success - markAsDoneOnUnsubscribe = false', async () => {
301275
// The unsubscribe endpoint call.
302-
nock('https://api.github.com/')
276+
nock('https://api.github.com')
303277
.put(`/notifications/threads/${id}/subscription`)
304278
.reply(200);
305279

306280
// The mark read endpoint call.
307-
nock('https://api.github.com/')
281+
nock('https://api.github.com')
308282
.patch(`/notifications/threads/${id}`)
309283
.reply(200);
310284

@@ -326,12 +300,12 @@ describe('renderer/hooks/useNotifications.ts', () => {
326300

327301
it('should unsubscribe from a notification with success - markAsDoneOnUnsubscribe = true', async () => {
328302
// The unsubscribe endpoint call.
329-
nock('https://api.github.com/')
303+
nock('https://api.github.com')
330304
.put(`/notifications/threads/${id}/subscription`)
331305
.reply(200);
332306

333307
// The mark done endpoint call.
334-
nock('https://api.github.com/')
308+
nock('https://api.github.com')
335309
.delete(`/notifications/threads/${id}`)
336310
.reply(200);
337311

@@ -359,12 +333,12 @@ describe('renderer/hooks/useNotifications.ts', () => {
359333

360334
it('should unsubscribe from a notification with failure', async () => {
361335
// The unsubscribe endpoint call.
362-
nock('https://api.github.com/')
336+
nock('https://api.github.com')
363337
.put(`/notifications/threads/${id}/subscription`)
364338
.reply(400);
365339

366-
// The mark read endpoint call.
367-
nock('https://api.github.com/')
340+
// The mark read endpoint call (won't be called since unsubscribe fails first).
341+
nock('https://api.github.com')
368342
.patch(`/notifications/threads/${id}`)
369343
.reply(400);
370344

@@ -378,10 +352,9 @@ describe('renderer/hooks/useNotifications.ts', () => {
378352
});
379353

380354
await waitFor(() => {
381-
expect(result.current.status).toBe('success');
355+
expect(result.current.status).toBe('error');
382356
});
383357

384-
expect(result.current.notifications.length).toBe(0);
385358
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);
386359
});
387360
});

src/utils/api/request.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
import { vi } from 'vitest';
2+
3+
// Mock to use axios instead of Tauri HTTP plugin
4+
vi.mock('../environment', () => ({ isTauriEnvironment: () => false }));
5+
6+
// Mock decryptValue to return 'decrypted' for consistent test expectations
7+
vi.mock('../comms', () => ({
8+
decryptValue: vi.fn().mockResolvedValue('decrypted'),
9+
}));
10+
211
import axios from 'axios';
312

413
import { mockToken } from '../../__mocks__/state-mocks';
@@ -20,9 +29,6 @@ import {
2029

2130
vi.mock('axios');
2231

23-
// Mock to use axios instead of Tauri HTTP plugin in tests
24-
vi.mock('../environment', () => ({ isTauriEnvironment: () => false }));
25-
2632
const url = 'https://example.com' as Link;
2733
const method = 'get';
2834

src/utils/links.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ describe('renderer/utils/links.ts', () => {
8888

8989
it('openDeveloperSettings', () => {
9090
const mockSettingsURL = 'https://github.com/settings/tokens' as Link;
91-
jest
92-
.spyOn(authUtils, 'getDeveloperSettingsURL')
93-
.mockReturnValue(mockSettingsURL);
91+
vi.spyOn(authUtils, 'getDeveloperSettingsURL').mockReturnValue(
92+
mockSettingsURL,
93+
);
9494

9595
openDeveloperSettings(mockGitHubCloudAccount);
9696

@@ -110,9 +110,9 @@ describe('renderer/utils/links.ts', () => {
110110

111111
it('openNotification', async () => {
112112
const mockNotificationUrl = mockSingleNotification.repository.htmlUrl;
113-
jest
114-
.spyOn(helpers, 'generateGitHubWebUrl')
115-
.mockResolvedValue(mockNotificationUrl);
113+
vi.spyOn(helpers, 'generateGitHubWebUrl').mockResolvedValue(
114+
mockNotificationUrl,
115+
);
116116

117117
await openNotification(mockSingleNotification);
118118

src/utils/notifications/handlers/checkSuite.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from 'vitest';
22

33
// Mock to use axios instead of Tauri HTTP plugin
4-
vi.mock('../../../environment', () => ({ isTauriEnvironment: () => false }));
4+
vi.mock('../../environment', () => ({ isTauriEnvironment: () => false }));
55

66
import { createPartialMockNotification } from '../../../__mocks__/notifications-mocks';
77
import { mockSettings } from '../../../__mocks__/state-mocks';

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from 'vitest';
22

33
// Mock to use axios instead of Tauri HTTP plugin
4-
vi.mock('../../../environment', () => ({ isTauriEnvironment: () => false }));
4+
vi.mock('../../environment', () => ({ isTauriEnvironment: () => false }));
55

66
import axios from 'axios';
77
import nock from 'nock';
@@ -45,7 +45,7 @@ describe('renderer/utils/notifications/handlers/commit.ts', () => {
4545
const result = await commitHandler.enrich(mockNotification, mockSettings);
4646

4747
expect(result).toEqual({
48-
state: null,
48+
state: undefined,
4949
user: {
5050
login: mockCommenter.login,
5151
htmlUrl: mockCommenter.html_url,
@@ -72,7 +72,7 @@ describe('renderer/utils/notifications/handlers/commit.ts', () => {
7272
const result = await commitHandler.enrich(mockNotification, mockSettings);
7373

7474
expect(result).toEqual({
75-
state: null,
75+
state: undefined,
7676
user: {
7777
login: mockAuthor.login,
7878
htmlUrl: mockAuthor.html_url,

src/utils/notifications/handlers/default.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from 'vitest';
22

33
// Mock to use axios instead of Tauri HTTP plugin
4-
vi.mock('../../../environment', () => ({ isTauriEnvironment: () => false }));
4+
vi.mock('../../environment', () => ({ isTauriEnvironment: () => false }));
55

66
import { createPartialMockNotification } from '../../../__mocks__/notifications-mocks';
77
import { mockSettings } from '../../../__mocks__/state-mocks';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from 'vitest';
22

33
// Mock to use axios instead of Tauri HTTP plugin
4-
vi.mock('../../../environment', () => ({ isTauriEnvironment: () => false }));
4+
vi.mock('../../environment', () => ({ isTauriEnvironment: () => false }));
55

66
import axios from 'axios';
77
import nock from 'nock';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from 'vitest';
22

33
// Mock to use axios instead of Tauri HTTP plugin
4-
vi.mock('../../../environment', () => ({ isTauriEnvironment: () => false }));
4+
vi.mock('../../environment', () => ({ isTauriEnvironment: () => false }));
55

66
import axios from 'axios';
77
import nock from 'nock';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from 'vitest';
22

33
// Mock to use axios instead of Tauri HTTP plugin
4-
vi.mock('../../../environment', () => ({ isTauriEnvironment: () => false }));
4+
vi.mock('../../environment', () => ({ isTauriEnvironment: () => false }));
55

66
import axios from 'axios';
77
import nock from 'nock';

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from 'vitest';
22

33
// Mock to use axios instead of Tauri HTTP plugin
4-
vi.mock('../../../environment', () => ({ isTauriEnvironment: () => false }));
4+
vi.mock('../../environment', () => ({ isTauriEnvironment: () => false }));
55

66
import axios from 'axios';
77
import nock from 'nock';
@@ -41,7 +41,7 @@ describe('renderer/utils/notifications/handlers/release.ts', () => {
4141
);
4242

4343
expect(result).toEqual({
44-
state: null,
44+
state: undefined,
4545
user: {
4646
login: mockAuthor.login,
4747
htmlUrl: mockAuthor.html_url,

0 commit comments

Comments
 (0)