Refactor tests a bit and fix typings

Signed-off-by: Francesco Saltori <francescosaltori@gmail.com>
This commit is contained in:
Francesco Saltori
2022-08-05 12:56:35 +02:00
parent 4a7315c5f4
commit 1cb5e2cbfe
@@ -26,7 +26,6 @@ import { resolve as resolvePath } from 'path';
import { Writable } from 'stream';
import { ActionContext, TemplateAction } from '../../types';
import {
CreateGithubPullRequestClientFactoryInput,
createPublishGithubPullRequestAction,
OctokitWithPullRequestPluginClient,
} from './githubPullRequest';
@@ -42,11 +41,12 @@ type GithubPullRequestActionInput = ReturnType<
describe('createPublishGithubPullRequestAction', () => {
let instance: TemplateAction<GithubPullRequestActionInput>;
let fakeClient: OctokitWithPullRequestPluginClient;
let clientFactory: (
input: CreateGithubPullRequestClientFactoryInput,
) => Promise<OctokitWithPullRequestPluginClient>;
let fakeClient: {
createPullRequest: jest.Mock;
rest: {
pulls: { requestReviewers: jest.Mock };
};
};
beforeEach(() => {
const integrations = ScmIntegrations.fromConfig(new ConfigReader({}));
@@ -64,11 +64,13 @@ describe('createPublishGithubPullRequestAction', () => {
}),
rest: {
pulls: {
requestReviewers: jest.fn(async (_: any) => ({ data: {} }))
}
}
requestReviewers: jest.fn(async (_: any) => ({ data: {} })),
},
},
};
clientFactory = jest.fn(async () => fakeClient);
const clientFactory = jest.fn(
async () => fakeClient as unknown as OctokitWithPullRequestPluginClient,
);
const githubCredentialsProvider: GithubCredentialsProvider = {
getCredentials: jest.fn(),
};
@@ -80,6 +82,11 @@ describe('createPublishGithubPullRequestAction', () => {
});
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
describe('with no sourcePath', () => {
let input: GithubPullRequestActionInput;
let ctx: ActionContext<GithubPullRequestActionInput>;
@@ -106,6 +113,7 @@ describe('createPublishGithubPullRequestAction', () => {
workspacePath,
};
});
it('creates a pull request', async () => {
await instance.handler(ctx);
@@ -140,10 +148,6 @@ describe('createPublishGithubPullRequestAction', () => {
);
expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123);
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
});
describe('with sourcePath', () => {
@@ -176,11 +180,6 @@ describe('createPublishGithubPullRequestAction', () => {
};
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
it('creates a pull request with only relevant files', async () => {
await instance.handler(ctx);
@@ -272,10 +271,6 @@ describe('createPublishGithubPullRequestAction', () => {
);
expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123);
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
});
describe('with reviewers and teamReviewers', () => {
@@ -289,7 +284,7 @@ describe('createPublishGithubPullRequestAction', () => {
branchName: 'new-app',
description: 'This PR is really good',
reviewers: ['foobar'],
teamReviewers: ['team-foo']
teamReviewers: ['team-foo'],
};
ctx = {
@@ -312,12 +307,14 @@ describe('createPublishGithubPullRequestAction', () => {
pull_number: 123,
reviewers: ['foobar'],
team_reviewers: ['team-foo'],
})
});
});
it('creates outputs for the pull request url and number even if requesting reviewers fails', async () => {
fakeClient.rest.pulls.requestReviewers.mockImplementation(() => { throw new Error('a random error') })
fakeClient.rest.pulls.requestReviewers.mockImplementation(() => {
throw new Error('a random error');
});
await instance.handler(ctx);
expect(ctx.output).toHaveBeenCalledWith(
@@ -326,12 +323,7 @@ describe('createPublishGithubPullRequestAction', () => {
);
expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123);
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
})
});
describe('with no reviewers and teamReviewers', () => {
let input: GithubPullRequestActionInput;
@@ -361,12 +353,7 @@ describe('createPublishGithubPullRequestAction', () => {
expect(fakeClient.createPullRequest).toBeCalled();
expect(fakeClient.rest.pulls.requestReviewers).not.toBeCalled();
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
})
});
describe('with executable file mode 755', () => {
let input: GithubPullRequestActionInput;
@@ -431,10 +418,6 @@ describe('createPublishGithubPullRequestAction', () => {
);
expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123);
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
});
describe('with executable file mode 775', () => {
@@ -500,9 +483,5 @@ describe('createPublishGithubPullRequestAction', () => {
);
expect(ctx.output).toHaveBeenCalledWith('pullRequestNumber', 123);
});
afterEach(() => {
mockFs.restore();
jest.resetAllMocks();
});
});
});