diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..bd80dc3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: node_js + +node_js: + - "node" + - "7" + - "6" + - "5" + - "4" diff --git a/README.md b/README.md index babd846..d9532c2 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/package.json b/package.json index d4ed99c..e542689 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/test/RedisPubSubAdapterTest.js b/test/RedisPubSubAdapterTest.js new file mode 100644 index 0000000..c6ee31e --- /dev/null +++ b/test/RedisPubSubAdapterTest.js @@ -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"}'); + }); + }); +});