Skip to content

Commit a314694

Browse files
committed
test: fix pull request payload types
1 parent 864a9e3 commit a314694

File tree

3 files changed

+81
-191
lines changed

3 files changed

+81
-191
lines changed

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 () => {

test/routes/github/pull_request.spec.ts

Lines changed: 57 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { initServer } from '../../../src/server';
22
import { getConfig } from '../../../src/config';
33
import { StreamLabs } from '../../../src/services/StreamLabs';
44
import { TwitchChat } from '../../../src/services/TwitchChat';
5+
import { PullRequestPayloadBuilder } from '../../builders/github/pull-request-payload-builder';
56

67
describe('POST /github', () => {
78
describe("GitHub 'pull_request' event", () => {
@@ -16,189 +17,72 @@ describe('POST /github', () => {
1617
spyTwitchChat.mockImplementationOnce(jest.fn());
1718
});
1819

19-
describe('StreamLabs alerts', () => {
20-
it('sends the notification to StreamLabs when a pull request was opened', async () => {
21-
const subject = await initServer(getConfig());
22-
const repositoryFullName = 'streamdevs/webhook';
23-
const pullRequestLogin = 'SantiMA10';
20+
it('handles the pull request opened event', async () => {
21+
const subject = await initServer(getConfig());
22+
const payload = new PullRequestPayloadBuilder().with({ action: 'opened' }).getInstance();
2423

25-
await subject.inject({
26-
method: 'POST',
27-
url: '/github',
28-
payload: {
29-
action: 'opened',
30-
repository: { full_name: repositoryFullName },
31-
pull_request: { user: { login: pullRequestLogin } },
32-
sender: { login: 'pepe' },
33-
},
34-
headers: { 'x-github-event': 'pull_request' },
35-
});
36-
37-
const expectedPayload = {
38-
message: `*${pullRequestLogin}* just opened a pull request in *${repositoryFullName}*`,
39-
};
40-
expect(spyStreamLabs).toHaveBeenCalledWith(expectedPayload);
41-
});
42-
43-
it("ignores other 'pull_request' event", async () => {
44-
const subject = await initServer(getConfig());
45-
const repositoryFullName = 'streamdevs/webhook';
46-
const pullRequestLogin = 'SantiMA10';
47-
48-
const { statusCode } = await subject.inject({
49-
method: 'POST',
50-
url: '/github',
51-
payload: {
52-
action: 'assigned',
53-
repository: { full_name: repositoryFullName },
54-
pull_request: { user: { login: pullRequestLogin } },
55-
sender: { login: 'pepe' },
56-
},
57-
headers: { 'x-github-event': 'pull_request' },
58-
});
59-
60-
expect(spyStreamLabs).not.toHaveBeenCalled();
61-
expect(statusCode).toBe(200);
62-
});
63-
64-
it("ignores other 'closed' event when it is not merged", async () => {
65-
const subject = await initServer(getConfig());
66-
const repositoryFullName = 'streamdevs/webhook';
67-
const pullRequestLogin = 'SantiMA10';
68-
69-
const { statusCode } = await subject.inject({
70-
method: 'POST',
71-
url: '/github',
72-
payload: {
73-
action: 'closed',
74-
repository: { full_name: repositoryFullName },
75-
pull_request: { user: { login: pullRequestLogin } },
76-
sender: { login: 'pepe' },
77-
},
78-
headers: { 'x-github-event': 'pull_request' },
79-
});
80-
81-
expect(spyStreamLabs).not.toHaveBeenCalled();
82-
expect(statusCode).toEqual(200);
24+
const { result } = await subject.inject({
25+
method: 'POST',
26+
url: '/github',
27+
payload,
28+
headers: { 'x-github-event': 'pull_request' },
8329
});
8430

85-
it('sends a notification to StreamLabs when a pull request was merged', async () => {
86-
const subject = await initServer(getConfig());
87-
const repositoryFullName = 'streamdevs/webhook';
88-
const pullRequestLogin = 'SantiMA10';
89-
90-
await subject.inject({
91-
method: 'POST',
92-
url: '/github',
93-
payload: {
94-
action: 'closed',
95-
repository: { full_name: repositoryFullName },
96-
pull_request: { user: { login: pullRequestLogin }, merged: true },
97-
sender: { login: 'pepe' },
98-
},
99-
headers: { 'x-github-event': 'pull_request' },
100-
});
101-
102-
const expectedPayload = {
103-
message: `The pull request from *${pullRequestLogin}* has been merged into *${repositoryFullName}*`,
104-
};
105-
106-
expect(spyStreamLabs).toHaveBeenCalledWith(expectedPayload);
107-
});
31+
expect(result).toEqual(
32+
expect.objectContaining({
33+
messages: [
34+
expect.objectContaining({
35+
twitchChat: expect.anything(),
36+
streamlabs: expect.anything(),
37+
}),
38+
],
39+
}),
40+
);
10841
});
10942

110-
describe('TwitchChat send', () => {
111-
it('sends the message to the Twitch chat when a pull request was opened', async () => {
112-
const subject = await initServer(getConfig());
113-
const repositoryUrl = 'https://github.com/streamdevs/webhook';
114-
const pullRequestLogin = 'SantiMA10';
115-
116-
await subject.inject({
117-
method: 'POST',
118-
url: '/github',
119-
payload: {
120-
action: 'opened',
121-
repository: {
122-
full_name: 'streamdevs/webhook',
123-
html_url: repositoryUrl,
124-
},
125-
pull_request: { user: { login: pullRequestLogin } },
126-
sender: { login: 'pepe' },
127-
},
128-
headers: { 'x-github-event': 'pull_request' },
129-
});
130-
131-
expect(spyTwitchChat).toHaveBeenCalledWith(
132-
`${pullRequestLogin} just opened a pull request in ${repositoryUrl}`,
133-
);
134-
});
135-
136-
it('sends the message to the Twitch chat when a pull request was merged', async () => {
137-
const subject = await initServer(getConfig());
138-
const repositoryUrl = 'https://github.com/streamdevs/webhook';
139-
const pullRequestLogin = 'SantiMA10';
140-
141-
await subject.inject({
142-
method: 'POST',
143-
url: '/github',
144-
payload: {
145-
action: 'closed',
146-
repository: {
147-
full_name: 'streamdevs/webhook',
148-
html_url: repositoryUrl,
149-
},
150-
pull_request: { user: { login: pullRequestLogin }, merged: true },
151-
sender: { login: 'pepe' },
152-
},
153-
headers: { 'x-github-event': 'pull_request' },
154-
});
155-
156-
expect(spyTwitchChat).toHaveBeenCalledWith(
157-
`The pull request from ${pullRequestLogin} has been merged into ${repositoryUrl}`,
158-
);
43+
it('handles the pull request merged event', async () => {
44+
const subject = await initServer(getConfig());
45+
const payload = new PullRequestPayloadBuilder()
46+
.with({ action: 'closed', pull_request: { merged: true } })
47+
.getInstance();
48+
49+
const { result } = await subject.inject({
50+
method: 'POST',
51+
url: '/github',
52+
payload,
53+
headers: { 'x-github-event': 'pull_request' },
15954
});
16055

161-
it("ignores other 'pull_request' event", async () => {
162-
const subject = await initServer(getConfig());
163-
const repositoryFullName = 'streamdevs/webhook';
164-
const pullRequestLogin = 'SantiMA10';
165-
166-
const { statusCode } = await subject.inject({
167-
method: 'POST',
168-
url: '/github',
169-
payload: {
170-
action: 'assigned',
171-
repository: { full_name: repositoryFullName },
172-
pull_request: { user: { login: pullRequestLogin } },
173-
sender: { login: 'pepe' },
174-
},
175-
headers: { 'x-github-event': 'pull_request' },
176-
});
56+
expect(result).toEqual(
57+
expect.objectContaining({
58+
messages: [
59+
expect.objectContaining({
60+
twitchChat: expect.anything(),
61+
streamlabs: expect.anything(),
62+
}),
63+
],
64+
}),
65+
);
66+
});
17767

178-
expect(spyTwitchChat).not.toHaveBeenCalled();
179-
expect(statusCode).toBe(200);
68+
it('ignores other pull request events', async () => {
69+
const subject = await initServer(getConfig());
70+
const payload = new PullRequestPayloadBuilder()
71+
.with({ action: 'edited', pull_request: { merged: true } })
72+
.getInstance();
73+
74+
const { result } = await subject.inject({
75+
method: 'POST',
76+
url: '/github',
77+
payload,
78+
headers: { 'x-github-event': 'pull_request' },
18079
});
18180

182-
it("ignores other 'closed' event when it is not merged", async () => {
183-
const subject = await initServer(getConfig());
184-
const repositoryFullName = 'streamdevs/webhook';
185-
const pullRequestLogin = 'SantiMA10';
186-
187-
const { statusCode } = await subject.inject({
188-
method: 'POST',
189-
url: '/github',
190-
payload: {
191-
action: 'closed',
192-
repository: { full_name: repositoryFullName },
193-
pull_request: { user: { login: pullRequestLogin } },
194-
sender: { login: 'pepe' },
195-
},
196-
headers: { 'x-github-event': 'pull_request' },
197-
});
198-
199-
expect(spyTwitchChat).not.toHaveBeenCalled();
200-
expect(statusCode).toEqual(200);
201-
});
81+
expect(result).toEqual(
82+
expect.objectContaining({
83+
message: expect.anything(),
84+
}),
85+
);
20286
});
20387
});
20488
});

0 commit comments

Comments
 (0)