diff --git a/src/reactions/github/ping.ts b/src/reactions/github/ping.ts index 79a6832..a73ad97 100644 --- a/src/reactions/github/ping.ts +++ b/src/reactions/github/ping.ts @@ -20,24 +20,55 @@ export class Ping { this.twitchChat = twitchChat; } - public async handle({ payload }: HandleOptions) { - const streamlabsMessage = `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *${payload.hook.events}* events 🎉`; - await this.streamlabs.alert({ - message: streamlabsMessage, - }); - - const twitchMessage = `🎉 Your repo ${payload.repository.full_name} is configured correctly for ${payload.hook.events} events 🎉`; - await this.twitchChat.send(twitchMessage); + private async notifyStreamlabs({ payload }: HandleOptions) { + try { + const streamlabsMessage = `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *${payload.hook.events}* events 🎉`; + await this.streamlabs.alert({ + message: streamlabsMessage, + }); - return { - twitchChat: { - message: twitchMessage, + return { notified: true, - }, - streamlabs: { message: streamlabsMessage, + }; + } catch { + // TODO: add logging + + return { + notified: false, + message: '', + }; + } + } + + private async notifyTwitch({ payload }: HandleOptions) { + try { + const message = `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *${payload.hook.events}* events 🎉`; + await this.twitchChat.send(message); + + return { notified: true, - }, + message, + }; + } catch { + // TODO: add logging + + return { + notified: false, + message: '', + }; + } + } + + public async handle({ payload }: HandleOptions) { + const [streamlabs, twitchChat] = await Promise.all([ + this.notifyStreamlabs({ payload }), + this.notifyTwitch({ payload }), + ]); + + return { + twitchChat, + streamlabs, }; } } diff --git a/test/reactions/github/ping.spec.ts b/test/reactions/github/ping.spec.ts index 2db98d7..69402e7 100644 --- a/test/reactions/github/ping.spec.ts +++ b/test/reactions/github/ping.spec.ts @@ -1,14 +1,16 @@ import { Ping } from '../../../src/reactions/github/ping'; +import { StreamLabs } from '../../../src/services/StreamLabs'; +import { TwitchChat } from '../../../src/services/TwitchChat'; describe('Ping', () => { describe('#handle', () => { - let streamlabs: any; - let twitchChat: any; + let streamlabs: StreamLabs; + let twitchChat: TwitchChat; let payload: any; beforeEach(() => { - streamlabs = { alert: jest.fn() }; - twitchChat = { send: jest.fn() }; + streamlabs = ({ alert: jest.fn() } as unknown) as StreamLabs; + twitchChat = ({ send: jest.fn() } as unknown) as TwitchChat; payload = { hook: { events: ['fork'], @@ -38,7 +40,7 @@ describe('Ping', () => { await subject.handle({ payload }); expect(twitchChat.send).toHaveBeenCalledWith( - `🎉 Your repo ${payload.repository.full_name} is configured correctly for fork events 🎉`, + `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *fork* events 🎉`, ); }); @@ -55,7 +57,7 @@ describe('Ping', () => { }); expect(response).toEqual({ - message: `🎉 Your repo ${payload.repository.full_name} is configured correctly for pull_request events 🎉`, + message: `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *pull_request* events 🎉`, notified: true, }); }); @@ -77,5 +79,31 @@ describe('Ping', () => { notified: true, }); }); + + it("returns 'streamlabs.notified' set to false is something goes wrong with StreamLabs", async () => { + jest.spyOn(streamlabs, 'alert').mockImplementationOnce(async () => { + throw new Error('boom'); + }); + const subject = new Ping({ streamlabs, twitchChat }); + + const { + streamlabs: { notified }, + } = await subject.handle({ payload }); + + expect(notified).toEqual(false); + }); + + it("returns 'twitchChat.notified' set to false is something goes wrong with TwitchChat", async () => { + jest.spyOn(twitchChat, 'send').mockImplementationOnce(async () => { + throw new Error('boom'); + }); + const subject = new Ping({ streamlabs, twitchChat }); + + const { + twitchChat: { notified }, + } = await subject.handle({ payload }); + + expect(notified).toEqual(false); + }); }); });