Skip to content

Commit 027a3e9

Browse files
committed
feat(api): codegen for discussions
Signed-off-by: Adam Setch <[email protected]>
1 parent 6d53584 commit 027a3e9

File tree

4 files changed

+62
-45
lines changed

4 files changed

+62
-45
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"package:win": "electron-builder --win --config ./config/electron-builder.js",
2020
"lint:check": "biome check",
2121
"lint": "biome check --fix",
22-
"test": "jest",
22+
"test": "TZ=UTC jest",
2323
"start": "electron . --enable-logging",
2424
"prepare": "husky",
2525
"codegen": "graphql-codegen --config codegen.ts"

src/renderer/utils/helpers.test.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
mockDiscussionByNumberGraphQLResponse,
1515
mockSingleNotification,
1616
} from './api/__mocks__/response-mocks';
17+
import * as apiClient from './api/client';
18+
import type { FetchDiscussionByNumberQuery } from './api/graphql/generated/graphql';
1719
import * as apiRequests from './api/request';
20+
import type { GitHubGraphQLResponse } from './api/types';
1821
import {
1922
generateGitHubWebUrl,
2023
generateNotificationReferrerId,
@@ -273,9 +276,9 @@ describe('renderer/utils/helpers.ts', () => {
273276
});
274277

275278
describe('Discussions URLs', () => {
276-
const performGraphQLRequestSpy = jest.spyOn(
277-
apiRequests,
278-
'performGraphQLRequest',
279+
const fetchDiscussionByNumberSpy = jest.spyOn(
280+
apiClient,
281+
'fetchDiscussionByNumber',
279282
);
280283

281284
it('when no subject urls and no discussions found via query, default to linking to repository discussions', async () => {
@@ -286,16 +289,16 @@ describe('renderer/utils/helpers.ts', () => {
286289
type: 'Discussion' as SubjectType,
287290
};
288291

289-
performGraphQLRequestSpy.mockResolvedValue({
290-
data: { data: { search: { nodes: [] } } },
291-
} as AxiosResponse);
292+
fetchDiscussionByNumberSpy.mockResolvedValue({
293+
data: { repository: { discussion: null } },
294+
} as any);
292295

293296
const result = await generateGitHubWebUrl({
294297
...mockSingleNotification,
295298
subject: subject,
296299
});
297300

298-
expect(performGraphQLRequestSpy).toHaveBeenCalledTimes(1);
301+
expect(fetchDiscussionByNumberSpy).toHaveBeenCalledTimes(1);
299302
expect(result).toBe(
300303
`${mockSingleNotification.repository.html_url}/discussions?${mockNotificationReferrer}`,
301304
);
@@ -309,18 +312,16 @@ describe('renderer/utils/helpers.ts', () => {
309312
type: 'Discussion' as SubjectType,
310313
};
311314

312-
performGraphQLRequestSpy.mockResolvedValue({
313-
data: {
314-
...mockDiscussionByNumberGraphQLResponse,
315-
},
316-
} as AxiosResponse);
315+
fetchDiscussionByNumberSpy.mockResolvedValue({
316+
data: mockDiscussionByNumberGraphQLResponse,
317+
} as GitHubGraphQLResponse<FetchDiscussionByNumberQuery>);
317318

318319
const result = await generateGitHubWebUrl({
319320
...mockSingleNotification,
320321
subject: subject,
321322
});
322323

323-
expect(performGraphQLRequestSpy).toHaveBeenCalledTimes(1);
324+
expect(fetchDiscussionByNumberSpy).toHaveBeenCalledTimes(1);
324325
expect(result).toBe(
325326
`https://github.com/gitify-app/notifications-test/discussions/612?${mockNotificationReferrer}#discussioncomment-2300902`,
326327
);
@@ -338,14 +339,20 @@ describe('renderer/utils/helpers.ts', () => {
338339
type: 'Discussion' as SubjectType,
339340
};
340341

341-
performGraphQLRequestSpy.mockResolvedValue(null as AxiosResponse);
342+
fetchDiscussionByNumberSpy.mockResolvedValue({
343+
errors: [
344+
{
345+
message: 'Something failed',
346+
},
347+
],
348+
} as GitHubGraphQLResponse<FetchDiscussionByNumberQuery>);
342349

343350
const result = await generateGitHubWebUrl({
344351
...mockSingleNotification,
345352
subject: subject,
346353
});
347354

348-
expect(performGraphQLRequestSpy).toHaveBeenCalledTimes(1);
355+
expect(fetchDiscussionByNumberSpy).toHaveBeenCalledTimes(1);
349356
expect(result).toBe(
350357
`https://github.com/gitify-app/notifications-test/discussions?${mockNotificationReferrer}`,
351358
);

src/renderer/utils/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ export async function generateGitHubWebUrl(
160160
err,
161161
notification,
162162
);
163+
164+
if (notification.subject.type === 'Discussion') {
165+
url.pathname += '/discussions';
166+
}
163167
}
164168

165169
url.searchParams.set(

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

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
2626
describe('enrich', () => {
2727
const partialRepository: Partial<Repository> = {
2828
full_name: 'gitify-app/notifications-test',
29+
owner: {
30+
login: 'gitify-app',
31+
} as any,
2932
};
3033

3134
const mockNotification = createPartialMockNotification({
3235
title: 'This is a mock discussion',
3336
type: 'Discussion',
37+
url: 'https://api.github.com/repos/gitify-app/notifications-test/discussions/123' as Link,
3438
});
3539
mockNotification.updated_at = '2024-01-01T00:00:00Z';
3640
mockNotification.repository = {
@@ -48,8 +52,8 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
4852
.post('/graphql')
4953
.reply(200, {
5054
data: {
51-
search: {
52-
nodes: [mockDiscussionNode(null, true)],
55+
repository: {
56+
discussion: mockDiscussionNode(null, true),
5357
},
5458
},
5559
});
@@ -78,10 +82,11 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
7882
.post('/graphql')
7983
.reply(200, {
8084
data: {
81-
search: {
82-
nodes: [
83-
mockDiscussionNode(DiscussionStateReason.Duplicate, false),
84-
],
85+
repository: {
86+
discussion: mockDiscussionNode(
87+
DiscussionStateReason.Duplicate,
88+
false,
89+
),
8590
},
8691
},
8792
});
@@ -110,8 +115,8 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
110115
.post('/graphql')
111116
.reply(200, {
112117
data: {
113-
search: {
114-
nodes: [mockDiscussionNode(null, false)],
118+
repository: {
119+
discussion: mockDiscussionNode(null, false),
115120
},
116121
},
117122
});
@@ -140,10 +145,11 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
140145
.post('/graphql')
141146
.reply(200, {
142147
data: {
143-
search: {
144-
nodes: [
145-
mockDiscussionNode(DiscussionStateReason.Outdated, false),
146-
],
148+
repository: {
149+
discussion: mockDiscussionNode(
150+
DiscussionStateReason.Outdated,
151+
false,
152+
),
147153
},
148154
},
149155
});
@@ -172,10 +178,11 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
172178
.post('/graphql')
173179
.reply(200, {
174180
data: {
175-
search: {
176-
nodes: [
177-
mockDiscussionNode(DiscussionStateReason.Reopened, false),
178-
],
181+
repository: {
182+
discussion: mockDiscussionNode(
183+
DiscussionStateReason.Reopened,
184+
false,
185+
),
179186
},
180187
},
181188
});
@@ -204,8 +211,11 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
204211
.post('/graphql')
205212
.reply(200, {
206213
data: {
207-
search: {
208-
nodes: [mockDiscussionNode(DiscussionStateReason.Resolved, true)],
214+
repository: {
215+
discussion: mockDiscussionNode(
216+
DiscussionStateReason.Resolved,
217+
true,
218+
),
209219
},
210220
},
211221
});
@@ -243,8 +253,8 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
243253
.post('/graphql')
244254
.reply(200, {
245255
data: {
246-
search: {
247-
nodes: [mockDiscussion],
256+
repository: {
257+
discussion: mockDiscussion,
248258
},
249259
},
250260
});
@@ -273,8 +283,8 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
273283
.post('/graphql')
274284
.reply(200, {
275285
data: {
276-
search: {
277-
nodes: [mockDiscussionNode(null, false)],
286+
repository: {
287+
discussion: mockDiscussionNode(null, false),
278288
},
279289
},
280290
});
@@ -324,11 +334,7 @@ function mockDiscussionNode(
324334
url: 'https://github.com/gitify-app/notifications-test/discussions/1' as Link,
325335
stateReason: state || undefined,
326336
isAnswered: isAnswered,
327-
author: {
328-
login: mockDiscussionAuthor.login,
329-
url: mockDiscussionAuthor.url,
330-
avatarUrl: mockDiscussionAuthor.avatar_url,
331-
} as Partial<Discussion>['author'],
337+
author: mockDiscussionAuthor,
332338
comments: {
333339
nodes: [],
334340
totalCount: 0,
@@ -338,7 +344,7 @@ function mockDiscussionNode(
338344
startCursor: null,
339345
endCursor: null,
340346
},
341-
} as Partial<Discussion>['comments'],
347+
} as unknown as Partial<Discussion>['comments'],
342348
labels: null as Partial<Discussion>['labels'],
343-
} as Partial<Discussion>;
349+
} as unknown as Partial<Discussion>;
344350
}

0 commit comments

Comments
 (0)