[Core] Spec for domain objects

Spec for DomainObject and DomainObjectProvider; part of
ongoing test coverage for transition of platform/core
bundle, WTD-573.
This commit is contained in:
Victor Woeltjen 2014-11-21 17:51:45 -08:00
parent c34c16e51c
commit b9b164ab31
4 changed files with 117 additions and 4 deletions

View File

@ -8,7 +8,7 @@
"provides": "objectService",
"type": "provider",
"implementation": "objects/DomainObjectProvider.js",
"depends": [ "modelService", "capabilityService" ]
"depends": [ "modelService", "capabilityService", "$q" ]
},
{
"provides": "capabilityService",

View File

@ -16,9 +16,10 @@ define(
* @param {CapabilityService} capabilityService the service
* which provides capabilities (dynamic behavior)
* for domain objects.
* @param $q Angular's $q, for promise consolidation
* @constructor
*/
function DomainObjectProvider(modelService, capabilityService) {
function DomainObjectProvider(modelService, capabilityService, $q) {
// Given a models object (containing key-value id-model pairs)
// create a function that will look up from the capability
// service based on id; for handy mapping below.
@ -54,7 +55,7 @@ define(
// domain object provider.
function getObjects(ids) {
return modelService.getModels(ids).then(function (models) {
return Promise.all(
return $q.all(
ids.map(capabilityResolver(models))
).then(function (capabilities) {
return assembleResult(ids, models, capabilities);

View File

@ -9,6 +9,65 @@ define(
"use strict";
describe("The domain object provider", function () {
var mockModelService,
mockCapabilityService,
mockQ,
provider;
function mockPromise(value) {
return (value && value.then) ? value : {
then: function (callback) {
return mockPromise(callback(value));
},
// Provide a synchronous way to get a value out
// of this phony promise.
testValue: value
};
}
function mockAll(mockPromises) {
return mockPromise(mockPromises.map(function (p) {
return mockPromise(p).testValue;
}));
}
beforeEach(function () {
mockModelService = jasmine.createSpyObj(
"modelService",
[ "getModels" ]
);
mockCapabilityService = jasmine.createSpyObj(
"capabilityService",
[ "getCapabilities" ]
);
mockQ = {
when: mockPromise,
all: mockAll
};
provider = new DomainObjectProvider(
mockModelService,
mockCapabilityService,
mockQ
);
});
it("requests models from the model service", function () {
var ids = [ "a", "b", "c" ];
mockModelService.getModels.andReturn(mockPromise({}));
provider.getObjects(ids);
expect(mockModelService.getModels).toHaveBeenCalledWith(ids);
});
it("instantiates objects with provided models", function () {
var ids = [ "a", "b", "c"],
model = { someKey: "some value"},
result;
mockModelService.getModels.andReturn(mockPromise({ a: model }));
result = provider.getObjects(ids).testValue;
expect(result.a.getId()).toEqual("a");
expect(result.a.getModel()).toEqual(model);
});
});
}

View File

@ -8,7 +8,60 @@ define(
function (DomainObject) {
"use strict";
describe("", function () {
describe("A domain object", function () {
var testId = "test id",
testModel = { someKey: "some value"},
testCapabilities = {
"static": "some static capability",
"dynamic": function (domainObject) {
return "Dynamically generated for " +
domainObject.getId();
},
"invokable": {
invoke: function (arg) {
return "invoked with " + arg;
}
}
},
domainObject;
beforeEach(function () {
domainObject = new DomainObject(
testId,
testModel,
testCapabilities
);
});
it("reports its id", function () {
expect(domainObject.getId()).toEqual(testId);
});
it("reports its model", function () {
expect(domainObject.getModel()).toEqual(testModel);
});
it("reports static capabilities", function () {
expect(domainObject.getCapability("static"))
.toEqual("some static capability");
});
it("instantiates dynamic capabilities", function () {
expect(domainObject.getCapability("dynamic"))
.toEqual("Dynamically generated for test id");
});
it("allows for checking for the presence of capabilities", function () {
Object.keys(testCapabilities).forEach(function (capability) {
expect(domainObject.hasCapability(capability)).toBeTruthy();
});
expect(domainObject.hasCapability("somethingElse")).toBeFalsy();
});
it("allows for shorthand capability invocation", function () {
expect(domainObject.useCapability("invokable", "a specific value"))
.toEqual("invoked with a specific value");
});
});
}