[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

@@ -47,7 +47,7 @@ define(
// so support that, but don't introduce complication of
// native promises.
function mockPromise(value) {
return {
return (value || {}).then ? value : {
then: function (callback) {
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);
});
});
}
);
);