Add test and fix receive method

This commit is contained in:
Brandon Keepers
2017-08-01 20:05:50 -05:00
parent 698f567713
commit fa6136cdcd
2 changed files with 24 additions and 9 deletions

View File

@@ -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);
}
};
};

View File

@@ -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();
});
});
});