Skip to content

Commit

Permalink
refactor: move tests for moved function
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsilver committed Nov 27, 2024
1 parent a5bdae1 commit 0926808
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 84 deletions.
85 changes: 1 addition & 84 deletions packages/dependency-graph-integrator/src/pull-requests.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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();
});
});

0 comments on commit 0926808

Please sign in to comment.