Skip to content
Permalink
Browse files

fixing tests and node parameter bug

  • Loading branch information...
2roy999 committed Aug 23, 2019
1 parent d0e9652 commit 62e56ccc45c78d96b9ffece39d6ff45526c8e867
Showing with 76 additions and 10 deletions.
  1. +8 −9 index.js
  2. +7 −0 mclient.js
  3. +61 −1 test/index.test.js
@@ -1,15 +1,14 @@
const { generateCorrelationId } = require('@first-lego-league/ms-correlation/lib/correlation-id')
const { getCorrelationId } = require('@first-lego-league/ms-correlation')

const MClient = require('./mclient')

class Messenger {
constructor (options = {}) {
this._Promise = options.promise || global.Promise
this.options = Object.assign({}, Messenger.DEFAULT_OPTIONS, options)

this._logger = this.options.logger
const inServer = (typeof window === 'undefined')
// eslint-disable-next-line import/no-dynamic-require
const ClientClass = require(inServer ? 'mhub/dist/src/nodeclient' : 'mhub/dist/src/browserclient').MClient
this._client = new ClientClass(this.options.mhubURI)
this._client = new MClient(this.options.mhubURI)
this._topics = []
this._topicsToIgnore = []

@@ -41,9 +40,9 @@ class Messenger {

send (topic, data) {
return this.connect()
.then(() => this._client.publish(this.node, topic, data, {
.then(() => this._client.publish(this.options.node, topic, data, {
'client-id': this._clientId,
'correlation-id': generateCorrelationId()
'correlation-id': getCorrelationId()
}))
}

@@ -61,7 +60,7 @@ class Messenger {

if (this._topics.length) {
this._connectionPromise = this._connectionPromise
.then(() => this._Promise.all(this._topics.map(topic => this._client.subscribe(this.node, topic))))
.then(() => this._Promise.all(this._topics.map(topic => this._client.subscribe(this.options.node, topic))))
}

this._connectionPromise = this._connectionPromise
@@ -90,7 +89,7 @@ const DO_NOTHING = () => { }
Messenger.DEFAULT_OPTIONS = {
mhubURI: process.env.MHUB_URI,
node: 'default',
clientId: generateCorrelationId(),
clientId: 'unknown',
reconnectTimeout: 10 * 1000, // 10 seconds
logger: {
debug: DO_NOTHING,
@@ -0,0 +1,7 @@
/* eslint-disable node/exports-style */

if (typeof window === 'undefined') {
module.exports = require('mhub/dist/src/nodeclient').MClient
} else {
module.exports = require('mhub/dist/src/browserclient').MClient
}
@@ -8,7 +8,7 @@ const { MSCorrelationMock } = require('./mocks/ms-correlation.mock')
let mclient

const { Messenger } = proxyquire('../', {
mhub: { MClient: function () { return mclient } },
'./mclient': function () { return mclient },
'@first-lego-league/ms-correlation': MSCorrelationMock
})

@@ -323,4 +323,64 @@ describe('ms-messenger', () => {

expect(sendPromise).to.be.instanceOf(global.Promise)
})

describe('with non-default node', () => {
let messenger
let messengerSandbox

const node = 'non-default-node'

beforeEach(() => {
messenger = new Messenger({ node })
})

describe('connect', () => {
beforeEach(() => {
messenger._setTimeoutToReconnect = () => Promise.resolve()
messengerSandbox = chai.spy.sandbox()
messengerSandbox.on(messenger, ['_setTimeoutToReconnect'])
})

it('subscribe to all topics with right node', () => {
messenger._topics = ['topic']
messenger._logger.debug = chai.spy.returns(Promise.resolve())
return messenger.connect()
.then(() => {
expect(mclient.subscribe).to.have.been.called.with(node)
})
})
})

describe('other methods', () => {
let callback

beforeEach(() => {
messenger.connect = () => Promise.resolve()
messengerSandbox = chai.spy.sandbox()
messengerSandbox.on(messenger, ['connect'])
callback = chai.spy()
})

it('calls publish', () => {
return messenger.send('topic', 'data')
.then(() => {
expect(mclient.publish).to.have.been.called.with(node)
})
})

it('listen method calls client.subscribe with the default node and topic', () => {
return messenger.listen('topic', callback)
.then(() => {
expect(mclient.subscribe).to.have.been.called.with(node)
})
})

it('on method calls client.subscribe with the default node and topic', () => {
return messenger.on('topic', callback)
.then(() => {
expect(mclient.subscribe).to.have.been.called.with(node)
})
})
})
})
})

0 comments on commit 62e56cc

Please sign in to comment.
You can’t perform that action at this time.