@@ -3,9 +3,14 @@ import { vi } from 'vitest';
33// Mock to use axios instead of Tauri HTTP plugin
44vi . 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+
611import { act , renderHook , waitFor } from '@testing-library/react' ;
712
8- import axios , { AxiosError } from 'axios' ;
13+ import axios from 'axios' ;
914import nock from 'nock' ;
1015
1116import { 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 } ) ;
0 commit comments