[Core] Specs for model service components

Fill in specs for model service components, part of
bundle platform/core; WTD-573.
This commit is contained in:
Victor Woeltjen 2014-11-21 17:25:10 -08:00
parent 99f9203e71
commit c34c16e51c
5 changed files with 158 additions and 3 deletions

View File

@ -26,7 +26,7 @@
"provides": "modelService",
"type": "provider",
"implementation": "models/RootModelProvider.js",
"depends": [ "roots[]", "$log" ]
"depends": [ "roots[]", "$q", "$log" ]
},
{
"provides": "modelService",

View File

@ -20,10 +20,10 @@ define(
*
* @constructor
*/
function RootModelProvider(roots, $log) {
function RootModelProvider(roots, $q, $log) {
// Pull out identifiers to used as ROOT's
var ids = roots.map(function (root) { return root.id; }),
baseProvider = new StaticModelProvider(roots, $log);
baseProvider = new StaticModelProvider(roots, $q, $log);
function addRoot(models) {
models.ROOT = {

View File

@ -9,6 +9,47 @@ define(
"use strict";
describe("The model aggregator", function () {
var mockQ,
mockProviders,
modelList = [
{ "a": { someKey: "some value" } },
{ "b": { someOtherKey: "some other value" } }
],
aggregator;
beforeEach(function () {
mockQ = jasmine.createSpyObj("$q", [ "all" ]);
mockProviders = modelList.map(function (models, i) {
var mockProvider = jasmine.createSpyObj(
"mockProvider" + i,
[ "getModels" ]
);
mockProvider.getModels.andReturn(models);
return mockProvider;
});
mockQ.all.andReturn({
then: function (c) { return c(modelList); }
});
aggregator = new ModelAggregator(mockQ, mockProviders);
});
it("aggregates results promised by multiple providers", function () {
expect(aggregator.getModels(["a", "b"])).toEqual({
"a": { someKey: "some value" },
"b": { someOtherKey: "some other value" }
});
});
it("passes ids to all aggregated providers", function () {
aggregator.getModels(["a", "b"]);
mockProviders.forEach(function (mockProvider) {
expect(mockProvider.getModels)
.toHaveBeenCalledWith(["a", "b"]);
});
});
});
}

View File

@ -9,6 +9,57 @@ define(
"use strict";
describe("The persisted model provider", function () {
var mockQ,
mockPersistenceService,
SPACE = "some space",
provider;
function mockPromise(value) {
return {
then: function (callback) {
return mockPromise(callback(value));
},
testValue: value
};
}
function mockAll(mockPromises) {
return mockPromise(mockPromises.map(function (p) {
return p.testValue;
}));
}
beforeEach(function () {
mockQ = { when: mockPromise, all: mockAll };
mockPersistenceService = {
readObject: function (space, id) {
return mockPromise({
space: space,
id: id
});
}
};
provider = new PersistedModelProvider(
mockPersistenceService,
mockQ,
SPACE
);
});
it("reads object models from persistence", function () {
var models;
provider.getModels(["a", "x", "zz"]).then(function (m) {
models = m;
});
expect(models).toEqual({
a: { space: SPACE, id: "a" },
x: { space: SPACE, id: "x" },
zz: { space: SPACE, id: "zz" }
});
});
});
}

View File

@ -9,6 +9,69 @@ define(
"use strict";
describe("The root model provider", function () {
var roots = [
{
"id": "a",
"model": {
"name": "Thing A",
"someProperty": "Some Value A"
}
},
{
"id": "b",
"model": {
"name": "Thing B",
"someProperty": "Some Value B"
}
}
],
captured,
mockLog,
mockQ,
provider;
function mockPromise(value) {
return {
then: function (callback) {
return mockPromise(callback(value));
}
};
}
function capture(value) { captured = value; }
beforeEach(function () {
mockQ = { when: mockPromise };
mockLog = jasmine.createSpyObj("$log", ["error", "warn", "info", "debug"]);
provider = new RootModelProvider(roots, mockQ, mockLog);
});
it("provides models from extension declarations", function () {
// Verify that we got the promise as the return value
provider.getModels(["a", "b"]).then(capture);
// Verify that the promise has the desired models
expect(captured.a.name).toEqual("Thing A");
expect(captured.a.someProperty).toEqual("Some Value A");
expect(captured.b.name).toEqual("Thing B");
expect(captured.b.someProperty).toEqual("Some Value B");
});
it("does not provide models which are not in extension declarations", function () {
provider.getModels(["c"]).then(capture);
// Verify that the promise has the desired models
expect(captured.c).toBeUndefined();
});
it("provides a ROOT object with roots in its composition", function () {
provider.getModels(["ROOT"]).then(capture);
expect(captured.ROOT).toBeDefined();
expect(captured.ROOT.composition).toEqual(["a", "b"]);
});
});
}