Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Souza committed Aug 4, 2020
1 parent 8ed6829 commit 9289fbb
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/eventHandlers/checkSuite/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { getOctokit } from '@actions/github';
import { OK } from 'http-status-codes';
import * as nock from 'nock';

import * as merge from '../../common/merge';
import { mergePullRequestMutation } from '../../graphql/mutations';
import { AllowedMergeMethods } from '../../utilities/inputParsers';
import * as log from '../../utilities/log';
import { checkSuiteHandle } from '.';

/* cspell:disable-next-line */
Expand Down Expand Up @@ -338,4 +340,64 @@ describe('check Suite event handler', (): void => {

expect(warningSpy).toHaveBeenCalled();
});

it('retries up to two times before failing', async (): Promise<void> => {
expect.assertions(6);

nock('https://api.github.com')
.post('/graphql')
.reply(OK, {
data: {
repository: {
pullRequest: {
commits: {
edges: [
{
node: {
commit: {
messageHeadline: COMMIT_HEADLINE,
},
},
},
],
},
id: PULL_REQUEST_ID,
mergeStateStatus: 'CLEAN',
mergeable: 'MERGEABLE',
merged: false,
reviews: {
edges: [
{
node: {
state: 'APPROVED',
},
},
],
},
state: 'OPEN',
},
},
},
});

const mergeSpy = jest
.spyOn(merge, 'merge')
.mockImplementation()
.mockRejectedValue(new Error('Error when merging'));
const logDebugSpy = jest.spyOn(log, 'logDebug');
const logInfoSpy = jest.spyOn(log, 'logInfo');

try {
await checkSuiteHandle(octokit, 'dependabot-preview[bot]', 2);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toStrictEqual('Error when merging');
expect(mergeSpy).toHaveBeenCalledTimes(3);
expect(logDebugSpy).toHaveBeenCalledTimes(3);
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
'An error ocurred while merging the Pull Request. This is usually caused by the base branch being out of sync with the target branch. In this case, the base branch must be rebased. Some tools, such as Dependabot, do that automatically.',
);
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 1000...');
}
}, 10000);
});
61 changes: 61 additions & 0 deletions src/eventHandlers/pullRequest/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { getOctokit } from '@actions/github';
import { OK } from 'http-status-codes';
import * as nock from 'nock';

import * as merge from '../../common/merge';
import { mergePullRequestMutation } from '../../graphql/mutations';
import { AllowedMergeMethods } from '../../utilities/inputParsers';
import * as log from '../../utilities/log';
import { pullRequestHandle } from '.';

/* cspell:disable-next-line */
Expand Down Expand Up @@ -131,4 +133,63 @@ describe('pull request event handler', (): void => {

await pullRequestHandle(octokit, 'dependabot-preview[bot]', 3);
});

it('retries up to two times before failing', async (): Promise<void> => {
expect.assertions(6);

nock('https://api.github.com')
.post('/graphql')
.reply(OK, {
data: {
repository: {
pullRequest: {
commits: {
edges: [
{
node: {
commit: {
message: COMMIT_HEADLINE,
},
},
},
],
},
id: PULL_REQUEST_ID,
mergeable: 'MERGEABLE',
merged: false,
reviews: {
edges: [
{
node: {
state: 'APPROVED',
},
},
],
},
state: 'OPEN',
},
},
},
});

const mergeSpy = jest
.spyOn(merge, 'merge')
.mockImplementation()
.mockRejectedValue(new Error('Error when merging'));
const logDebugSpy = jest.spyOn(log, 'logDebug');
const logInfoSpy = jest.spyOn(log, 'logInfo');

try {
await pullRequestHandle(octokit, 'dependabot-preview[bot]', 2);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toStrictEqual('Error when merging');
expect(mergeSpy).toHaveBeenCalledTimes(3);
expect(logDebugSpy).toHaveBeenCalledTimes(3);
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
'An error ocurred while merging the Pull Request. This is usually caused by the base branch being out of sync with the target branch. In this case, the base branch must be rebased. Some tools, such as Dependabot, do that automatically.',
);
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 1000...');
}
}, 10000);
});
54 changes: 54 additions & 0 deletions src/eventHandlers/push/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { getOctokit } from '@actions/github';
import { OK } from 'http-status-codes';
import * as nock from 'nock';

import * as merge from '../../common/merge';
import { mergePullRequestMutation } from '../../graphql/mutations';
import { AllowedMergeMethods } from '../../utilities/inputParsers';
import * as log from '../../utilities/log';
import { pushHandle } from '.';

/* cspell:disable-next-line */
Expand Down Expand Up @@ -258,4 +260,56 @@ describe('push event handler', (): void => {

expect(warningSpy).toHaveBeenCalled();
});

it('retries up to two times before failing', async (): Promise<void> => {
expect.assertions(6);

nock('https://api.github.com')
.post('/graphql')
.reply(OK, {
data: {
repository: {
pullRequests: {
nodes: [
{
id: PULL_REQUEST_ID,
mergeable: 'MERGEABLE',
merged: false,
reviews: {
edges: [
{
node: {
state: 'APPROVED',
},
},
],
},
state: 'OPEN',
},
],
},
},
},
});

const mergeSpy = jest
.spyOn(merge, 'merge')
.mockImplementation()
.mockRejectedValue(new Error('Error when merging'));
const logDebugSpy = jest.spyOn(log, 'logDebug');
const logInfoSpy = jest.spyOn(log, 'logInfo');

try {
await pushHandle(octokit, 'dependabot-preview[bot]', 2);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toStrictEqual('Error when merging');
expect(mergeSpy).toHaveBeenCalledTimes(3);
expect(logDebugSpy).toHaveBeenCalledTimes(3);
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
'An error ocurred while merging the Pull Request. This is usually caused by the base branch being out of sync with the target branch. In this case, the base branch must be rebased. Some tools, such as Dependabot, do that automatically.',
);
expect(logInfoSpy.mock.calls[2][0]).toStrictEqual('Retrying in 1000...');
}
}, 10000);
});

0 comments on commit 9289fbb

Please sign in to comment.