diff --git a/README.md b/README.md index e94b562..b39fe31 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,53 @@ describe('http', function() { Note that `yield` and *generators* are part of [**ECMA6**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*), so it may not work on older node.js versions. It will wait for the delay to complete the `beforeEach` before proceeding to the test `it`. +#### Testing messages sent to other rooms + +You can also test messages sent by your script to other rooms through Hubot's `robot.messageRoom(...)` method. + +Given the following script: +```javascript +module.exports = robot => + robot.respond(/announce otherRoom: (.+)$/i, msg => { + robot.messageRoom('otherRoom', "@#{msg.envelope.user.name} said: #{msg.msg.match[1]}"); + }) +``` + +you could test the messages sent to other rooms like this: +```javascript +const Helper = require('../src/index'); +const helper = new Helper('../scripts/message-room.js'); + +const expect = require('chai').expect; + +describe('message-room', function() { + beforeEach(function() { + this.room = helper.createRoom({name: 'room', httpd: false}); + }); + + context('user asks hubot to announce something', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('alice', '@hubot announce otherRoom: I love hubot!'); + }.bind(this)); + }); + + it('should not post to this channel', function() { + expect(this.room.messages).to.eql([ + ['alice', '@hubot announce otherRoom: I love hubot!'] + ]); + }); + + it('should post to the other channel', function() { + expect(this.room.robot.messagesTo['otherRoom']).to.eql([ + ['hubot', '@alice says: I love hubot!'] + ]); + }); + }); +}); +``` + + #### Testing events You can also test events emitted by your script. For example, Slack users diff --git a/src/index.js b/src/index.js index dd29a51..b7d6885 100644 --- a/src/index.js +++ b/src/index.js @@ -19,9 +19,22 @@ class MockRobot extends Hubot.Robot { if (httpd == null) { httpd = true; } super(null, null, httpd, 'hubot'); + this.messagesTo = {}; + this.Response = MockResponse; } + messageRoom(roomName, str) { + if (roomName == this.adapter.name) { + this.adapter.messages.push(['hubot', str]); + } else { + if (!(roomName in this.messagesTo)) { + this.messagesTo[roomName] = []; + } + this.messagesTo[roomName].push(['hubot', str]); + } + } + loadAdapter() { this.adapter = new Room(this); } diff --git a/test/message-room_test.js b/test/message-room_test.js new file mode 100644 index 0000000..b0f031a --- /dev/null +++ b/test/message-room_test.js @@ -0,0 +1,33 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts/message-room.js'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('message-room', function() { + beforeEach(function() { + this.room = helper.createRoom({name: 'room', httpd: false}); + }); + + context('user asks hubot to announce something', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('alice', '@hubot announce otherRoom: I love hubot!'); + }.bind(this)); + }); + + it('should not post to this channel', function() { + expect(this.room.messages).to.eql([ + ['alice', '@hubot announce otherRoom: I love hubot!'] + ]); + }); + + it('should post to the other channel', function() { + expect(this.room.robot.messagesTo['otherRoom']).to.eql([ + ['hubot', '@alice says: I love hubot!'] + ]); + }); + }); +}); diff --git a/test/scripts/message-room.js b/test/scripts/message-room.js new file mode 100644 index 0000000..aaf0436 --- /dev/null +++ b/test/scripts/message-room.js @@ -0,0 +1,6 @@ +// Description: +// Test script +module.exports = robot => + robot.respond(/announce otherRoom: (.+)$/i, msg => { + robot.messageRoom('otherRoom', '@' + msg.envelope.user.name + ' says: ' + msg.match[1]); + })