Skip to content

Commit

Permalink
feat: add ability to test robot.messageRoom(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidalpert committed Mar 11, 2018
1 parent 93a6772 commit 13a8a28
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
33 changes: 33 additions & 0 deletions test/message-room_test.js
Original file line number Diff line number Diff line change
@@ -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!']
]);
});
});
});
6 changes: 6 additions & 0 deletions test/scripts/message-room.js
Original file line number Diff line number Diff line change
@@ -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]);
})

0 comments on commit 13a8a28

Please sign in to comment.