Skip to content

Commit c3c4a7d

Browse files
setchyadufr
authored andcommitted
refactor: extract state notification logic (gitify-app#845)
* refactor: extract state notification logic * refactor tests
1 parent 0be4553 commit c3c4a7d

File tree

6 files changed

+543
-379
lines changed

6 files changed

+543
-379
lines changed

src/__mocks__/mockedData.ts

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -497,83 +497,3 @@ export const mockedGraphQLResponse: GraphQLSearch<DiscussionSearchResultEdge> =
497497
},
498498
},
499499
};
500-
501-
export const mockedDiscussionNotifications = [
502-
{
503-
id: 1,
504-
updated_at: '2024-02-26T00:00:00Z',
505-
repository: {
506-
full_name: 'some/repo',
507-
},
508-
subject: {
509-
title: 'This is an answered discussion',
510-
type: 'Discussion',
511-
},
512-
},
513-
{
514-
id: 2,
515-
updated_at: '2024-02-26T00:00:00Z',
516-
repository: {
517-
full_name: 'some/repo',
518-
},
519-
subject: {
520-
title: 'This is a duplicate discussion',
521-
type: 'Discussion',
522-
},
523-
},
524-
{
525-
id: 3,
526-
updated_at: '2024-02-26T00:00:00Z',
527-
repository: {
528-
full_name: 'some/repo',
529-
},
530-
subject: {
531-
title: 'This is an open discussion',
532-
type: 'Discussion',
533-
},
534-
},
535-
{
536-
id: 4,
537-
updated_at: '2024-02-26T00:00:00Z',
538-
repository: {
539-
full_name: 'some/repo',
540-
},
541-
subject: {
542-
title: 'This is nm outdated discussion',
543-
type: 'Discussion',
544-
},
545-
},
546-
{
547-
id: 5,
548-
updated_at: '2024-02-26T00:00:00Z',
549-
repository: {
550-
full_name: 'some/repo',
551-
},
552-
subject: {
553-
title: 'This is a reopened discussion',
554-
type: 'Discussion',
555-
},
556-
},
557-
{
558-
id: 6,
559-
updated_at: '2024-02-26T00:00:00Z',
560-
repository: {
561-
full_name: 'some/repo',
562-
},
563-
subject: {
564-
title: 'This is a resolved discussion',
565-
type: 'Discussion',
566-
},
567-
},
568-
{
569-
id: 7,
570-
updated_at: '2024-02-26T00:00:00Z',
571-
repository: {
572-
full_name: 'some/repo',
573-
},
574-
subject: {
575-
title: 'This is a default discussion',
576-
type: 'Discussion',
577-
},
578-
},
579-
];

src/hooks/useNotifications.test.ts

Lines changed: 25 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import axios from 'axios';
33
import nock from 'nock';
44

55
import { mockAccounts, mockSettings } from '../__mocks__/mock-state';
6-
import {
7-
mockedDiscussionNotifications,
8-
mockedUser,
9-
} from '../__mocks__/mockedData';
6+
import { mockedUser } from '../__mocks__/mockedData';
107
import { AuthState } from '../types';
118
import { useNotifications } from './useNotifications';
129

