sync/test/pubsub/redis.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-08-16 01:23:03 +00:00
const assert = require('assert');
const { RedisMessageBus } = require('../../lib/pubsub/redis');
const { EventEmitter } = require('events');
const sinon = require('sinon');
describe('RedisMessageBus', () => {
let pubClient, subClient, messageBus, publishSpy, subscribeSpy;
beforeEach(() => {
pubClient = { publish: () => {} };
subClient = new EventEmitter();
subClient.subscribe = () => {};
subscribeSpy = sinon.spy(subClient, 'subscribe');
publishSpy = sinon.spy(pubClient, 'publish');
messageBus = new RedisMessageBus(pubClient, subClient, 'test');
subClient.emit('ready');
});
describe('#onMessage', () => {
it('processes a valid message', done => {
messageBus.once('testEvent', payload => {
assert(subscribeSpy.withArgs('test').calledOnce);
assert.deepStrictEqual(payload, { foo: 'bar' });
done();
});
messageBus.onMessage('test', '{"event":"testEvent","payload":{"foo":"bar"}}');
});
it('processes a syntactically invalid message', done => {
messageBus.onMessage('test', 'not valid json lol');
done();
});
});
describe('#emit', () => {
it('emits messages', () => {
messageBus.emit('testEvent', { foo: 'bar' });
assert(publishSpy.withArgs('test', sinon.match(arg => {
arg = JSON.parse(arg);
return arg.event === 'testEvent' && arg.payload.foo === 'bar';
})).calledOnce);
});
});
});