[Core] Add specs for contextual objects

Add specs for the context capability, and the domain object
wrapper which introduces it. Ongoing transition work for
platform/core, WTD-573.
This commit is contained in:
Victor Woeltjen 2014-11-21 16:22:16 -08:00
parent e18b4f234f
commit 2b82262775
2 changed files with 94 additions and 0 deletions

View File

@ -8,7 +8,53 @@ define(
function (ContextCapability) {
"use strict";
var DOMAIN_OBJECT_METHODS = [
"getId",
"getModel",
"getCapability",
"hasCapability",
"useCapability"
];
describe("The context capability", function () {
var mockDomainObject,
mockParent,
mockGrandparent,
mockContext,
context;
beforeEach(function () {
mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS);
mockParent = jasmine.createSpyObj("parent", DOMAIN_OBJECT_METHODS);
mockGrandparent = jasmine.createSpyObj("grandparent", DOMAIN_OBJECT_METHODS);
mockContext = jasmine.createSpyObj("context", [ "getParent", "getRoot", "getPath" ]);
mockParent.getCapability.andReturn(mockContext);
mockContext.getParent.andReturn(mockGrandparent);
mockContext.getRoot.andReturn(mockGrandparent);
mockContext.getPath.andReturn([mockGrandparent, mockParent]);
context = new ContextCapability(mockParent, mockDomainObject);
});
it("allows an object's parent to be retrieved", function () {
expect(context.getParent()).toEqual(mockParent);
});
it("allows an object's full ancestry to be retrieved", function () {
expect(context.getPath()).toEqual([mockGrandparent, mockParent, mockDomainObject]);
});
it("allows the deepest ancestor of an object to be retrieved", function () {
expect(context.getRoot()).toEqual(mockGrandparent);
});
it("treats ancestors with no context capability as deepest ancestors", function () {
mockParent.getCapability.andReturn(undefined);
expect(context.getPath()).toEqual([mockParent, mockDomainObject]);
expect(context.getRoot()).toEqual(mockParent);
});
});
}

View File

@ -8,7 +8,55 @@ define(
function (ContextualDomainObject) {
"use strict";
var DOMAIN_OBJECT_METHODS = [
"getId",
"getModel",
"getCapability",
"hasCapability",
"useCapability"
];
describe("A contextual domain object", function () {
var mockParent,
mockDomainObject,
model,
contextualDomainObject;
beforeEach(function () {
mockParent = jasmine.createSpyObj("parent", DOMAIN_OBJECT_METHODS);
mockDomainObject = jasmine.createSpyObj("parent", DOMAIN_OBJECT_METHODS);
model = { someKey: "some value" };
mockDomainObject.getCapability.andReturn("some capability");
mockDomainObject.getModel.andReturn(model);
contextualDomainObject = new ContextualDomainObject(
mockDomainObject,
mockParent
);
});
it("adds a context capability to a domain object", function () {
var context = contextualDomainObject.getCapability('context');
// Expect something that looks like a context capability
expect(context).toBeDefined();
expect(context.getPath).toBeDefined();
expect(context.getRoot).toBeDefined();
expect(context.getParent()).toEqual(mockParent);
});
it("does not shadow other domain object methods", function () {
expect(contextualDomainObject.getModel())
.toEqual(model);
expect(contextualDomainObject.getCapability("other"))
.toEqual("some capability");
});
});
}