From fa6136cdcd99fbb65f6d74e4cbe022b4ba1378a8 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Tue, 1 Aug 2017 20:05:50 -0500 Subject: [PATCH] Add test and fix receive method --- index.js | 20 ++++++++++++-------- test/index.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 3067159..7195b10 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,10 @@ module.exports = (options = {}) => { const server = createServer(webhook); // Log all received webhooks - webhook.on('*', event => logger.trace(event, 'webhook received')); + webhook.on('*', event => { + logger.trace(event, 'webhook received'); + receive(event); + }); // Log all webhook errors webhook.on('error', logger.error.bind(logger)); @@ -51,9 +54,16 @@ module.exports = (options = {}) => { logger.addStream(sentryStream(Raven)); } + const robots = []; + + function receive(event) { + return Promise.all(robots.map(robot => robot.receive(event))); + } + return { server, webhook, + receive, start() { server.listen(options.port); @@ -66,17 +76,11 @@ module.exports = (options = {}) => { // Connect the router from the robot to the server server.use(robot.router); - // Forward webhooks to robot - webhook.on('*', event => robot.receive(event)); - // Initialize the plugin plugin(robot); + robots.push(robot); return robot; - }, - - receive(event) { - return robot.receive(event); } }; }; diff --git a/test/index.js b/test/index.js index eac2d43..09ea944 100644 --- a/test/index.js +++ b/test/index.js @@ -48,7 +48,6 @@ describe('Probot', () => { ['foo', 'bar'].forEach(name => { probot.load(robot => { const app = robot.route('/' + name); - console.log(app); app.use(function (req, res, next) { res.append('X-Test', name); @@ -68,4 +67,16 @@ describe('Probot', () => { .expect('X-Test', 'bar'); }); }); + + describe('receive', () => { + it('forwards events to each plugin', async () => { + const spy = expect.createSpy(); + const robot = probot.load(robot => robot.on('push', spy)); + robot.auth = expect.createSpy().andReturn(Promise.resolve({})); + + await probot.receive(event); + + expect(spy).toHaveBeenCalled(); + }); + }); });