Merge remote-tracking branch 'github-open/open97' into open-master
This commit is contained in:
@@ -86,37 +86,18 @@ define(
|
||||
// composition, so that it will subsequently appear
|
||||
// as a child contained by that parent.
|
||||
function addToComposition(id, parent, parentPersistence) {
|
||||
var mutatationResult = parent.useCapability("mutation", function (model) {
|
||||
if (Array.isArray(model.composition)) {
|
||||
// Don't add if the id is already there
|
||||
if (model.composition.indexOf(id) === -1) {
|
||||
model.composition.push(id);
|
||||
}
|
||||
} else {
|
||||
// This is abnormal; composition should be an array
|
||||
self.$log.warn(NO_COMPOSITION_WARNING + parent.getId());
|
||||
return false; // Cancel mutation
|
||||
}
|
||||
});
|
||||
var compositionCapability = parent.getCapability('composition'),
|
||||
addResult = compositionCapability &&
|
||||
compositionCapability.add(id);
|
||||
|
||||
return self.$q.when(mutatationResult).then(function (result) {
|
||||
return self.$q.when(addResult).then(function (result) {
|
||||
if (!result) {
|
||||
self.$log.error("Could not mutate " + parent.getId());
|
||||
self.$log.error("Could not modify " + parent.getId());
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return parentPersistence.persist().then(function () {
|
||||
// Locate and return new Object in context of parent.
|
||||
return parent
|
||||
.useCapability('composition')
|
||||
.then(function (children) {
|
||||
var i;
|
||||
for (i = 0; i < children.length; i += 1) {
|
||||
if (children[i].getId() === id) {
|
||||
return children[i];
|
||||
}
|
||||
}
|
||||
});
|
||||
return result;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ define(
|
||||
);
|
||||
mockCompositionCapability = jasmine.createSpyObj(
|
||||
"composition",
|
||||
["invoke"]
|
||||
["invoke", "add"]
|
||||
);
|
||||
mockContextCapability = jasmine.createSpyObj(
|
||||
"context",
|
||||
@@ -120,6 +120,7 @@ define(
|
||||
mockCompositionCapability.invoke.andReturn(
|
||||
mockPromise([mockNewObject])
|
||||
);
|
||||
mockCompositionCapability.add.andReturn(mockPromise(true));
|
||||
|
||||
creationService = new CreationService(
|
||||
mockPersistenceService,
|
||||
@@ -143,33 +144,34 @@ define(
|
||||
parentModel = { composition: ["notAnyUUID"] };
|
||||
creationService.createObject(model, mockParentObject);
|
||||
|
||||
// Invoke the mutation callback
|
||||
expect(mockMutationCapability.invoke).toHaveBeenCalled();
|
||||
mockMutationCapability.invoke.mostRecentCall.args[0](parentModel);
|
||||
|
||||
// Should have a longer composition now, with the new UUID
|
||||
expect(parentModel.composition.length).toEqual(2);
|
||||
// Verify that a new ID was added
|
||||
expect(mockCompositionCapability.add)
|
||||
.toHaveBeenCalledWith(jasmine.any(String));
|
||||
});
|
||||
|
||||
it("warns if parent has no composition", function () {
|
||||
var model = { someKey: "some value" },
|
||||
parentModel = { };
|
||||
creationService.createObject(model, mockParentObject);
|
||||
it("provides the newly-created object", function () {
|
||||
var mockDomainObject = jasmine.createSpyObj(
|
||||
'newDomainObject',
|
||||
['getId', 'getModel', 'getCapability']
|
||||
),
|
||||
mockCallback = jasmine.createSpy('callback');
|
||||
|
||||
// Verify precondition; no prior warnings
|
||||
expect(mockLog.warn).not.toHaveBeenCalled();
|
||||
// Act as if the object had been created
|
||||
mockCompositionCapability.add.andCallFake(function (id) {
|
||||
mockDomainObject.getId.andReturn(id);
|
||||
mockCompositionCapability.invoke
|
||||
.andReturn(mockPromise([mockDomainObject]));
|
||||
return mockPromise(mockDomainObject);
|
||||
});
|
||||
|
||||
// Invoke the mutation callback
|
||||
expect(mockMutationCapability.invoke).toHaveBeenCalled();
|
||||
mockMutationCapability.invoke.mostRecentCall.args[0](parentModel);
|
||||
// Should find it in the composition
|
||||
creationService.createObject({}, mockParentObject)
|
||||
.then(mockCallback);
|
||||
|
||||
expect(mockCallback).toHaveBeenCalledWith(mockDomainObject);
|
||||
|
||||
// Should have a longer composition now, with the new UUID
|
||||
expect(mockLog.warn).toHaveBeenCalled();
|
||||
// Composition should still be undefined
|
||||
expect(parentModel.composition).toBeUndefined();
|
||||
});
|
||||
|
||||
|
||||
it("warns if parent has no persistence capability", function () {
|
||||
// Callbacks
|
||||
var success = jasmine.createSpy("success"),
|
||||
@@ -185,7 +187,6 @@ define(
|
||||
expect(mockLog.warn).toHaveBeenCalled();
|
||||
expect(success).not.toHaveBeenCalled();
|
||||
expect(failure).toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
it("logs an error when mutaton fails", function () {
|
||||
@@ -194,7 +195,7 @@ define(
|
||||
var model = { someKey: "some value" },
|
||||
parentModel = { composition: ["notAnyUUID"] };
|
||||
|
||||
mockMutationCapability.invoke.andReturn(mockPromise(false));
|
||||
mockCompositionCapability.add.andReturn(mockPromise(false));
|
||||
|
||||
creationService.createObject(model, mockParentObject);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user