Skip to content

Commit

Permalink
tests for messaging SDK proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
VladRomero committed Jun 1, 2017
1 parent 2206618 commit 34ee281
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/utils/BarracksMessagingSDKProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class BarracksMessagingSDKProxy {
apiKey: apiKey
});
messengerSDK.listenMessages(apiKey, unitId, timeout).then(response => {
console.log('log du listen dans barracksmessagingsdkproxy');
resolve(response);
console.log('un autre log très sympa');
}).catch(err => {
logger.debug('Failed to listen to messages :', err);
reject(err);
Expand Down
92 changes: 92 additions & 0 deletions tests/utils/BarracksMessagingSDKProxy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const messagingSDKProxyPath = '../../src/utils/BarracksMessagingSDKProxy';
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chaiAsPromised = require('chai-as-promised');
const BarracksSDKProxy = require(messagingSDKProxyPath);
const proxyquire = require('proxyquire').noCallThru();

chai.use(chaiAsPromised);
chai.use(sinonChai);

function getProxifiedBarracks(constructorSpy, listenMessagesSpy) {
return proxyquire(messagingSDKProxyPath, {
'barracks-messenger-sdk-betatest': function Constructor(options) {
constructorSpy(options);
this.listenMessages = listenMessagesSpy;
}
});
}

describe('BarracksMessagingSDKProxy', () => {

const baseUrl = 'https://app.barracks.io';
const apiKey = 'myApiKey';
const unitId = 'unitId';
const timeout = 60000;

describe('#listenMessages()', () => {

const message = 'Bonjour !';

it('should reject an error if client fails', done => {
// Given
const error = 'error';
const constructorSpy = sinon.spy();
const listenMessagesSpy = sinon.stub().returns(Promise.reject(error));
const ProxifiedBarracks = getProxifiedBarracks(constructorSpy, listenMessagesSpy);
const barracks = new ProxifiedBarracks();
barracks.baseUrl = baseUrl;

// When / Then
barracks.listenMessages(apiKey, unitId, timeout).then(result => {
done('should have failed');
}).catch(err => {
expect(constructorSpy).to.have.been.calledOnce;
expect(constructorSpy).to.have.been.calledWithExactly({
apiKey,
baseUrl: baseUrl,
});
expect(listenMessagesSpy).to.have.been.calledWithExactly(
apiKey,
unitId,
60000
);
expect(listenMessagesSpy).to.have.been.calledOnce;
done();
});
});

it('should call client when apiKey and deviceId provided', done => {
// Given
const response = { a: 'response' };
const constructorSpy = sinon.spy();
const listenMessagesSpy = sinon.stub().returns(Promise.resolve(response));
const ProxifiedBarracks = getProxifiedBarracks(constructorSpy, listenMessagesSpy);

const barracks = new ProxifiedBarracks();
barracks.baseUrl = baseUrl;

// When / Then
barracks.listenMessages(apiKey, unitId, 60000).then(result => {
expect(result).to.be.equals(response);
expect(constructorSpy).to.have.been.calledOnce;
expect(constructorSpy).to.have.been.calledWithExactly({
apiKey,
baseUrl: baseUrl
});
expect(listenMessagesSpy).to.have.been.calledOnce;
expect(listenMessagesSpy).to.have.been.calledWithExactly(
apiKey,
unitId,
60000
);
done();
}).catch(err => {
done(err);
});
});
});

});
1 change: 0 additions & 1 deletion tests/utils/BarracksSDK2Proxy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe('BarracksSDK2Proxy', () => {
const versionId = 'version1';

describe('#resolveDevicePackages()', () => {

const packages = [
{
reference: "io.baracks.app1",
Expand Down

0 comments on commit 34ee281

Please sign in to comment.