Skip to content

Commit

Permalink
Update code following comment from pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégoire Weber committed Apr 3, 2017
1 parent b12af51 commit cde5b28
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Barracks.prototype.checkUpdate = function (packages, customClientData) {
message: 'Check Update request failed: ' + error.message
});
} else if (response.statusCode == 200) {
resolve(responseBuilder.buildResponse(JSON.parse(body), that));
resolve(responseBuilder.buildResponse(JSON.parse(body), that.downloadPackage.bind(that)));
} else {
reject({
type: ERROR_UNEXPECTED_SERVER_RESPONSE,
Expand Down
10 changes: 5 additions & 5 deletions src/responseBuilder.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
function getPackageWithDownload(pckg, barracks) {
function getPackageWithDownload(pckg, downloadFunction) {
pckg.download = function (filePath) {
return barracks.downloadPackage(pckg, filePath);
return downloadFunction(pckg, filePath);
};
return pckg;
}

module.exports = {
buildResponse: function (body, barracks) {
buildResponse: function (body, downloadFunction) {
return {
available: body.available.map(function (pckg) {
return getPackageWithDownload(pckg, barracks);
return getPackageWithDownload(pckg, downloadFunction);
}),
changed: body.changed.map(function (pckg) {
return getPackageWithDownload(pckg, barracks);
return getPackageWithDownload(pckg, downloadFunction);
}),
unchanged: body.unchanged,
unavailable: body.unavailable
Expand Down
16 changes: 8 additions & 8 deletions tests/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ describe('checkUpdate(components, customClientData) ', function () {
return requestMock(options, callback);
},
'./responseBuilder': {
buildResponse: function (body, barracks) {
return buildResponseMock(body, barracks);
buildResponse: function (body, downloadFunction) {
return buildResponseMock(body, downloadFunction);
}
}
});
Expand Down Expand Up @@ -222,8 +222,8 @@ describe('checkUpdate(components, customClientData) ', function () {
callback(undefined, response, response.body);
};
var buildResponseSpy = sinon.spy();
buildResponseMock = function (body, barracks) {
buildResponseSpy(body, barracks);
buildResponseMock = function (body, downloadFunction) {
buildResponseSpy(body, downloadFunction);
return componentInfo;
};

Expand All @@ -238,7 +238,7 @@ describe('checkUpdate(components, customClientData) ', function () {
expect(buildResponseSpy).to.have.been.calledOnce;
expect(buildResponseSpy).to.have.been.calledWithExactly(
componentInfo,
sinon.match.object
sinon.match.func
);
done();
}).catch(function (err) {
Expand All @@ -265,8 +265,8 @@ describe('checkUpdate(components, customClientData) ', function () {
callback(undefined, response, response.body);
};
var buildResponseSpy = sinon.spy();
buildResponseMock = function (body, barracks) {
buildResponseSpy(body, barracks);
buildResponseMock = function (body, downloadFunction) {
buildResponseSpy(body, downloadFunction);
return componentInfo;
};

Expand All @@ -281,7 +281,7 @@ describe('checkUpdate(components, customClientData) ', function () {
expect(buildResponseSpy).to.have.been.calledOnce;
expect(buildResponseSpy).to.have.been.calledWithExactly(
componentInfo,
sinon.match.object
sinon.match.func
);
done();
}).catch(function (err) {
Expand Down
37 changes: 30 additions & 7 deletions tests/responseBuilder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
/* global describe, it */

var chai = require('chai');
var sinon = require('sinon');
var expect = chai.expect;
var builder = require('../src/responseBuilder');

describe('buildResponse : ', function () {

var barracks = {
downloadPackage: function () {}
};
var downloadPackage = function () {};

it('Should add a download function to the available packages', function () {
// Given
Expand All @@ -29,7 +28,7 @@ describe('buildResponse : ', function () {
};

// When
var result = builder.buildResponse(body, barracks);
var result = builder.buildResponse(body, downloadPackage);

// Then
expect(result.available[0]).to.have.property('reference').and.to.be.equals(package1.reference);
Expand Down Expand Up @@ -61,7 +60,7 @@ describe('buildResponse : ', function () {
};

// When
var result = builder.buildResponse(body, barracks);
var result = builder.buildResponse(body, downloadPackage);

// Then
expect(result.available).to.be.an('array').and.to.have.lengthOf(0);
Expand Down Expand Up @@ -93,7 +92,7 @@ describe('buildResponse : ', function () {
};

// When
var result = builder.buildResponse(body, barracks);
var result = builder.buildResponse(body, downloadPackage);

// Then
expect(result.available).to.be.an('array').and.to.have.lengthOf(0);
Expand Down Expand Up @@ -125,7 +124,7 @@ describe('buildResponse : ', function () {
};

// When
var result = builder.buildResponse(body, barracks);
var result = builder.buildResponse(body, downloadPackage);

// Then
expect(result.available).to.be.an('array').and.to.have.lengthOf(0);
Expand All @@ -138,4 +137,28 @@ describe('buildResponse : ', function () {
expect(result.unavailable[1]).to.have.property('version').and.to.be.equals(package2.version);
expect(result.unavailable[1]).to.not.have.property('download');
});

it('Should bind object with given function', function () {
// Given
var package = {
reference: 'plop',
version: 'plop1.1'
};
var body = {
available: [ package ],
changed: [],
unchanged: [],
unavailable: []
};
var downloadFunction = sinon.spy();
var response = builder.buildResponse(body, downloadFunction);
var filePath = 'path/to/file.sh';

// When
response.available[0].download(filePath);

// Then
expect(downloadFunction).to.have.been.calledOnce;
expect(downloadFunction).to.have.been.calledWithExactly(package, filePath);
});
});

0 comments on commit cde5b28

Please sign in to comment.