From 7f0e75f37bd7e68967c796aecca3973b8cf98091 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Fri, 13 Nov 2015 17:59:30 +0100 Subject: [PATCH] Bug fixes in getServiceConfig function --- gulpfile.js | 3 +++ src/asteroid-collections.js | 12 ++++----- test/unit/asteroid-collections.js | 41 +++++++++++++++++-------------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 39d205c..2f94e57 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -8,6 +8,9 @@ var webpack = require("webpack"); var build = function (minify, callback) { webpack({ entry: "./src/asteroid-collections.js", + externals: { + "immutable": "umd immutable" + }, module: { loaders: [{ test: /\.js$/, diff --git a/src/asteroid-collections.js b/src/asteroid-collections.js index d36d89e..a1dd733 100644 --- a/src/asteroid-collections.js +++ b/src/asteroid-collections.js @@ -12,12 +12,12 @@ export function init () { } export function getServiceConfig (providerName) { - const serviceConfiguration = this.collections.getIn([ - SERVICE_CONFIG_COLLECTION, - providerName - ]); - if (!serviceConfiguration) { + try { + return this.collections + .get(SERVICE_CONFIG_COLLECTION) + .find(serviceConfig => serviceConfig.get("service") === providerName) + .toJS(); + } catch (ignore) { throw new Error(`No configuration found for provider ${providerName}`); } - return serviceConfiguration.toJS(); } diff --git a/test/unit/asteroid-collections.js b/test/unit/asteroid-collections.js index 4847242..3803d5d 100644 --- a/test/unit/asteroid-collections.js +++ b/test/unit/asteroid-collections.js @@ -102,18 +102,33 @@ describe("`asteroid-collection` mixin", function () { asteroidCollections.__ResetDependency__("SERVICE_CONFIG_COLLECTION"); }); - it("should call the `getIn` function of collections with the correct parameter", function () { + it("should call the `get` function of collections with the correct parameter", function () { const instance = { collections: { - getIn: sinon.stub().returns(Immutable.Map()) + get: sinon.stub().returns(Immutable.Map({id: Immutable.Map({service: "providerName"})})) } }; asteroidCollections.getServiceConfig.call(instance, providerName); - expect(instance.collections.getIn).to.have.callCount(1); - expect(instance.collections.getIn).to.have.been.calledWith([ - SERVICE_CONFIG_COLLECTION, - providerName - ]); + expect(instance.collections.get).to.have.callCount(1); + expect(instance.collections.get).to.have.been.calledWith(SERVICE_CONFIG_COLLECTION); + }); + + it("should return an object", function () { + const instance = { + collections: { + get: sinon.stub().returns(Immutable.Map({ + id: Immutable.Map( + {service: "providerName", clientID: "clientID"} + )} + )) + } + }; + var ret = asteroidCollections.getServiceConfig.call(instance, providerName); + expect(ret).to.be.an.instanceOf(Object); + expect(ret).to.deep.equal({ + service: "providerName", + clientID: "clientID" + }); }); it("should throw an `Error` with correct message if serviceConfiguration is `undefined`", function () { @@ -123,21 +138,9 @@ describe("`asteroid-collection` mixin", function () { const troubleMaker = () => { asteroidCollections.getServiceConfig.call(instance, providerName); }; - // expect(ret).to.be.an.instanceOf(Error); expect(troubleMaker).to.throw("No configuration found for provider providerName"); }); - it("should return an object", function () { - const instance = { - collections: { - getIn: sinon.stub().returns(Immutable.Map()) - } - }; - var ret = asteroidCollections.getServiceConfig.call(instance, providerName); - expect(ret).to.be.an.instanceOf(Object); - expect(ret).to.deep.equal({}); - }); - }); });