From c53848eec1570f2b140563eb6474c80c78b8f2ef Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Tue, 19 Mar 2024 04:53:55 -0400 Subject: [PATCH] refactor: update subject tests. extract common user logic --- src/utils/subject.test.ts | 62 +++++++++++++++++++++++---------------- src/utils/subject.ts | 52 +++++++++++++------------------- 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/utils/subject.test.ts b/src/utils/subject.test.ts index d2eb357cf..b4c659afb 100644 --- a/src/utils/subject.test.ts +++ b/src/utils/subject.test.ts @@ -5,11 +5,10 @@ import { mockAccounts } from '../__mocks__/mock-state'; import { mockedSingleNotification } from '../__mocks__/mockedData'; import { getCheckSuiteAttributes, - getGitifySubjectForDiscussion, - getGitifySubjectForIssue, - getGitifySubjectForPullRequest, + getGitifySubjectDetails, getWorkflowRunAttributes, } from './subject'; +import { SubjectType } from '../typesGithub'; describe('utils/state.ts', () => { beforeEach(() => { // axios will default to using the XHR adapter which can't be intercepted @@ -142,6 +141,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is an answered discussion', + type: 'Discussion' as SubjectType, }, }; @@ -165,7 +165,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -180,6 +180,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is a duplicate discussion', + type: 'Discussion' as SubjectType, }, }; @@ -203,7 +204,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -218,6 +219,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is an open discussion', + type: 'Discussion' as SubjectType, }, }; @@ -241,7 +243,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -256,6 +258,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is an outdated discussion', + type: 'Discussion' as SubjectType, }, }; @@ -279,7 +282,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -294,6 +297,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is a reopened discussion', + type: 'Discussion' as SubjectType, }, }; @@ -317,7 +321,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -332,6 +336,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is a resolved discussion', + type: 'Discussion' as SubjectType, }, }; @@ -355,7 +360,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -370,6 +375,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is a discussion', + type: 'Discussion' as SubjectType, }, }; @@ -402,7 +408,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -410,8 +416,6 @@ describe('utils/state.ts', () => { expect(result.state).toBe('OPEN'); expect(result.user).toBe(null); }); - - it('handles unknown or missing results', async () => {}); }); describe('getGitifySubjectForIssue', () => { @@ -424,7 +428,7 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForIssue( + const result = await getGitifySubjectDetails( mockedSingleNotification, mockAccounts.token, ); @@ -442,7 +446,7 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForIssue( + const result = await getGitifySubjectDetails( mockedSingleNotification, mockAccounts.token, ); @@ -460,7 +464,7 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForIssue( + const result = await getGitifySubjectDetails( mockedSingleNotification, mockAccounts.token, ); @@ -478,7 +482,7 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForIssue( + const result = await getGitifySubjectDetails( mockedSingleNotification, mockAccounts.token, ); @@ -496,7 +500,7 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForIssue( + const result = await getGitifySubjectDetails( mockedSingleNotification, mockAccounts.token, ); @@ -507,6 +511,14 @@ describe('utils/state.ts', () => { }); describe('getGitifySubjectForPullRequest', () => { + const mockNotification = { + ...mockedSingleNotification, + subject: { + ...mockedSingleNotification.subject, + type: 'PullRequest' as SubjectType, + }, + }; + it('closed pull request state', async () => { nock('https://api.github.com') .get('/repos/manosim/notifications-test/issues/1') @@ -516,8 +528,8 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForPullRequest( - mockedSingleNotification, + const result = await getGitifySubjectDetails( + mockNotification, mockAccounts.token, ); @@ -534,8 +546,8 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForPullRequest( - mockedSingleNotification, + const result = await getGitifySubjectDetails( + mockNotification, mockAccounts.token, ); @@ -552,8 +564,8 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForPullRequest( - mockedSingleNotification, + const result = await getGitifySubjectDetails( + mockNotification, mockAccounts.token, ); @@ -570,8 +582,8 @@ describe('utils/state.ts', () => { .get('/repos/manosim/notifications-test/issues/comments/302888448') .reply(200, { user: { login: 'some-user' } }); - const result = await getGitifySubjectForPullRequest( - mockedSingleNotification, + const result = await getGitifySubjectDetails( + mockNotification, mockAccounts.token, ); diff --git a/src/utils/subject.ts b/src/utils/subject.ts index c58f54fd6..a1c9e7cdc 100644 --- a/src/utils/subject.ts +++ b/src/utils/subject.ts @@ -9,6 +9,7 @@ import { Notification, PullRequest, PullRequestStateType, + User, WorkflowRunAttributes, } from '../typesGithub'; import { apiRequestAuth } from './api-requests'; @@ -77,7 +78,7 @@ function getCheckSuiteStatus(statusDisplayName: string): CheckSuiteStatus { } } -export function getGitifySubjectForCheckSuite( +function getGitifySubjectForCheckSuite( notification: Notification, ): GitifySubject { return { @@ -86,7 +87,7 @@ export function getGitifySubjectForCheckSuite( }; } -export async function getGitifySubjectForDiscussion( +async function getGitifySubjectForDiscussion( notification: Notification, token: string, ): Promise { @@ -116,7 +117,7 @@ export async function getGitifySubjectForDiscussion( }; } -export async function getGitifySubjectForIssue( +async function getGitifySubjectForIssue( notification: Notification, token: string, ): Promise { @@ -124,26 +125,15 @@ export async function getGitifySubjectForIssue( await apiRequestAuth(notification.subject.url, 'GET', token) ).data; - let issueCommentUser = null; - if (notification.subject.latest_comment_url) { - const issueComment: IssueComments = ( - await apiRequestAuth( - notification.subject.latest_comment_url, - 'GET', - token, - ) - ).data; - - issueCommentUser = issueComment.user.login; - } + const issueCommentUser = await getLatestCommentUser(notification, token); return { state: issue.state_reason ?? issue.state, - user: issueCommentUser, + user: issueCommentUser.login, }; } -export async function getGitifySubjectForPullRequest( +async function getGitifySubjectForPullRequest( notification: Notification, token: string, ): Promise { @@ -151,18 +141,7 @@ export async function getGitifySubjectForPullRequest( await apiRequestAuth(notification.subject.url, 'GET', token) ).data; - let prCommentUser = null; - if (notification.subject.latest_comment_url) { - const prComment: IssueComments = ( - await apiRequestAuth( - notification.subject.latest_comment_url, - 'GET', - token, - ) - ).data; - - prCommentUser = prComment.user.login; - } + const prCommentUser = await getLatestCommentUser(notification, token); let prState: PullRequestStateType = pr.state; if (pr.merged) { @@ -173,11 +152,11 @@ export async function getGitifySubjectForPullRequest( return { state: prState, - user: prCommentUser, + user: prCommentUser.login, }; } -export function getGitifySubjectForWorkflowRun( +function getGitifySubjectForWorkflowRun( notification: Notification, ): GitifySubject { return { @@ -219,3 +198,14 @@ function getWorkflowRunStatus(statusDisplayName: string): CheckSuiteStatus { return null; } } + +async function getLatestCommentUser( + notification: Notification, + token: string, +): Promise { + const response: IssueComments = ( + await apiRequestAuth(notification.subject.latest_comment_url, 'GET', token) + ).data; + + return response.user; +}