Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new 'Ping' reaction #104

Merged
merged 1 commit into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
});
});
});