Skip to content

Commit

Permalink
feat: add new 'Ping' reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiMA10 committed Apr 18, 2020
1 parent 93e31a8 commit 959e4f1
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/reactions/github/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { StreamLabs } from '../../services/StreamLabs';
import { TwitchChat } from '../../services/TwitchChat';

interface PingConfig {
streamlabs: StreamLabs;
twitchChat: TwitchChat;
}

interface HandleOptions {
// FIXME: add Payload type
payload: any;
}

export class Ping {
private streamlabs: StreamLabs;
private twitchChat: TwitchChat;

public constructor({ streamlabs, twitchChat }: PingConfig) {
this.streamlabs = streamlabs;
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);

return {
twitchChat: {
message: twitchMessage,
notified: true,
},
streamlabs: {
message: streamlabsMessage,
notified: true,
},
};
}
}
81 changes: 81 additions & 0 deletions test/reactions/github/ping.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Ping } from '../../../src/reactions/github/ping';

describe('Ping', () => {
describe('#handle', () => {
let streamlabs: any;
let twitchChat: any;
let payload: any;

beforeEach(() => {
streamlabs = { alert: jest.fn() };
twitchChat = { send: jest.fn() };
payload = {
hook: {
events: ['fork'],
},
repository: {
full_name: 'streamdevs/webhook',
},
sender: {
login: 'orestes',
},
};
});

it('calls StreamLabs with the expected message', async () => {
const subject = new Ping({ streamlabs, twitchChat });

await subject.handle({ payload });

expect(streamlabs.alert).toHaveBeenCalledWith({
message: `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *fork* events 🎉`,
});
});

it('calls TwitchChat with the expected message', async () => {
const subject = new Ping({ streamlabs, twitchChat });

await subject.handle({ payload });

expect(twitchChat.send).toHaveBeenCalledWith(
`🎉 Your repo ${payload.repository.full_name} is configured correctly for fork events 🎉`,
);
});

it('returns the message that was send to Twitch', async () => {
const subject = new Ping({ streamlabs, twitchChat });

const { twitchChat: response } = await subject.handle({
payload: {
...payload,
hook: {
events: ['pull_request'],
},
},
});

expect(response).toEqual({
message: `🎉 Your repo ${payload.repository.full_name} is configured correctly for pull_request events 🎉`,
notified: true,
});
});

it('returns the message that was send to StreamLabs', async () => {
const subject = new Ping({ streamlabs, twitchChat });

const { streamlabs: response } = await subject.handle({
payload: {
...payload,
hook: {
events: ['star'],
},
},
});

expect(response).toEqual({
message: `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *star* events 🎉`,
notified: true,
});
});
});
});

0 comments on commit 959e4f1

Please sign in to comment.