From 17e9e87a2b592e10feaf6cc98ff25cfae1cdd4d8 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 2 Sep 2015 11:56:37 -0700 Subject: [PATCH] [Composition] Test composition.add Add test case to verify the behavior of the add method of the composition capability. --- .../src/capabilities/CompositionCapability.js | 22 ++--- .../capabilities/CompositionCapabilitySpec.js | 96 ++++++++++++++++++- 2 files changed, 105 insertions(+), 13 deletions(-) diff --git a/platform/core/src/capabilities/CompositionCapability.js b/platform/core/src/capabilities/CompositionCapability.js index 8fcca62174..925e27ba79 100644 --- a/platform/core/src/capabilities/CompositionCapability.js +++ b/platform/core/src/capabilities/CompositionCapability.js @@ -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); }; diff --git a/platform/core/test/capabilities/CompositionCapabilitySpec.js b/platform/core/test/capabilities/CompositionCapabilitySpec.js index 518cb2e795..08b135b172 100644 --- a/platform/core/test/capabilities/CompositionCapabilitySpec.js +++ b/platform/core/test/capabilities/CompositionCapabilitySpec.js @@ -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); + }); + }); } -); \ No newline at end of file +);