Skip to content

Commit

Permalink
add unit tests (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgoslett authored May 8, 2017
1 parent ed8d49b commit 1dcdcef
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js

node_js:
- "node"
- "7"
- "6"
- "5"
- "4"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
A Redis adapter for the [js-pubsub](https://github.com/Superbalist/js-pubsub) package.

[![Author](http://img.shields.io/badge/author-@superbalist-blue.svg?style=flat-square)](https://twitter.com/superbalist)
[![Build Status](https://img.shields.io/travis/Superbalist/js-pubsub-redis/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/js-pubsub-redis)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![NPM Version](https://img.shields.io/npm/v/@superbalist/js-pubsub-redis.svg)](https://www.npmjs.com/package/@superbalist/js-pubsub-redis)
[![NPM Downloads](https://img.shields.io/npm/dt/@superbalist/js-pubsub-redis.svg)](https://www.npmjs.com/package/@superbalist/js-pubsub-redis)
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A Redis adapter for the js-pubsub package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha test",
"coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec -t 5000"
},
"repository": {
"type": "git",
Expand All @@ -25,7 +26,11 @@
"redis": "^2.7.1"
},
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^3.19.0",
"eslint-config-google": "^0.7.1"
"eslint-config-google": "^0.7.1",
"istanbul": "^0.4.5",
"mocha": "^3.3.0",
"sinon": "^2.2.0"
}
}
94 changes: 94 additions & 0 deletions test/RedisPubSubAdapterTest.js
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"}');
});
});
});

0 comments on commit 1dcdcef

Please sign in to comment.