[Composition] Update specs

Update specs to reflect usage of the add method in
the composition capability.
This commit is contained in:
Victor Woeltjen 2015-09-02 09:37:26 -07:00
parent d3d94d67ea
commit 8759fdbd95
5 changed files with 75 additions and 68 deletions

View File

@ -87,12 +87,12 @@ define(
// as a child contained by that parent. // as a child contained by that parent.
function addToComposition(id, parent, parentPersistence) { function addToComposition(id, parent, parentPersistence) {
var compositionCapability = parent.getCapability('composition'), var compositionCapability = parent.getCapability('composition'),
mutationResult = compositionCapability && addResult = compositionCapability &&
compositionCapability.add(id); compositionCapability.add(id);
return self.$q.when(mutatationResult).then(function (result) { return self.$q.when(addResult).then(function (result) {
if (!result) { if (!result) {
self.$log.error("Could not mutate " + parent.getId()); self.$log.error("Could not modify " + parent.getId());
return undefined; return undefined;
} }

View File

@ -86,7 +86,7 @@ define(
); );
mockCompositionCapability = jasmine.createSpyObj( mockCompositionCapability = jasmine.createSpyObj(
"composition", "composition",
["invoke"] ["invoke", "add"]
); );
mockContextCapability = jasmine.createSpyObj( mockContextCapability = jasmine.createSpyObj(
"context", "context",
@ -120,6 +120,7 @@ define(
mockCompositionCapability.invoke.andReturn( mockCompositionCapability.invoke.andReturn(
mockPromise([mockNewObject]) mockPromise([mockNewObject])
); );
mockCompositionCapability.add.andReturn(mockPromise(true));
creationService = new CreationService( creationService = new CreationService(
mockPersistenceService, mockPersistenceService,
@ -143,32 +144,33 @@ define(
parentModel = { composition: ["notAnyUUID"] }; parentModel = { composition: ["notAnyUUID"] };
creationService.createObject(model, mockParentObject); creationService.createObject(model, mockParentObject);
// Invoke the mutation callback // Verify that a new ID was added
expect(mockMutationCapability.invoke).toHaveBeenCalled(); expect(mockCompositionCapability.add)
mockMutationCapability.invoke.mostRecentCall.args[0](parentModel); .toHaveBeenCalledWith(jasmine.any(String));
// Should have a longer composition now, with the new UUID
expect(parentModel.composition.length).toEqual(2);
}); });
it("warns if parent has no composition", function () { it("provides the newly-created object", function () {
var model = { someKey: "some value" }, var mockDomainObject = jasmine.createSpyObj(
parentModel = { }; 'newDomainObject',
creationService.createObject(model, mockParentObject); ['getId', 'getModel', 'getCapability']
),
mockCallback = jasmine.createSpy('callback');
// Verify precondition; no prior warnings // Act as if the object had been created
expect(mockLog.warn).not.toHaveBeenCalled(); mockCompositionCapability.add.andCallFake(function (id) {
mockDomainObject.getId.andReturn(id);
// Invoke the mutation callback mockCompositionCapability.invoke
expect(mockMutationCapability.invoke).toHaveBeenCalled(); .andReturn(mockPromise([mockDomainObject]));
mockMutationCapability.invoke.mostRecentCall.args[0](parentModel); return mockPromise(true);
// Should have a longer composition now, with the new UUID
expect(mockLog.warn).toHaveBeenCalled();
// Composition should still be undefined
expect(parentModel.composition).toBeUndefined();
}); });
// Should find it in the composition
creationService.createObject({}, mockParentObject)
.then(mockCallback);
expect(mockCallback).toHaveBeenCalledWith(mockDomainObject);
});
it("warns if parent has no persistence capability", function () { it("warns if parent has no persistence capability", function () {
// Callbacks // Callbacks
@ -185,7 +187,6 @@ define(
expect(mockLog.warn).toHaveBeenCalled(); expect(mockLog.warn).toHaveBeenCalled();
expect(success).not.toHaveBeenCalled(); expect(success).not.toHaveBeenCalled();
expect(failure).toHaveBeenCalled(); expect(failure).toHaveBeenCalled();
}); });
it("logs an error when mutaton fails", function () { it("logs an error when mutaton fails", function () {
@ -194,7 +195,7 @@ define(
var model = { someKey: "some value" }, var model = { someKey: "some value" },
parentModel = { composition: ["notAnyUUID"] }; parentModel = { composition: ["notAnyUUID"] };
mockMutationCapability.invoke.andReturn(mockPromise(false)); mockCompositionCapability.add.andReturn(mockPromise(false));
creationService.createObject(model, mockParentObject); creationService.createObject(model, mockParentObject);

View File

@ -31,7 +31,7 @@ define(
mockDomainObject, mockDomainObject,
mockParent, mockParent,
mockContext, mockContext,
mockMutation, mockComposition,
mockPersistence, mockPersistence,
mockType, mockType,
actionContext, actionContext,
@ -67,7 +67,7 @@ define(
} }
}; };
mockContext = jasmine.createSpyObj("context", [ "getParent" ]); mockContext = jasmine.createSpyObj("context", [ "getParent" ]);
mockMutation = jasmine.createSpyObj("mutation", [ "invoke" ]); mockComposition = jasmine.createSpyObj("composition", [ "invoke", "add" ]);
mockPersistence = jasmine.createSpyObj("persistence", [ "persist" ]); mockPersistence = jasmine.createSpyObj("persistence", [ "persist" ]);
mockType = jasmine.createSpyObj("type", [ "hasFeature" ]); mockType = jasmine.createSpyObj("type", [ "hasFeature" ]);
@ -75,11 +75,11 @@ define(
mockDomainObject.getCapability.andReturn(mockContext); mockDomainObject.getCapability.andReturn(mockContext);
mockContext.getParent.andReturn(mockParent); mockContext.getParent.andReturn(mockParent);
mockType.hasFeature.andReturn(true); mockType.hasFeature.andReturn(true);
mockMutation.invoke.andReturn(mockPromise(true)); mockComposition.invoke.andReturn(mockPromise(true));
mockComposition.add.andReturn(mockPromise(true));
capabilities = { capabilities = {
mutation: mockMutation, composition: mockComposition,
persistence: mockPersistence, persistence: mockPersistence,
type: mockType type: mockType
}; };
@ -96,30 +96,14 @@ define(
}); });
it("mutates the parent when performed", function () { it("adds to the parent's composition when performed", function () {
action.perform(); action.perform();
expect(mockMutation.invoke) expect(mockComposition.add)
.toHaveBeenCalledWith(jasmine.any(Function)); .toHaveBeenCalledWith(mockDomainObject);
}); });
it("changes composition from its mutation function", function () { it("persists changes afterward", function () {
var mutator, result;
action.perform(); action.perform();
mutator = mockMutation.invoke.mostRecentCall.args[0];
result = mutator(model);
// Should not have cancelled the mutation
expect(result).not.toBe(false);
// Simulate mutate's behavior (remove can either return a
// new model or modify this one in-place)
result = result || model;
// Should have removed "test" - that was our
// mock domain object's id.
expect(result.composition).toEqual(["a", "b", "c", "test"]);
// Finally, should have persisted
expect(mockPersistence.persist).toHaveBeenCalled(); expect(mockPersistence.persist).toHaveBeenCalled();
}); });

View File

@ -45,7 +45,7 @@ define(
if (parentCandidate.getId() === object.getId()) { if (parentCandidate.getId() === object.getId()) {
return false; return false;
} }
if (parentCandidate.getModel().composition.indexOf(object.getId()) !== -1) { if ((parentCandidate.getModel().composition || []).indexOf(object.getId()) !== -1) {
return false; return false;
} }
return this.policyService.allow( return this.policyService.allow(

View File

@ -55,11 +55,18 @@ define(
name: 'object' name: 'object'
}); });
parentCandidate = domainObjectFactory({ parentCandidate = domainObjectFactory({
name: 'parentCandidate' name: 'parentCandidate',
capabilities: {
composition: jasmine.createSpyObj(
'composition',
['invoke', 'add']
)
}
}); });
validate = function () { validate = function () {
return linkService.validate(object, parentCandidate); return linkService.validate(object, parentCandidate);
}; };
mockPolicyService.allow.andReturn(true);
}); });
it("does not allow invalid parentCandidate", function () { it("does not allow invalid parentCandidate", function () {
@ -81,6 +88,23 @@ define(
expect(validate()).toBe(false); expect(validate()).toBe(false);
}); });
it("does not allow parents that contains object", function () {
object.id = 'abc';
parentCandidate.id = 'xyz';
parentCandidate.model.composition = ['abc'];
expect(validate()).toBe(false);
});
it("does not allow parents without composition", function () {
parentCandidate = domainObjectFactory({
name: 'parentCandidate'
});
object.id = 'abc';
parentCandidate.id = 'xyz';
parentCandidate.model.composition = undefined;
expect(validate()).toBe(false);
});
describe("defers to policyService", function () { describe("defers to policyService", function () {
beforeEach(function () { beforeEach(function () {
object.id = 'abc'; object.id = 'abc';
@ -121,16 +145,16 @@ define(
linkedObject, linkedObject,
parentModel, parentModel,
parentObject, parentObject,
mutationPromise,
compositionPromise, compositionPromise,
persistencePromise, persistencePromise,
addPromise,
compositionCapability, compositionCapability,
persistenceCapability; persistenceCapability;
beforeEach(function () { beforeEach(function () {
mutationPromise = new ControlledPromise();
compositionPromise = new ControlledPromise(); compositionPromise = new ControlledPromise();
persistencePromise = new ControlledPromise(); persistencePromise = new ControlledPromise();
addPromise = new ControlledPromise();
persistenceCapability = jasmine.createSpyObj( persistenceCapability = jasmine.createSpyObj(
'persistenceCapability', 'persistenceCapability',
['persist'] ['persist']
@ -138,9 +162,10 @@ define(
persistenceCapability.persist.andReturn(persistencePromise); persistenceCapability.persist.andReturn(persistencePromise);
compositionCapability = jasmine.createSpyObj( compositionCapability = jasmine.createSpyObj(
'compositionCapability', 'compositionCapability',
['invoke'] ['invoke', 'add']
); );
compositionCapability.invoke.andReturn(compositionPromise); compositionCapability.invoke.andReturn(compositionPromise);
compositionCapability.add.andReturn(addPromise);
parentModel = { parentModel = {
composition: [] composition: []
}; };
@ -151,7 +176,7 @@ define(
mutation: { mutation: {
invoke: function (mutator) { invoke: function (mutator) {
mutator(parentModel); mutator(parentModel);
return mutationPromise; return new ControlledPromise();
} }
}, },
persistence: persistenceCapability, persistence: persistenceCapability,
@ -172,20 +197,17 @@ define(
}); });
it("modifies parent model composition", function () { it("adds to the parent's composition", function () {
expect(parentModel.composition.length).toBe(0); expect(compositionCapability.add).not.toHaveBeenCalled();
linkService.perform(object, parentObject); linkService.perform(object, parentObject);
expect(parentObject.useCapability).toHaveBeenCalledWith( expect(compositionCapability.add)
'mutation', .toHaveBeenCalledWith(object);
jasmine.any(Function)
);
expect(parentModel.composition).toContain('xyz');
}); });
it("persists parent", function () { it("persists parent", function () {
linkService.perform(object, parentObject); linkService.perform(object, parentObject);
expect(mutationPromise.then).toHaveBeenCalled(); expect(addPromise.then).toHaveBeenCalled();
mutationPromise.resolve(); addPromise.resolve();
expect(parentObject.getCapability) expect(parentObject.getCapability)
.toHaveBeenCalledWith('persistence'); .toHaveBeenCalledWith('persistence');
expect(persistenceCapability.persist).toHaveBeenCalled(); expect(persistenceCapability.persist).toHaveBeenCalled();
@ -197,7 +219,7 @@ define(
whenComplete = jasmine.createSpy('whenComplete'); whenComplete = jasmine.createSpy('whenComplete');
returnPromise.then(whenComplete); returnPromise.then(whenComplete);
mutationPromise.resolve(); addPromise.resolve();
persistencePromise.resolve(); persistencePromise.resolve();
compositionPromise.resolve([linkedObject]); compositionPromise.resolve([linkedObject]);
expect(whenComplete).toHaveBeenCalledWith(linkedObject); expect(whenComplete).toHaveBeenCalledWith(linkedObject);