Skip to content

Commit ca611de

Browse files
authored
Merge pull request #185 from streamdevs/179-pull-request-payload-builder
test: new 'PullRequestPayloadBuilder'
2 parents 8964409 + a314694 commit ca611de

File tree

7 files changed

+141
-195
lines changed

7 files changed

+141
-195
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"tmi.js": "^1.5.0"
2323
},
2424
"devDependencies": {
25-
"lodash": "^4.17.15",
2625
"@types/faker": "^4.1.11",
2726
"@types/hapi__hapi": "^19.0.2",
2827
"@types/hapi__joi": "^16.0.12",
@@ -36,10 +35,12 @@
3635
"husky": "^4.2.5",
3736
"jest": "^25.2.7",
3837
"lint-staged": ">=10",
38+
"lodash": "^4.17.15",
3939
"nock": "^12.0.3",
4040
"nodemon": "^2.0.3",
4141
"prettier": "^2.0.4",
4242
"ts-jest": "^25.4.0",
43-
"typescript": "^3.8.3"
43+
"typescript": "^3.8.3",
44+
"utility-types": "^3.10.0"
4445
}
4546
}
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import { RepositoryWebhookPayload } from './repository-webhook-payload';
22

33
export interface PullRequestPayload extends RepositoryWebhookPayload {
4-
action: string;
5-
pull_request: { user: { login: string }; merged?: boolean };
4+
action:
5+
| 'assigned'
6+
| 'unassigned'
7+
| 'review_requested'
8+
| 'review_request_removed'
9+
| 'labeled'
10+
| 'unlabeled'
11+
| 'opened'
12+
| 'edited'
13+
| 'closed'
14+
| 'ready_for_review'
15+
| 'locked'
16+
| 'unlocked'
17+
| 'reopened';
18+
pull_request: { user: { login: string }; merged?: boolean; html_url: string };
619
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { internet, random } from 'faker';
2+
import { merge } from 'lodash';
3+
import { PullRequestPayload } from '../../../src/schemas/github/pull-request-payload';
4+
import { DeepPartial } from 'utility-types';
5+
6+
export class PullRequestPayloadBuilder {
7+
private payload: PullRequestPayload = {
8+
action: random.arrayElement([
9+
'assigned',
10+
'unassigned',
11+
'review_requested',
12+
'review_request_removed',
13+
'labeled',
14+
'unlabeled',
15+
'opened',
16+
'edited',
17+
'closed',
18+
'ready_for_review',
19+
'locked',
20+
'unlocked',
21+
'reopened',
22+
]),
23+
pull_request: { user: { login: internet.userName() }, html_url: internet.url() },
24+
repository: { html_url: internet.url(), full_name: 'streamdevs/webhook' },
25+
sender: { login: internet.userName() },
26+
};
27+
28+
public with(payload: DeepPartial<PullRequestPayload>): PullRequestPayloadBuilder {
29+
merge(this.payload, payload);
30+
31+
return this;
32+
}
33+
34+
public getInstance(): PullRequestPayload {
35+
return this.payload;
36+
}
37+
}

test/reactions/github/pull-request-merged.spec.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PullRequestMerged } from '../../../src/reactions/github/pull-request-me
22
import { PullRequestPayload } from '../../../src/schemas/github/pull-request-payload';
33
import { TwitchChatMock } from '../../__mocks__/TwitchChat';
44
import { StreamLabsMock } from '../../__mocks__/StreamLabs';
5+
import { PullRequestPayloadBuilder } from '../../builders/github/pull-request-payload-builder';
56

67
describe('PullRequestMerged', () => {
78
let streamlabs: StreamLabsMock;
@@ -16,15 +17,17 @@ describe('PullRequestMerged', () => {
1617
let payload: PullRequestPayload;
1718

1819
beforeEach(() => {
19-
payload = {
20-
action: 'closed',
21-
repository: {
22-
full_name: 'streamdevs/webhook',
23-
html_url: 'https://github.com/streamdevs/webhook',
24-
},
25-
pull_request: { user: { login: 'SantiMA10' }, merged: true },
26-
sender: { login: 'pepe' },
27-
};
20+
payload = new PullRequestPayloadBuilder()
21+
.with({
22+
action: 'closed',
23+
repository: {
24+
full_name: 'streamdevs/webhook',
25+
html_url: 'https://github.com/streamdevs/webhook',
26+
},
27+
pull_request: { user: { login: 'SantiMA10' }, merged: true },
28+
sender: { login: 'pepe' },
29+
})
30+
.getInstance();
2831
});
2932

3033
it("returns 'twitchChat.notified' === false if something goes wrong with TwitchChat", async () => {

test/reactions/github/pull-request-opened.spec.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PullRequestPayload } from '../../../src/schemas/github/pull-request-pay
33
import { StreamLabsMock } from '../../__mocks__/StreamLabs';
44
import { TwitchChatMock } from '../../__mocks__/TwitchChat';
55
import { Config } from '../../../src/config';
6+
import { PullRequestPayloadBuilder } from '../../builders/github/pull-request-payload-builder';
67

78
describe('PullRequestOpened', () => {
89
let streamlabs: StreamLabsMock;
@@ -17,15 +18,17 @@ describe('PullRequestOpened', () => {
1718
let payload: PullRequestPayload;
1819

1920
beforeEach(() => {
20-
payload = {
21-
action: 'opened',
22-
repository: {
23-
full_name: 'streamdevs/webhook',
24-
html_url: 'https://github.com/streamdevs/webhook',
25-
},
26-
pull_request: { user: { login: 'SantiMA10' } },
27-
sender: { login: 'pepe' },
28-
};
21+
payload = new PullRequestPayloadBuilder()
22+
.with({
23+
action: 'opened',
24+
repository: {
25+
full_name: 'streamdevs/webhook',
26+
html_url: 'https://github.com/streamdevs/webhook',
27+
},
28+
pull_request: { user: { login: 'SantiMA10' } },
29+
sender: { login: 'pepe' },
30+
})
31+
.getInstance();
2932
});
3033

3134
it("returns 'twitchChat.notified' === false if something goes wrong with TwitchChat", async () => {

0 commit comments

Comments
 (0)