Skip to content

Commit

Permalink
spec: improve Clerk testing (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored Sep 18, 2024
1 parent 59b1ae0 commit 3cbf6de
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"husky": "^8.0.0",
"jest": "^29.0.3",
"lint-staged": "^13.0.4",
"nock": "^13.5.5",
"prettier": "^2.7.1",
"ts-jest": "^29.0.1",
"typescript": "^5.6.2"
Expand Down
214 changes: 214 additions & 0 deletions spec/comment.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { Probot, Context } from 'probot';
import { probotRunner } from '../src/index';
import * as noteUtils from '../src/note-utils';
import { SEMANTIC_BUILD_PREFIX } from '../src/constants';
import { PullRequestOpenedEvent, PullRequestClosedEvent } from '@octokit/webhooks-types';

const nock = require('nock');

const GH_API = 'https://api.github.com';

describe('probotRunner', () => {
let probot: Probot;

beforeEach(() => {
nock.disableNetConnect();

probot = new Probot({
// ruby -rsecurerandom -e 'puts SecureRandom.hex(20)'
privateKey: '9489ead8d9cb3566ba761a2c3dd278822f8d1205',
appId: 690857,
});

probot.load(probotRunner);
});

afterEach(() => {
jest.clearAllMocks();
nock.cleanAll();
nock.enableNetConnect();
});

it('should post a failure status if release notes are missing', async () => {
jest.spyOn(noteUtils, 'findNoteInPRBody').mockReturnValue(null);

const payload = {
action: 'opened',
pull_request: {
number: 1,
body: 'Fixes something broken',
title: 'fix: something broken',
user: { login: 'codebytere' },
head: { sha: 'abc123' },
state: 'open',
merged: false,
},
repository: {
name: 'electron',
owner: { login: 'electron' },
},
} as PullRequestOpenedEvent;

nock(GH_API)
.post(
`/repos/electron/electron/statuses/${payload.pull_request.head.sha}`,
(body: Record<string, string>) => {
expect(body).toMatchObject({
context: 'release-notes',
description: 'Missing release notes',
state: 'failure',
});
return true;
},
)
.reply(200);

await probot.receive({ id: '123', name: 'pull_request', payload });
});

it('should add "Notes: none" to Dependabot PR body', async () => {
jest.spyOn(noteUtils, 'findNoteInPRBody').mockReturnValue(null);

const payload = {
action: 'opened',
pull_request: {
number: 1,
title: 'chore(deps): bump lodash from 4.17.15 to 4.17.19',
body: 'Update lodash to the latest version',
user: {
login: 'dependabot[bot]',
},
},
} as PullRequestOpenedEvent;

nock(GH_API)
.patch(
`/repos/electron/electron/pulls/${payload.pull_request.number}`,
(body: Record<string, string>) => {
expect(body).toMatchObject({
body: 'This is a test PR\n\n---\n\nNotes: none',
});
return true;
},
)
.reply(200);

await probot.receive({ id: '123', name: 'pull_request', payload });
});

it('should add "Notes: none" to build PR body', async () => {
jest.spyOn(noteUtils, 'findNoteInPRBody').mockReturnValue(null);

const payload = {
action: 'opened',
pull_request: {
number: 1,
title: `${SEMANTIC_BUILD_PREFIX} Build PR`,
body: 'Fix something to do with GitHub Actions',
user: { login: 'codebytere' },
},
} as PullRequestOpenedEvent;

nock(GH_API)
.patch(
`/repos/electron/electron/pulls/${payload.pull_request.number}`,
(body: Record<string, string>) => {
expect(body).toMatchObject({
body: 'This is a test PR\n\n---\n\nNotes: none',
});
return true;
},
)
.reply(200);

await probot.receive({ id: '123', name: 'pull_request', payload });
});

it('should post a success status if release notes are found', async () => {
jest.spyOn(noteUtils, 'findNoteInPRBody').mockReturnValue('Notes: added a new feature');

const payload = {
action: 'opened',
pull_request: {
number: 1,
body: 'Notes: Added a new feature',
title: 'feat: add new exciting feature',
user: { login: 'codebytere' },
head: { sha: 'abc123' },
state: 'open',
merged: false,
},
repository: {
name: 'electron',
owner: { login: 'electron' },
},
} as PullRequestOpenedEvent;

nock(GH_API)
.post(
`/repos/electron/electron/statuses/${payload.pull_request.head.sha}`,
(body: Record<string, string>) => {
expect(body).toMatchObject({
context: 'release-notes',
description: 'Release notes found',
state: 'success',
});
return true;
},
)
.reply(200);

await probot.receive({ id: '123', name: 'pull_request', payload });
});

it('should create a comment if release notes are found and shouldComment is true', async () => {
const releaseNotesComment = 'Comment from release notes';
jest.spyOn(noteUtils, 'findNoteInPRBody').mockReturnValue('Added a new feature');
jest.spyOn(noteUtils, 'createPRCommentFromNotes').mockReturnValue(releaseNotesComment);

const payload = {
action: 'closed',
pull_request: {
number: 1,
body: 'Notes: Added a new feature',
title: 'feat: add new exciting feature',
user: { login: 'codebytere' },
head: { sha: 'abc123' },
state: 'closed',
merged: true,
},
repository: {
name: 'electron',
owner: { login: 'electron' },
},
} as PullRequestClosedEvent;

nock(GH_API)
.post(
`/repos/electron/electron/statuses/${payload.pull_request.head.sha}`,
(body: Record<string, string>) => {
expect(body).toMatchObject({
context: 'release-notes',
description: 'Release notes found',
state: 'success',
});
return true;
},
)
.reply(200);

nock(GH_API)
.post(
`/repos/electron/electron/issues/${payload.pull_request.number}/comments`,
(body: Record<string, string>) => {
expect(body).toMatchObject({
body: releaseNotesComment,
});
return true;
},
)
.reply(200);

await probot.receive({ id: '123', name: 'pull_request', payload });
});
});
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const submitFeedbackForPR = async (
}
};

const probotRunner = (app: Probot) => {
export const probotRunner = (app: Probot) => {
app.on('pull_request', async (context) => {
const pr = context.payload.pull_request;

Expand All @@ -84,5 +84,3 @@ const probotRunner = (app: Probot) => {
}
});
};

module.exports = probotRunner;
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3017,6 +3017,11 @@ json-parse-even-better-errors@^2.3.0:
resolved "https://registry.yarnpkg.com/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz#7c47805a94319928e05777405dc12e1f7a4ee02d"
integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==

json-stringify-safe@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==

json5@^2.2.1:
version "2.2.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
Expand Down Expand Up @@ -3330,6 +3335,15 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

nock@^13.5.5:
version "13.5.5"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.5.5.tgz#cd1caaca281d42be17d51946367a3d53a6af3e78"
integrity sha512-XKYnqUrCwXC8DGG1xX4YH5yNIrlh9c065uaMZZHUoeUUINTOyt+x/G+ezYk0Ft6ExSREVIs+qBJDK503viTfFA==
dependencies:
debug "^4.1.0"
json-stringify-safe "^5.0.1"
propagate "^2.0.0"

node-fetch@^2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
Expand Down Expand Up @@ -3703,6 +3717,11 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.4"

propagate@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==

proxy-addr@~2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025"
Expand Down

0 comments on commit 3cbf6de

Please sign in to comment.