From 3916968544a9f5ed89f6b3a3123d843a0944dabc Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 17 Feb 2015 15:47:18 -0800 Subject: [PATCH] [Edit] Add EditToolbarRepresenter spec Add tests for representer which mediates between view's selection and toolbar contents, WTD-878. --- .../representers/EditToolbarRepresenter.js | 4 +- .../EditToolbarRepresenterSpec.js | 91 +++++++++++++++++++ platform/commonUI/edit/test/suite.json | 3 +- 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 platform/commonUI/edit/test/representers/EditToolbarRepresenterSpec.js diff --git a/platform/commonUI/edit/src/representers/EditToolbarRepresenter.js b/platform/commonUI/edit/src/representers/EditToolbarRepresenter.js index 01cb96f41b..feeb5bcd53 100644 --- a/platform/commonUI/edit/src/representers/EditToolbarRepresenter.js +++ b/platform/commonUI/edit/src/representers/EditToolbarRepresenter.js @@ -37,7 +37,9 @@ define( // Update selection models to match changed toolbar state function updateState(state) { - state.forEach(toolbar.updateState); + state.forEach(function (value, index) { + toolbar.updateState(index, value); + }); } // Represent a domain object using this definition diff --git a/platform/commonUI/edit/test/representers/EditToolbarRepresenterSpec.js b/platform/commonUI/edit/test/representers/EditToolbarRepresenterSpec.js new file mode 100644 index 0000000000..134f5c0261 --- /dev/null +++ b/platform/commonUI/edit/test/representers/EditToolbarRepresenterSpec.js @@ -0,0 +1,91 @@ +/*global define,describe,it,expect,beforeEach,jasmine*/ + +define( + ["../../src/representers/EditToolbarRepresenter"], + function (EditToolbarRepresenter) { + "use strict"; + + describe("The Edit mode toolbar representer", function () { + var mockScope, + mockElement, + testAttrs, + mockUnwatch, + representer; + + beforeEach(function () { + mockScope = jasmine.createSpyObj( + '$scope', + [ '$on', '$watch', '$watchCollection' ] + ); + mockElement = {}; + testAttrs = { toolbar: 'testToolbar' }; + mockScope.$parent = jasmine.createSpyObj( + '$parent', + [ '$watch', '$watchCollection' ] + ); + mockUnwatch = jasmine.createSpy('unwatch'); + + mockScope.$parent.$watchCollection.andReturn(mockUnwatch); + + representer = new EditToolbarRepresenter( + mockScope, + mockElement, + testAttrs + ); + }); + + it("exposes toolbar state under a attr-defined name", function () { + // A strucutre/state object should have been added to the + // parent scope under the name provided in the "toolbar" + // attribute + expect(mockScope.$parent.testToolbar).toBeDefined(); + }); + + it("is robust against lack of a toolbar definition", function () { + expect(function () { + representer.represent({}); + }).not.toThrow(); + }); + + it("watches for toolbar state changes", function () { + expect(mockScope.$parent.$watchCollection).toHaveBeenCalledWith( + "testToolbar.state", + jasmine.any(Function) + ); + }); + + it("stops watching toolbar state when destroyed", function () { + expect(mockUnwatch).not.toHaveBeenCalled(); + representer.destroy(); + expect(mockUnwatch).toHaveBeenCalled(); + }); + + // Verify a simple interaction between selection state and toolbar + // state; more complicated interactions are tested in EditToolbar. + it("conveys state changes", function () { + var testObject = { k: 123 }; + + // Provide a view which has a toolbar + representer.represent({ + toolbar: { sections: [ { items: [ { property: 'k' } ] } ] } + }); + + // Update the selection + mockScope.selection.push(testObject); + expect(mockScope.$watchCollection.mostRecentCall.args[0]) + .toEqual('selection'); // Make sure we're using right watch + mockScope.$watchCollection.mostRecentCall.args[1]([testObject]); + + // Update the state + mockScope.$parent.testToolbar.state[0] = 456; + mockScope.$parent.$watchCollection.mostRecentCall.args[1]( + mockScope.$parent.testToolbar.state + ); + + // Should have updated the original object + expect(testObject.k).toEqual(456); + }); + + }); + } +); \ No newline at end of file diff --git a/platform/commonUI/edit/test/suite.json b/platform/commonUI/edit/test/suite.json index 834055331a..5744ff8ea8 100644 --- a/platform/commonUI/edit/test/suite.json +++ b/platform/commonUI/edit/test/suite.json @@ -16,5 +16,6 @@ "objects/EditableDomainObject", "objects/EditableDomainObjectCache", "objects/EditableModelCache", - "representers/EditToolbar" + "representers/EditToolbar", + "representers/EditToolbarRepresenter" ] \ No newline at end of file