From 11de3c9e89d63287d80699dfd019ed35b1d21208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Mart=C3=ADn=20Agra?= Date: Tue, 21 Apr 2020 20:52:12 +0200 Subject: [PATCH 1/2] feat: new 'PullRequestMerged' 'Reaction' --- src/reactions/github/pull-request-merged.ts | 21 +++ .../github/pull-request-merged.spec.ts | 128 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 src/reactions/github/pull-request-merged.ts create mode 100644 test/reactions/github/pull-request-merged.spec.ts diff --git a/src/reactions/github/pull-request-merged.ts b/src/reactions/github/pull-request-merged.ts new file mode 100644 index 0000000..fb427ed --- /dev/null +++ b/src/reactions/github/pull-request-merged.ts @@ -0,0 +1,21 @@ +import { + Reaction, + ReactionCanHandleOptions, + ReactionHandleOptions, +} from './reaction'; + +export class PullRequestMerged extends Reaction { + getStreamLabsMessage({ payload }: ReactionHandleOptions): string { + return `The pull request from *${payload.pull_request.user.login}* has been merged into *${payload.repository.full_name}*`; + } + getTwitchChatMessage({ payload }: ReactionHandleOptions): string { + return `The pull request from *${payload.pull_request.user.login}* has been merged into ${payload.repository.html_url}`; + } + canHandle({ payload, event }: ReactionCanHandleOptions): boolean { + return ( + event === 'pull_request' && + payload.pull_request.merged && + payload.action === 'closed' + ); + } +} diff --git a/test/reactions/github/pull-request-merged.spec.ts b/test/reactions/github/pull-request-merged.spec.ts new file mode 100644 index 0000000..0985515 --- /dev/null +++ b/test/reactions/github/pull-request-merged.spec.ts @@ -0,0 +1,128 @@ +import { PullRequestMerged } from '../../../src/reactions/github/pull-request-merged'; +import { TwitchChat } from '../../../src/services/TwitchChat'; +import { StreamLabs } from '../../../src/services/StreamLabs'; + +describe('PullRequestMerged', () => { + describe('#handle', () => { + let payload: any; + let streamlabs: StreamLabs; + let twitchChat: TwitchChat; + + beforeEach(() => { + payload = { + action: 'closed', + repository: { + full_name: 'streamdevs/webhook', + html_url: 'https://github.com/streamdevs/webhook', + }, + pull_request: { user: { login: 'SantiMA10' }, merged: true }, + sender: { login: 'pepe' }, + }; + + twitchChat = ({ + send: jest.fn(), + } as unknown) as TwitchChat; + streamlabs = ({ + alert: jest.fn(), + } as unknown) as StreamLabs; + }); + + it("returns 'twitchChat.notified' if something goes wrong with TwitchChat", async () => { + const twitchChat = ({ + send: jest.fn(async () => { + throw new Error('boom'); + }), + } as unknown) as TwitchChat; + const subject = new PullRequestMerged(twitchChat, streamlabs); + + const { + twitchChat: { notified }, + } = await subject.handle({ payload }); + + expect(notified).toEqual(false); + }); + + it("returns 'streamlabs.notified' if something goes wrong with StreamLabs", async () => { + const streamlabs = ({ + alert: jest.fn(async () => { + throw new Error('boom'); + }), + } as unknown) as StreamLabs; + const subject = new PullRequestMerged(twitchChat, streamlabs); + + const { + streamlabs: { notified }, + } = await subject.handle({ payload }); + + expect(notified).toEqual(false); + }); + + it("returns 'twitchChat' with the send message and notified set to true if everything goes well", async () => { + const subject = new PullRequestMerged(twitchChat, streamlabs); + + const { twitchChat: response } = await subject.handle({ payload }); + + expect(response).toEqual({ + notified: true, + message: `The pull request from *${payload.pull_request.user.login}* has been merged into ${payload.repository.html_url}`, + }); + }); + + it("returns 'streamlabs' with the send message and notified set to true if everything goes well", async () => { + const subject = new PullRequestMerged(twitchChat, streamlabs); + + const { streamlabs: response } = await subject.handle({ payload }); + + expect(response).toEqual({ + notified: true, + message: `The pull request from *${payload.pull_request.user.login}* has been merged into *${payload.repository.full_name}*`, + }); + }); + }); + + describe('#canHandle', () => { + it("returns true when the event is 'pull_request' and the payload contains action 'closed' and 'merged' set to 'true'", () => { + const subject = new PullRequestMerged(null as any, null as any); + + const result = subject.canHandle({ + event: 'pull_request', + payload: { action: 'closed', pull_request: { merged: true } }, + }); + + expect(result).toEqual(true); + }); + + it("returns false when the event is not 'pull_request'", () => { + const subject = new PullRequestMerged(null as any, null as any); + + const result = subject.canHandle({ + event: 'fork', + payload: { action: 'closed', pull_request: { merged: true } }, + }); + + expect(result).toEqual(false); + }); + + it("returns false when the event is 'pull_request' but is not merged", () => { + const subject = new PullRequestMerged(null as any, null as any); + + const result = subject.canHandle({ + event: 'pull_request', + payload: { action: 'closed', pull_request: { merged: false } }, + }); + + expect(result).toEqual(false); + }); + + it("returns false when the event is 'pull_request' but the action is not closed", () => { + const subject = new PullRequestMerged(null as any, null as any); + + const result = subject.canHandle({ + event: 'pull_request', + payload: { action: 'opened', pull_request: { merged: true } }, + }); + + expect(result).toEqual(false); + }); + }); +}); From 205675e436278a6900feefef3c7181b6be2efe8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Mart=C3=ADn=20Agra?= Date: Tue, 21 Apr 2020 21:36:11 +0200 Subject: [PATCH 2/2] test: improve tests descriptions --- test/reactions/github/pull-request-merged.spec.ts | 4 ++-- test/reactions/github/pull-request-opened.spec.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/reactions/github/pull-request-merged.spec.ts b/test/reactions/github/pull-request-merged.spec.ts index 0985515..5f45cd3 100644 --- a/test/reactions/github/pull-request-merged.spec.ts +++ b/test/reactions/github/pull-request-merged.spec.ts @@ -27,7 +27,7 @@ describe('PullRequestMerged', () => { } as unknown) as StreamLabs; }); - it("returns 'twitchChat.notified' if something goes wrong with TwitchChat", async () => { + it("returns 'twitchChat.notified' === false if something goes wrong with TwitchChat", async () => { const twitchChat = ({ send: jest.fn(async () => { throw new Error('boom'); @@ -42,7 +42,7 @@ describe('PullRequestMerged', () => { expect(notified).toEqual(false); }); - it("returns 'streamlabs.notified' if something goes wrong with StreamLabs", async () => { + it("returns 'streamlabs.notified' === false if something goes wrong with StreamLabs", async () => { const streamlabs = ({ alert: jest.fn(async () => { throw new Error('boom'); diff --git a/test/reactions/github/pull-request-opened.spec.ts b/test/reactions/github/pull-request-opened.spec.ts index afa8878..cbda21e 100644 --- a/test/reactions/github/pull-request-opened.spec.ts +++ b/test/reactions/github/pull-request-opened.spec.ts @@ -27,7 +27,7 @@ describe('PullRequestOpened', () => { } as unknown) as StreamLabs; }); - it("returns 'twitchChat.notified' if something goes wrong with TwitchChat", async () => { + it("returns 'twitchChat.notified' === false if something goes wrong with TwitchChat", async () => { const twitchChat = ({ send: jest.fn(async () => { throw new Error('boom'); @@ -42,7 +42,7 @@ describe('PullRequestOpened', () => { expect(notified).toEqual(false); }); - it("returns 'streamlabs.notified' if something goes wrong with StreamLabs", async () => { + it("returns 'streamlabs.notified' === false if something goes wrong with StreamLabs", async () => { const streamlabs = ({ alert: jest.fn(async () => { throw new Error('boom');