From c01beb99a81761cfebe9c3820b568f137ea2fc3d Mon Sep 17 00:00:00 2001 From: TJ Silver Date: Wed, 27 Nov 2024 09:53:19 +0000 Subject: [PATCH] refactor: move tests for moved function --- .../src/pull-requests.test.ts | 85 +------------------ .../src/pull-requests.ts | 3 - .../send-to-sns.test.ts | 84 ++++++++++++++++++ 3 files changed, 85 insertions(+), 87 deletions(-) diff --git a/packages/dependency-graph-integrator/src/pull-requests.test.ts b/packages/dependency-graph-integrator/src/pull-requests.test.ts index 82c7ffb81..d0aa386b2 100644 --- a/packages/dependency-graph-integrator/src/pull-requests.test.ts +++ b/packages/dependency-graph-integrator/src/pull-requests.test.ts @@ -1,87 +1,4 @@ -import type { Octokit } from 'octokit'; -import { generateBranchName, getExistingPullRequest } from './pull-requests'; - -/** - * Create a mocked version of the Octokit SDK that returns a given array of pull requests - */ -function mockOctokit(pulls: unknown[]) { - return { - // eslint-disable-next-line @typescript-eslint/no-unused-vars -- It's just a mock - paginate: (arg0: unknown, arg1: unknown) => Promise.resolve(pulls), - rest: { - pulls: { - list: () => {}, - }, - }, - } as Octokit; -} - -describe('getPullRequest', () => { - const featureBranch = { - head: { - ref: 'feature-branch', - }, - user: { - login: 'some-user', - type: 'User', - }, - }; - - const dependabotBranch = { - head: { - ref: 'integrate-dependabot-abcd', - }, - user: { - login: 'gu-dependency-graph-integrator[bot]', - type: 'Bot', - }, - }; - - const dependabotBranch2 = { - ...dependabotBranch, - head: { - ref: 'integrate-dependabot-efgh', - }, - }; - - it('should return undefined when no matching branch found', async () => { - const pulls = [featureBranch]; - const foundPull = await getExistingPullRequest( - mockOctokit(pulls), - 'repo', - 'owner', - 'gu-dependency-graph-integrator[bot]', - ); - expect(foundPull).toBeUndefined(); - }); - - it('should return pull request when author matches', async () => { - const pulls = [featureBranch, dependabotBranch]; - const foundPull = await getExistingPullRequest( - mockOctokit(pulls), - 'repo', - 'owner', - 'gu-dependency-graph-integrator[bot]', - ); - expect(foundPull).toEqual(dependabotBranch); - }); - - it('should return first pull request that matches and log warning', async () => { - const warn = jest.spyOn(console, 'warn'); - const pulls = [featureBranch, dependabotBranch, dependabotBranch2]; - const foundPull = await getExistingPullRequest( - mockOctokit(pulls), - 'repo', - 'owner', - 'gu-dependency-graph-integrator[bot]', - ); - expect(foundPull).toEqual(dependabotBranch); - expect(warn).toHaveBeenCalledWith( - 'More than one PR found on repo - choosing the first.', - ); - warn.mockRestore(); - }); -}); +import { generateBranchName } from './pull-requests'; describe('generateBranchName', () => { it('does not produce the same branch name twice', () => { diff --git a/packages/dependency-graph-integrator/src/pull-requests.ts b/packages/dependency-graph-integrator/src/pull-requests.ts index 07bd98c20..da6085fec 100644 --- a/packages/dependency-graph-integrator/src/pull-requests.ts +++ b/packages/dependency-graph-integrator/src/pull-requests.ts @@ -90,9 +90,6 @@ export async function requestTeamReview( } } - - - export async function createPrAndAddToProject( stage: string, repoName: string, diff --git a/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.test.ts b/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.test.ts index 62f6105a3..6a230e386 100644 --- a/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.test.ts +++ b/packages/repocop/src/remediation/dependency_graph-integrator/send-to-sns.test.ts @@ -8,11 +8,13 @@ import type { Repository, RepositoryWithDepGraphLanguage, } from 'common/src/types'; +import type { Octokit } from 'octokit'; import { removeRepoOwner } from '../shared-utilities'; import { checkRepoForLanguage, createSnsEventsForDependencyGraphIntegration, doesRepoHaveDepSubmissionWorkflowForLanguage, + getExistingPullRequest, getReposWithoutWorkflows, } from './send-to-sns'; @@ -229,3 +231,85 @@ describe('When getting suitable events to send to SNS', () => { ]); }); }); + +/** + * Create a mocked version of the Octokit SDK that returns a given array of pull requests + */ +function mockOctokit(pulls: unknown[]) { + return { + // eslint-disable-next-line @typescript-eslint/no-unused-vars -- It's just a mock + paginate: (arg0: unknown, arg1: unknown) => Promise.resolve(pulls), + rest: { + pulls: { + list: () => {}, + }, + }, + } as Octokit; +} + +describe('getPullRequest', () => { + const featureBranch = { + head: { + ref: 'feature-branch', + }, + user: { + login: 'some-user', + type: 'User', + }, + }; + + const dependabotBranch = { + head: { + ref: 'integrate-dependabot-abcd', + }, + user: { + login: 'gu-dependency-graph-integrator[bot]', + type: 'Bot', + }, + }; + + const dependabotBranch2 = { + ...dependabotBranch, + head: { + ref: 'integrate-dependabot-efgh', + }, + }; + + it('should return undefined when no matching branch found', async () => { + const pulls = [featureBranch]; + const foundPull = await getExistingPullRequest( + mockOctokit(pulls), + 'repo', + 'owner', + 'gu-dependency-graph-integrator[bot]', + ); + expect(foundPull).toBeUndefined(); + }); + + it('should return pull request when author matches', async () => { + const pulls = [featureBranch, dependabotBranch]; + const foundPull = await getExistingPullRequest( + mockOctokit(pulls), + 'repo', + 'owner', + 'gu-dependency-graph-integrator[bot]', + ); + expect(foundPull).toEqual(dependabotBranch); + }); + + it('should return first pull request that matches and log warning', async () => { + const warn = jest.spyOn(console, 'warn'); + const pulls = [featureBranch, dependabotBranch, dependabotBranch2]; + const foundPull = await getExistingPullRequest( + mockOctokit(pulls), + 'repo', + 'owner', + 'gu-dependency-graph-integrator[bot]', + ); + expect(foundPull).toEqual(dependabotBranch); + expect(warn).toHaveBeenCalledWith( + 'More than one PR found on repo - choosing the first.', + ); + warn.mockRestore(); + }); +});