[Composition] Test composition.add

Add test case to verify the behavior of the add method
of the composition capability.
This commit is contained in:
Victor Woeltjen
2015-09-02 11:56:37 -07:00
parent b2649de649
commit 17e9e87a2b
2 changed files with 105 additions and 13 deletions

View File

@ -65,7 +65,10 @@ define(
CompositionCapability.prototype.add = function (domainObject, index) {
var self = this,
id = typeof domainObject === 'string' ?
domainObject : domainObject.getId();
domainObject : domainObject.getId(),
model = self.domainObject.getModel(),
composition = model.composition,
oldIndex = composition.indexOf(id);
// Find the object with the above id, used to contextualize
function findObject(objects) {
@ -82,16 +85,6 @@ define(
}
function addIdToModel(model) {
var composition = model.composition,
oldIndex = composition.indexOf(id);
// If no index has been specified already and the id is already
// present, nothing to do. If the id is already at that index,
// also nothing to do, so cancel mutation.
if ((isNaN(index) && oldIndex !== -1) || (index === oldIndex)) {
return false;
}
// Pick a specific index if needed.
index = isNaN(index) ? composition.length : index;
// Also, don't put past the end of the array
@ -106,6 +99,13 @@ define(
model.composition.splice(index, 0, id);
}
// If no index has been specified already and the id is already
// present, nothing to do. If the id is already at that index,
// also nothing to do, so cancel mutation.
if ((isNaN(index) && oldIndex !== -1) || (index === oldIndex)) {
return contextualize(true);
}
return this.domainObject.useCapability('mutation', addIdToModel)
.then(contextualize);
};