Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const chai = require('chai'); |
| 5 | const expect = chai.expect; |
| 6 | const sinon = require('sinon'); |
| 7 | const sinonChai = require('sinon-chai'); |
| 8 | const mockery = require('mockery'); |
| 9 | chai.use(sinonChai); |
| 10 | const fakeredis = require('fakeredis'); |
| 11 | |
| 12 | const client = fakeredis.createClient('test-client'); |
| 13 | const publisher = fakeredis.createClient('test-client'); |
| 14 | |
| 15 | const socketSpy = sinon.spy(); |
| 16 | const mockSocket = { |
| 17 | get: () => { |
| 18 | return { |
| 19 | emit: socketSpy |
| 20 | } |
| 21 | } |
| 22 | }; |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 23 | |
| 24 | const mockRequest = { |
| 25 | get: () => { |
| 26 | return { |
| 27 | end: (fn) => { |
| 28 | fn(null, {body: [ |
| 29 | {name: 'Slice'}, |
| 30 | {name: 'Site'} |
| 31 | ]}); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 36 | const channelName = 'Site'; |
| 37 | |
| 38 | describe('The event system', () => { |
| 39 | |
| 40 | before((done) => { |
| 41 | |
| 42 | // Enable mockery to mock objects |
| 43 | mockery.enable({ |
| 44 | warnOnReplace: false, |
| 45 | warnOnUnregistered: false |
| 46 | }); |
| 47 | |
| 48 | // Stub the createClient method to *always* return the client created above |
| 49 | sinon.stub(fakeredis, 'createClient', () => client); |
| 50 | |
| 51 | // Override the redis module with our fakeredis instance |
| 52 | mockery.registerMock('redis', fakeredis); |
| 53 | |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 54 | // Override the superagent module with our mockRequest instance |
| 55 | mockery.registerMock('superagent', mockRequest); |
| 56 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 57 | // mock the socketIo client to have a spy |
| 58 | mockery.registerMock('./websocket.js', mockSocket); |
| 59 | |
| 60 | require('../src/controllers/redis.js'); |
| 61 | setTimeout(() => { |
| 62 | done(); |
| 63 | }, 1000); |
| 64 | }); |
| 65 | |
| 66 | after(() => { |
| 67 | mockery.disable(); |
| 68 | fakeredis.createClient.restore(); |
| 69 | }); |
| 70 | |
| 71 | // run after each test |
| 72 | beforeEach(() => { |
| 73 | client.unsubscribe(channelName); |
| 74 | client.subscribe(channelName); |
| 75 | publisher.flushdb(); |
| 76 | }); |
| 77 | |
| 78 | it('should send a websocket event when it receive a redis event that is not JSON', (done) => { |
| 79 | publisher.publish(channelName, 'I am sending a message.'); |
| 80 | setTimeout(() => { |
| 81 | expect(socketSpy).to.have.been.called; |
| 82 | expect(socketSpy).to.have.been.calledWith('event', { |
| 83 | model: channelName, |
| 84 | msg: 'I am sending a message.' |
| 85 | }); |
| 86 | done(); |
| 87 | }, 500) |
| 88 | }); |
| 89 | |
| 90 | it('should send a websocket event when it receive a redis event that is JSON', (done) => { |
| 91 | publisher.publish(channelName, JSON.stringify({msg: 'Json Message'})); |
| 92 | setTimeout(() => { |
| 93 | expect(socketSpy).to.have.been.called; |
| 94 | expect(socketSpy).to.have.been.calledWith('event', { |
| 95 | model: channelName, |
| 96 | msg: {msg: 'Json Message'} |
| 97 | }); |
| 98 | done(); |
| 99 | }, 1000) |
| 100 | }); |
| 101 | }); |
| 102 | })(); |