mirror of
https://github.com/nasa/openmct.git
synced 2025-06-06 01:11:41 +00:00
[Composition] Test composition.add
Add test case to verify the behavior of the add method of the composition capability.
This commit is contained in:
parent
b2649de649
commit
17e9e87a2b
@ -65,7 +65,10 @@ define(
|
|||||||
CompositionCapability.prototype.add = function (domainObject, index) {
|
CompositionCapability.prototype.add = function (domainObject, index) {
|
||||||
var self = this,
|
var self = this,
|
||||||
id = typeof domainObject === 'string' ?
|
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
|
// Find the object with the above id, used to contextualize
|
||||||
function findObject(objects) {
|
function findObject(objects) {
|
||||||
@ -82,16 +85,6 @@ define(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addIdToModel(model) {
|
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.
|
// Pick a specific index if needed.
|
||||||
index = isNaN(index) ? composition.length : index;
|
index = isNaN(index) ? composition.length : index;
|
||||||
// Also, don't put past the end of the array
|
// Also, don't put past the end of the array
|
||||||
@ -106,6 +99,13 @@ define(
|
|||||||
model.composition.splice(index, 0, id);
|
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)
|
return this.domainObject.useCapability('mutation', addIdToModel)
|
||||||
.then(contextualize);
|
.then(contextualize);
|
||||||
};
|
};
|
||||||
|
@ -47,7 +47,7 @@ define(
|
|||||||
// so support that, but don't introduce complication of
|
// so support that, but don't introduce complication of
|
||||||
// native promises.
|
// native promises.
|
||||||
function mockPromise(value) {
|
function mockPromise(value) {
|
||||||
return {
|
return (value || {}).then ? value : {
|
||||||
then: function (callback) {
|
then: function (callback) {
|
||||||
return mockPromise(callback(value));
|
return mockPromise(callback(value));
|
||||||
}
|
}
|
||||||
@ -111,6 +111,98 @@ define(
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("allows domain objects to be added", function () {
|
||||||
|
var result,
|
||||||
|
testModel = { composition: [] },
|
||||||
|
mockChild = jasmine.createSpyObj("child", DOMAIN_OBJECT_METHODS);
|
||||||
|
|
||||||
|
mockDomainObject.getModel.andReturn(testModel);
|
||||||
|
mockObjectService.getObjects.andReturn(mockPromise({a: mockChild}));
|
||||||
|
mockChild.getCapability.andReturn(undefined);
|
||||||
|
mockChild.getId.andReturn('a');
|
||||||
|
|
||||||
|
mockDomainObject.useCapability.andCallFake(function (key, mutator) {
|
||||||
|
if (key === 'mutation') {
|
||||||
|
mutator(testModel);
|
||||||
|
return mockPromise(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
composition.add(mockChild).then(function (domainObject) {
|
||||||
|
result = domainObject;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(testModel.composition).toEqual(['a']);
|
||||||
|
|
||||||
|
// Should have returned the added object in its new context
|
||||||
|
expect(result.getId()).toEqual('a');
|
||||||
|
expect(result.getCapability('context')).toBeDefined();
|
||||||
|
expect(result.getCapability('context').getParent())
|
||||||
|
.toEqual(mockDomainObject);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not re-add IDs which are already present", function () {
|
||||||
|
var result,
|
||||||
|
testModel = { composition: [ 'a' ] },
|
||||||
|
mockChild = jasmine.createSpyObj("child", DOMAIN_OBJECT_METHODS);
|
||||||
|
|
||||||
|
mockDomainObject.getModel.andReturn(testModel);
|
||||||
|
mockObjectService.getObjects.andReturn(mockPromise({a: mockChild}));
|
||||||
|
mockChild.getCapability.andReturn(undefined);
|
||||||
|
mockChild.getId.andReturn('a');
|
||||||
|
|
||||||
|
mockDomainObject.useCapability.andCallFake(function (key, mutator) {
|
||||||
|
if (key === 'mutation') {
|
||||||
|
mutator(testModel);
|
||||||
|
return mockPromise(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
composition.add(mockChild).then(function (domainObject) {
|
||||||
|
result = domainObject;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Still just 'a'
|
||||||
|
expect(testModel.composition).toEqual(['a']);
|
||||||
|
|
||||||
|
// Should have returned the added object in its new context
|
||||||
|
expect(result.getId()).toEqual('a');
|
||||||
|
expect(result.getCapability('context')).toBeDefined();
|
||||||
|
expect(result.getCapability('context').getParent())
|
||||||
|
.toEqual(mockDomainObject);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("can add objects at a specified index", function () {
|
||||||
|
var result,
|
||||||
|
testModel = { composition: [ 'a', 'b', 'c' ] },
|
||||||
|
mockChild = jasmine.createSpyObj("child", DOMAIN_OBJECT_METHODS);
|
||||||
|
|
||||||
|
mockDomainObject.getModel.andReturn(testModel);
|
||||||
|
mockObjectService.getObjects.andReturn(mockPromise({a: mockChild}));
|
||||||
|
mockChild.getCapability.andReturn(undefined);
|
||||||
|
mockChild.getId.andReturn('a');
|
||||||
|
|
||||||
|
mockDomainObject.useCapability.andCallFake(function (key, mutator) {
|
||||||
|
if (key === 'mutation') {
|
||||||
|
mutator(testModel);
|
||||||
|
return mockPromise(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
composition.add(mockChild, 1).then(function (domainObject) {
|
||||||
|
result = domainObject;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Still just 'a'
|
||||||
|
expect(testModel.composition).toEqual(['b', 'a', 'c']);
|
||||||
|
|
||||||
|
// Should have returned the added object in its new context
|
||||||
|
expect(result.getId()).toEqual('a');
|
||||||
|
expect(result.getCapability('context')).toBeDefined();
|
||||||
|
expect(result.getCapability('context').getParent())
|
||||||
|
.toEqual(mockDomainObject);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
Loading…
x
Reference in New Issue
Block a user