@@ -195,107 +192,45 @@ describe('hooks/useNotifications.ts', () => {
195192
{
196193
id: 1,
197194
subject: {
198-
title: 'This is a notification.',
199-
type: 'Issue',
195+
title: 'This is a Discussion.',
196+
type: 'Discussion',
200197
url: 'https://api.github.com/1',
201198
},
199+
repository: {
200+
full_name: 'some/repo',
201+
},
202+
updated_at: '2024-02-26T00:00:00Z',
202203
},
203204
{
204205
id: 2,
205206
subject: {
206-
title: 'A merged PR.',
207-
type: 'PullRequest',
207+
title: 'This is an Issue.',
208+
type: 'Issue',
208209
url: 'https://api.github.com/2',
209210
},
210211
},
211212
{
212213
id: 3,
213214
subject: {
214-
title: 'A closed PR.',
215+
title: 'This is a Pull Request.',
215216
type: 'PullRequest',
216217
url: 'https://api.github.com/3',
217218
},
218219
},
219220
{
220221
id: 4,
221222
subject: {
222-
title: 'A draft PR.',
223-
type: 'PullRequest',
223+
title: 'This is an invitation.',
224+
type: 'RepositoryInvitation',
224225
url: 'https://api.github.com/4',
225226
},
226227
},
227-
{
228-
id: 5,
229-
subject: {
230-
title: 'A draft PR.',
231-
type: 'PullRequest',
232-
url: 'https://api.github.com/5',
233-
},
234-
},
235228
];
236229

237230
nock('https://api.github.com')
238231
.get('/notifications?participating=false')
239232
.reply(200, notifications);
240233

241-
nock('https://api.github.com').get('/1').reply(200, { state: 'open' });
242-
nock('https://api.github.com')
243-
.get('/2')
244-
.reply(200, { state: 'closed', merged: true });
245-
nock('https://api.github.com')
246-
.get('/3')
247-
.reply(200, { state: 'closed', merged: false });
248-
nock('https://api.github.com')
249-
.get('/4')
250-
.reply(200, { state: 'open', draft: false });
251-
nock('https://api.github.com')
252-
.get('/5')
253-
.reply(200, { state: 'open', draft: true });
254-
255-
const { result } = renderHook(() => useNotifications(true));
256-
257-
act(() => {
258-
result.current.fetchNotifications(accounts, {
259-
...mockSettings,
260-
colors: true,
261-
});
262-
});
263-
264-
expect(result.current.isFetching).toBe(true);
265-
266-
await waitFor(() => {
267-
expect(result.current.notifications[0].hostname).toBe('github.com');
268-
});
269-
270-
expect(result.current.notifications[0].notifications.length).toBe(5);
271-
expect(
272-
result.current.notifications[0].notifications[0].subject.state,
273-
).toBe('open');
274-
expect(
275-
result.current.notifications[0].notifications[1].subject.state,
276-
).toBe('merged');
277-
expect(
278-
result.current.notifications[0].notifications[2].subject.state,
279-
).toBe('closed');
280-
expect(
281-
result.current.notifications[0].notifications[3].subject.state,
282-
).toBe('open');
283-
expect(
284-
result.current.notifications[0].notifications[4].subject.state,
285-
).toBe('draft');
286-
});
287-
288-
it('should fetch discussion notifications with success - with colors', async () => {
289-
const accounts: AuthState = {
290-
...mockAccounts,
291-
enterpriseAccounts: [],
292-
user: mockedUser,
293-
};
294-
295-
nock('https://api.github.com')
296-
.get('/notifications?participating=false')
297-
.reply(200, mockedDiscussionNotifications);
298-
299234
nock('https://api.github.com')
300235
.post('/graphql')
301236
.reply(200, {
@@ -313,117 +248,19 @@ describe('hooks/useNotifications.ts', () => {
313248
],
314249
},
315250
},
316-
})
317-
.post('/graphql')
318-
.reply(200, {
319-
data: {
320-
search: {
321-
edges: [
322-
{
323-
node: {
324-
title: 'This is a duplicate discussion',
325-
viewerSubscription: 'SUBSCRIBED',
326-
stateReason: 'DUPLICATE',
327-
isAnswered: false,
328-
},
329-
},
330-
],
331-
},
332-
},
333-
})
334-
.post('/graphql')
335-
.reply(200, {
336-
data: {
337-
search: {
338-
edges: [
339-
{
340-
node: {
341-
title: 'This is an open discussion',
342-
viewerSubscription: 'SUBSCRIBED',
343-
stateReason: null,
344-
isAnswered: false,
345-
},
346-
},
347-
{
348-
node: {
349-
title: 'This is an open discussion',
350-
viewerSubscription: 'IGNORED',
351-
stateReason: null,
352-
isAnswered: false,
353-
},
354-
},
355-
],
356-
},
357-
},
358-
})
359-
.post('/graphql')
360-
.reply(200, {
361-
data: {
362-
search: {
363-
edges: [
364-
{
365-
node: {
366-
title: 'This is nm outdated discussion',
367-
viewerSubscription: 'SUBSCRIBED',
368-
stateReason: 'OUTDATED',
369-
isAnswered: false,
370-
},
371-
},
372-
],
373-
},
374-
},
375-
})
376-
.post('/graphql')
377-
.reply(200, {
378-
data: {
379-
search: {
380-
edges: [
381-
{
382-
node: {
383-
title: 'This is a reopened discussion',
384-
viewerSubscription: 'SUBSCRIBED',
385-
stateReason: 'REOPENED',
386-
isAnswered: false,
387-
},
388-
},
389-
],
390-
},
391-
},
392-
})
393-
.post('/graphql')
394-
.reply(200, {
395-
data: {
396-
search: {
397-
edges: [
398-
{
399-
node: {
400-
title: 'This is a resolved discussion',
401-
viewerSubscription: 'SUBSCRIBED',
402-
stateReason: 'RESOLVED',
403-
isAnswered: false,
404-
},
405-
},
406-
],
407-
},
408-
},
409-
})
410-
.post('/graphql')
411-
.reply(200, {
412-
data: {
413-
search: {
414-
edges: [
415-
{
416-
node: {
417-
title: 'unknown search result',
418-
viewerSubscription: 'SUBSCRIBED',
419-
stateReason: null,
420-
isAnswered: false,
421-
},
422-
},
423-
],
424-
},
425-
},
426251
});
252+
nock('https://api.github.com')
253+
.get('/2')
254+
.reply(200, { state: 'closed', merged: true });
255+
nock('https://api.github.com')
256+
.get('/3')
257+
.reply(200, { state: 'closed', merged: false });
258+
nock('https://api.github.com')
259+
.get('/4')
260+
.reply(200, { state: 'open', draft: false });
261+
nock('https://api.github.com')
262+
.get('/5')
263+
.reply(200, { state: 'open', draft: true });
427264

428265
const { result } = renderHook(() => useNotifications(true));
429266

@@ -440,26 +277,7 @@ describe('hooks/useNotifications.ts', () => {
440277
expect(result.current.notifications[0].hostname).toBe('github.com');
441278
});
442279

443-
const resultNotifications = result.current.notifications[0];
444-
445-
expect(resultNotifications.notifications.length).toBe(7);
446-
expect(resultNotifications.notifications[0].subject.state).toBe(
447-
'ANSWERED',
448-
);
449-
expect(resultNotifications.notifications[1].subject.state).toBe(
450-
'DUPLICATE',
451-
);
452-
expect(resultNotifications.notifications[2].subject.state).toBe('OPEN');
453-
expect(resultNotifications.notifications[3].subject.state).toBe(
454-
'OUTDATED',
455-
);
456-
expect(resultNotifications.notifications[4].subject.state).toBe(
457-
'REOPENED',
458-
);
459-
expect(resultNotifications.notifications[5].subject.state).toBe(
460-
'RESOLVED',
461-
);
462-
expect(resultNotifications.notifications[6].subject.state).toBe('OPEN');
280+
expect(result.current.notifications[0].notifications.length).toBe(4);
463281
});
464282
});
465283
});

0 commit comments

Comments
 (0)