Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(platform/azure): coverage #17269

Merged
merged 4 commits into from
Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
150 changes: 97 additions & 53 deletions lib/modules/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,34 @@ describe('modules/platform/azure/index', () => {
});

function initRepo(args?: Partial<RepoParams> | string) {
azureApi.gitApi.mockImplementationOnce(
() =>
({
getRepositories: jest.fn(() => [
{
name: 'repo',
id: '1',
privateRepo: true,
isFork: false,
defaultBranch: 'defBr',
project: {
name: 'some',
},
azureApi.gitApi.mockResolvedValueOnce(
partial<IGitApi>({
getRepositories: jest.fn().mockResolvedValue([
{
name: 'repo',
id: '1',
privateRepo: true,
isFork: false,
defaultBranch: 'defBr',
project: {
name: 'some',
},
{
name: 'repo2',
project: {
name: 'prj2',
},
},
{
name: 'repo2',
project: {
name: 'prj2',
},
{
name: 'repo3',
project: {
name: 'some',
},
isDisabled: true,
},
{
name: 'repo3',
project: {
name: 'some',
},
]),
} as any)
isDisabled: true,
},
]),
})
);

if (is.string(args)) {
Expand Down Expand Up @@ -317,6 +316,19 @@ describe('modules/platform/azure/index', () => {
});
expect(res).toMatchSnapshot();
});

it('catches errors', async () => {
azureApi.gitApi.mockResolvedValueOnce(
partial<IGitApi>({
getPullRequests: jest.fn().mockRejectedValueOnce(new Error()),
})
);
const res = await azure.findPr({
branchName: 'branch-a',
prTitle: 'branch a pr',
});
expect(res).toBeNull();
});
});

describe('getPrList()', () => {
Expand All @@ -334,41 +346,51 @@ describe('modules/platform/azure/index', () => {
describe('getBranchPr(branchName)', () => {
it('should return null if no PR exists', async () => {
await initRepo({ repository: 'some/repo' });
azureApi.gitApi.mockImplementationOnce(
() =>
({
findPr: jest.fn(() => false),
getPr: jest.fn(() => {
'myPRName';
}),
} as any)
azureApi.gitApi.mockResolvedValue(
partial<IGitApi>({
getPullRequests: jest.fn().mockResolvedValueOnce([]),
})
);
const pr = await azure.getBranchPr('somebranch');
expect(pr).toBeNull();
});

it('should return the pr', async () => {
await initRepo({ repository: 'some/repo' });
azureApi.gitApi.mockImplementation(
() =>
({
getPullRequests: jest
.fn()
.mockReturnValue([])
.mockReturnValueOnce([
{
pullRequestId: 1,
sourceRefName: 'refs/heads/branch-a',
title: 'branch a pr',
status: 2,
},
]),
getPullRequestCommits: jest.fn().mockReturnValue([]),
} as any)
azureApi.gitApi.mockResolvedValue(
partial<IGitApi>({
getPullRequests: jest
.fn()
.mockResolvedValueOnce([
{
pullRequestId: 1,
sourceRefName: 'refs/heads/branch-a',
title: 'branch a pr',
status: 1,
},
])
.mockResolvedValueOnce([]),
getPullRequestLabels: jest.fn().mockResolvedValue([]),
})
);
const pr = await azure.getBranchPr('somebranch');
// TODO: should this return a PR instead?
expect(pr).toBeNull();
const pr = await azure.getBranchPr('branch-a');
expect(pr).toEqual({
bodyStruct: {
hash: 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
},
createdAt: undefined,
displayNumber: 'Pull Request #1',
hasReviewers: false,
labels: [],
number: 1,
pullRequestId: 1,
sourceBranch: 'branch-a',
sourceRefName: 'refs/heads/branch-a',
state: 'open',
status: 1,
targetBranch: undefined,
title: 'branch a pr',
});
});
});

Expand Down Expand Up @@ -499,6 +521,28 @@ describe('modules/platform/azure/index', () => {
expect(res).toBe(BranchStatus.yellow);
});

it('should return yellow if status is unknown', async () => {
await initRepo({ repository: 'some/repo' });
azureApi.gitApi.mockResolvedValueOnce(
partial<IGitApi>({
getBranch: jest
.fn()
.mockResolvedValue({ commit: { commitId: 'abcd1234' } }),
getStatuses: jest.fn().mockResolvedValue([
{
state: -1,
context: { genre: 'a-genre', name: 'a-name' },
},
]),
})
);
const res = await azure.getBranchStatusCheck(
'somebranch',
'a-genre/a-name'
);
expect(res).toBe(BranchStatus.yellow);
});

it('should return null if status not found', async () => {
await initRepo({ repository: 'some/repo' });
azureApi.gitApi.mockImplementationOnce(
Expand Down