-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed8d49b
commit 1dcdcef
Showing
4 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "node" | ||
- "7" | ||
- "6" | ||
- "5" | ||
- "4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
let chai = require('chai'); | ||
let expect = chai.expect; | ||
let sinon = require('sinon'); | ||
let redis = require('redis'); | ||
let RedisPubSubAdapter = require('../src/RedisPubSubAdapter'); | ||
|
||
describe('RedisPubSubAdapter', () => { | ||
describe('construct instance', () => { | ||
it('should set the client property', () => { | ||
let client = sinon.createStubInstance(redis.RedisClient); | ||
let adapter = new RedisPubSubAdapter(client); | ||
expect(adapter.client).to.equal(client); | ||
}); | ||
}); | ||
|
||
describe('subscribe', () => { | ||
it('should subscribe to messages on the channel', () => { | ||
let client = sinon.createStubInstance(redis.RedisClient); | ||
|
||
client.on = sinon.stub(); | ||
client.subscribe = sinon.stub(); | ||
|
||
let adapter = new RedisPubSubAdapter(client); | ||
|
||
let handler = sinon.spy(); | ||
|
||
adapter.subscribe('my_channel', handler); | ||
|
||
sinon.assert.calledOnce(client.subscribe); | ||
sinon.assert.calledWith(client.subscribe, 'my_channel'); | ||
|
||
sinon.assert.calledOnce(client.on); | ||
sinon.assert.calledWith(client.on, 'message'); | ||
}); | ||
|
||
it('should pass the message to the handler when a message is received', () => { | ||
let client = sinon.createStubInstance(redis.RedisClient); | ||
|
||
client.on = sinon.stub(); | ||
client.subscribe = sinon.stub(); | ||
|
||
let adapter = new RedisPubSubAdapter(client); | ||
|
||
let handler = sinon.spy(); | ||
|
||
adapter.subscribe('my_channel', handler); | ||
|
||
client.on.yield('my_channel', 'Hello World!'); | ||
|
||
sinon.assert.calledOnce(handler); | ||
sinon.assert.calledWith(handler, 'Hello World!'); | ||
}); | ||
|
||
it('should pass an unserialized message to the handler when a message is received', () => { | ||
let client = sinon.createStubInstance(redis.RedisClient); | ||
|
||
client.on = sinon.stub(); | ||
client.subscribe = sinon.stub(); | ||
|
||
let adapter = new RedisPubSubAdapter(client); | ||
|
||
let handler = sinon.spy(); | ||
|
||
adapter.subscribe('my_channel', handler); | ||
|
||
client.on.yield('my_channel', 'Hello World!'); | ||
client.on.yield('my_channel', '{"hello":"world"}'); | ||
|
||
sinon.assert.calledTwice(handler); | ||
|
||
sinon.assert.calledWith(handler, 'Hello World!'); | ||
sinon.assert.calledWith(handler, {hello: 'world'}); | ||
}); | ||
}); | ||
|
||
describe('publish', () => { | ||
it('should publish the message to a channel', () => { | ||
let client = sinon.createStubInstance(redis.RedisClient); | ||
|
||
client.publish = sinon.stub(); | ||
|
||
let adapter = new RedisPubSubAdapter(client); | ||
|
||
adapter.publish('my_channel', 'Hello World!'); | ||
adapter.publish('another_channel', '{"hello":"world"}'); | ||
|
||
sinon.assert.calledTwice(client.publish); | ||
sinon.assert.calledWith(client.publish, 'my_channel', 'Hello World!'); | ||
sinon.assert.calledWith(client.publish, 'another_channel', '{"hello":"world"}'); | ||
}); | ||
}); | ||
}); |