Skip to content

Commit a656756

Browse files
authored
refactor: update subject tests. extract common user logic (#900)
1 parent 2f3e11f commit a656756

File tree

2 files changed

+58
-56
lines changed

2 files changed

+58
-56
lines changed

src/utils/subject.test.ts

+37-25
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import { mockAccounts } from '../__mocks__/mock-state';
55
import { mockedSingleNotification } from '../__mocks__/mockedData';
66
import {
77
getCheckSuiteAttributes,
8-
getGitifySubjectForDiscussion,
9-
getGitifySubjectForIssue,
10-
getGitifySubjectForPullRequest,
8+
getGitifySubjectDetails,
119
getWorkflowRunAttributes,
1210
} from './subject';
11+
import { SubjectType } from '../typesGithub';
1312
describe('utils/state.ts', () => {
1413
beforeEach(() => {
1514
// axios will default to using the XHR adapter which can't be intercepted
@@ -142,6 +141,7 @@ describe('utils/state.ts', () => {
142141
subject: {
143142
...mockedSingleNotification.subject,
144143
title: 'This is an answered discussion',
144+
type: 'Discussion' as SubjectType,
145145
},
146146
};
147147

@@ -165,7 +165,7 @@ describe('utils/state.ts', () => {
165165
},
166166
});
167167

168-
const result = await getGitifySubjectForDiscussion(
168+
const result = await getGitifySubjectDetails(
169169
mockNotification,
170170
mockAccounts.token,
171171
);
@@ -180,6 +180,7 @@ describe('utils/state.ts', () => {
180180
subject: {
181181
...mockedSingleNotification.subject,
182182
title: 'This is a duplicate discussion',
183+
type: 'Discussion' as SubjectType,
183184
},
184185
};
185186

@@ -203,7 +204,7 @@ describe('utils/state.ts', () => {
203204
},
204205
});
205206

206-
const result = await getGitifySubjectForDiscussion(
207+
const result = await getGitifySubjectDetails(
207208
mockNotification,
208209
mockAccounts.token,
209210
);
@@ -218,6 +219,7 @@ describe('utils/state.ts', () => {
218219
subject: {
219220
...mockedSingleNotification.subject,
220221
title: 'This is an open discussion',
222+
type: 'Discussion' as SubjectType,
221223
},
222224
};
223225

@@ -241,7 +243,7 @@ describe('utils/state.ts', () => {
241243
},
242244
});
243245

244-
const result = await getGitifySubjectForDiscussion(
246+
const result = await getGitifySubjectDetails(
245247
mockNotification,
246248
mockAccounts.token,
247249
);
@@ -256,6 +258,7 @@ describe('utils/state.ts', () => {
256258
subject: {
257259
...mockedSingleNotification.subject,
258260
title: 'This is an outdated discussion',
261+
type: 'Discussion' as SubjectType,
259262
},
260263
};
261264

@@ -279,7 +282,7 @@ describe('utils/state.ts', () => {
279282
},
280283
});
281284

282-
const result = await getGitifySubjectForDiscussion(
285+
const result = await getGitifySubjectDetails(
283286
mockNotification,
284287
mockAccounts.token,
285288
);
@@ -294,6 +297,7 @@ describe('utils/state.ts', () => {
294297
subject: {
295298
...mockedSingleNotification.subject,
296299
title: 'This is a reopened discussion',
300+
type: 'Discussion' as SubjectType,
297301
},
298302
};
299303

@@ -317,7 +321,7 @@ describe('utils/state.ts', () => {
317321
},
318322
});
319323

320-
const result = await getGitifySubjectForDiscussion(
324+
const result = await getGitifySubjectDetails(
321325
mockNotification,
322326
mockAccounts.token,
323327
);
@@ -332,6 +336,7 @@ describe('utils/state.ts', () => {
332336
subject: {
333337
...mockedSingleNotification.subject,
334338
title: 'This is a resolved discussion',
339+
type: 'Discussion' as SubjectType,
335340
},
336341
};
337342

@@ -355,7 +360,7 @@ describe('utils/state.ts', () => {
355360
},
356361
});
357362

358-
const result = await getGitifySubjectForDiscussion(
363+
const result = await getGitifySubjectDetails(
359364
mockNotification,
360365
mockAccounts.token,
361366
);
@@ -370,6 +375,7 @@ describe('utils/state.ts', () => {
370375
subject: {
371376
...mockedSingleNotification.subject,
372377
title: 'This is a discussion',
378+
type: 'Discussion' as SubjectType,
373379
},
374380
};
375381

@@ -402,16 +408,14 @@ describe('utils/state.ts', () => {
402408
},
403409
});
404410

405-
const result = await getGitifySubjectForDiscussion(
411+
const result = await getGitifySubjectDetails(
406412
mockNotification,
407413
mockAccounts.token,
408414
);
409415

410416
expect(result.state).toBe('OPEN');
411417
expect(result.user).toBe(null);
412418
});
413-
414-
it('handles unknown or missing results', async () => {});
415419
});
416420

417421
describe('getGitifySubjectForIssue', () => {
@@ -424,7 +428,7 @@ describe('utils/state.ts', () => {
424428
.get('/repos/manosim/notifications-test/issues/comments/302888448')
425429
.reply(200, { user: { login: 'some-user' } });
426430

427-
const result = await getGitifySubjectForIssue(
431+
const result = await getGitifySubjectDetails(
428432
mockedSingleNotification,
429433
mockAccounts.token,
430434
);
@@ -442,7 +446,7 @@ describe('utils/state.ts', () => {
442446
.get('/repos/manosim/notifications-test/issues/comments/302888448')
443447
.reply(200, { user: { login: 'some-user' } });
444448

445-
const result = await getGitifySubjectForIssue(
449+
const result = await getGitifySubjectDetails(
446450
mockedSingleNotification,
447451
mockAccounts.token,
448452
);
@@ -460,7 +464,7 @@ describe('utils/state.ts', () => {
460464
.get('/repos/manosim/notifications-test/issues/comments/302888448')
461465
.reply(200, { user: { login: 'some-user' } });
462466

463-
const result = await getGitifySubjectForIssue(
467+
const result = await getGitifySubjectDetails(
464468
mockedSingleNotification,
465469
mockAccounts.token,
466470
);
@@ -478,7 +482,7 @@ describe('utils/state.ts', () => {
478482
.get('/repos/manosim/notifications-test/issues/comments/302888448')
479483
.reply(200, { user: { login: 'some-user' } });
480484

481-
const result = await getGitifySubjectForIssue(
485+
const result = await getGitifySubjectDetails(
482486
mockedSingleNotification,
483487
mockAccounts.token,
484488
);
@@ -496,7 +500,7 @@ describe('utils/state.ts', () => {
496500
.get('/repos/manosim/notifications-test/issues/comments/302888448')
497501
.reply(200, { user: { login: 'some-user' } });
498502

499-
const result = await getGitifySubjectForIssue(
503+
const result = await getGitifySubjectDetails(
500504
mockedSingleNotification,
501505
mockAccounts.token,
502506
);
@@ -507,6 +511,14 @@ describe('utils/state.ts', () => {
507511
});
508512

509513
describe('getGitifySubjectForPullRequest', () => {
514+
const mockNotification = {
515+
...mockedSingleNotification,
516+
subject: {
517+
...mockedSingleNotification.subject,
518+
type: 'PullRequest' as SubjectType,
519+
},
520+
};
521+
510522
it('closed pull request state', async () => {
511523
nock('https://api.github.com')
512524
.get('/repos/manosim/notifications-test/issues/1')
@@ -516,8 +528,8 @@ describe('utils/state.ts', () => {
516528
.get('/repos/manosim/notifications-test/issues/comments/302888448')
517529
.reply(200, { user: { login: 'some-user' } });
518530

519-
const result = await getGitifySubjectForPullRequest(
520-
mockedSingleNotification,
531+
const result = await getGitifySubjectDetails(
532+
mockNotification,
521533
mockAccounts.token,
522534
);
523535

@@ -534,8 +546,8 @@ describe('utils/state.ts', () => {
534546
.get('/repos/manosim/notifications-test/issues/comments/302888448')
535547
.reply(200, { user: { login: 'some-user' } });
536548

537-
const result = await getGitifySubjectForPullRequest(
538-
mockedSingleNotification,
549+
const result = await getGitifySubjectDetails(
550+
mockNotification,
539551
mockAccounts.token,
540552
);
541553

@@ -552,8 +564,8 @@ describe('utils/state.ts', () => {
552564
.get('/repos/manosim/notifications-test/issues/comments/302888448')
553565
.reply(200, { user: { login: 'some-user' } });
554566

555-
const result = await getGitifySubjectForPullRequest(
556-
mockedSingleNotification,
567+
const result = await getGitifySubjectDetails(
568+
mockNotification,
557569
mockAccounts.token,
558570
);
559571

@@ -570,8 +582,8 @@ describe('utils/state.ts', () => {
570582
.get('/repos/manosim/notifications-test/issues/comments/302888448')
571583
.reply(200, { user: { login: 'some-user' } });
572584

573-
const result = await getGitifySubjectForPullRequest(
574-
mockedSingleNotification,
585+
const result = await getGitifySubjectDetails(
586+
mockNotification,
575587
mockAccounts.token,
576588
);
577589

src/utils/subject.ts

+21-31
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Notification,
1010
PullRequest,
1111
PullRequestStateType,
12+
User,
1213
WorkflowRunAttributes,
1314
} from '../typesGithub';
1415
import { apiRequestAuth } from './api-requests';
@@ -77,7 +78,7 @@ function getCheckSuiteStatus(statusDisplayName: string): CheckSuiteStatus {
7778
}
7879
}
7980

80-
export function getGitifySubjectForCheckSuite(
81+
function getGitifySubjectForCheckSuite(
8182
notification: Notification,
8283
): GitifySubject {
8384
return {
@@ -86,7 +87,7 @@ export function getGitifySubjectForCheckSuite(
8687
};
8788
}
8889

89-
export async function getGitifySubjectForDiscussion(
90+
async function getGitifySubjectForDiscussion(
9091
notification: Notification,
9192
token: string,
9293
): Promise<GitifySubject> {
@@ -116,53 +117,31 @@ export async function getGitifySubjectForDiscussion(
116117
};
117118
}
118119

119-
export async function getGitifySubjectForIssue(
120+
async function getGitifySubjectForIssue(
120121
notification: Notification,
121122
token: string,
122123
): Promise<GitifySubject> {
123124
const issue: Issue = (
124125
await apiRequestAuth(notification.subject.url, 'GET', token)
125126
).data;
126127

127-
let issueCommentUser = null;
128-
if (notification.subject.latest_comment_url) {
129-
const issueComment: IssueComments = (
130-
await apiRequestAuth(
131-
notification.subject.latest_comment_url,
132-
'GET',
133-
token,
134-
)
135-
).data;
136-
137-
issueCommentUser = issueComment.user.login;
138-
}
128+
const issueCommentUser = await getLatestCommentUser(notification, token);
139129

140130
return {
141131
state: issue.state_reason ?? issue.state,
142-
user: issueCommentUser,
132+
user: issueCommentUser.login,
143133
};
144134
}
145135

146-
export async function getGitifySubjectForPullRequest(
136+
async function getGitifySubjectForPullRequest(
147137
notification: Notification,
148138
token: string,
149139
): Promise<GitifySubject> {
150140
const pr: PullRequest = (
151141
await apiRequestAuth(notification.subject.url, 'GET', token)
152142
).data;
153143

154-
let prCommentUser = null;
155-
if (notification.subject.latest_comment_url) {
156-
const prComment: IssueComments = (
157-
await apiRequestAuth(
158-
notification.subject.latest_comment_url,
159-
'GET',
160-
token,
161-
)
162-
).data;
163-
164-
prCommentUser = prComment.user.login;
165-
}
144+
const prCommentUser = await getLatestCommentUser(notification, token);
166145

167146
let prState: PullRequestStateType = pr.state;
168147
if (pr.merged) {
@@ -173,11 +152,11 @@ export async function getGitifySubjectForPullRequest(
173152

174153
return {
175154
state: prState,
176-
user: prCommentUser,
155+
user: prCommentUser.login,
177156
};
178157
}
179158

180-
export function getGitifySubjectForWorkflowRun(
159+
function getGitifySubjectForWorkflowRun(
181160
notification: Notification,
182161
): GitifySubject {
183162
return {
@@ -219,3 +198,14 @@ function getWorkflowRunStatus(statusDisplayName: string): CheckSuiteStatus {
219198
return null;
220199
}
221200
}
201+
202+
async function getLatestCommentUser(
203+
notification: Notification,
204+
token: string,
205+
): Promise<User> {
206+
const response: IssueComments = (
207+
await apiRequestAuth(notification.subject.latest_comment_url, 'GET', token)
208+
).data;
209+
210+
return response.user;
211+
}

0 commit comments

Comments
 (0)