diff --git a/platform/commonUI/edit/src/representers/EditToolbarSelection.js b/platform/commonUI/edit/src/representers/EditToolbarSelection.js index 8bec96d486..bc6e2fa861 100644 --- a/platform/commonUI/edit/src/representers/EditToolbarSelection.js +++ b/platform/commonUI/edit/src/representers/EditToolbarSelection.js @@ -6,16 +6,17 @@ define( "use strict"; /** - * Tracks selection state for Layout and Fixed Position views. - * This manages and mutates the provided selection array in-place, - * and takes care to only modify the array elements it manages - * (the view's proxy, and the single selection); selections may be - * added or removed elsewhere provided that similar care is taken - * elsewhere. + * Tracks selection state for editable views. Selection is + * implemented such that (from the toolbar's perspective) + * up to two objects can be "selected" at any given time: * - * @param {Array} selection the selection array from the view's scope - * @param [proxy] an object which represents the selection of the view - * itself (which handles view-level toolbar behavior) + * * The view proxy (see the `proxy` method), which provides + * an interface for interacting with the view itself (e.g. + * for buttons like "Add") + * * The selection, for single selected elements within the + * view. + * + * @constructor */ function EditToolbarSelection() { var selection = [ {} ], diff --git a/platform/commonUI/edit/test/representers/EditToolbarRepresenterSpec.js b/platform/commonUI/edit/test/representers/EditToolbarRepresenterSpec.js index b3c5c64a1f..0d788c834f 100644 --- a/platform/commonUI/edit/test/representers/EditToolbarRepresenterSpec.js +++ b/platform/commonUI/edit/test/representers/EditToolbarRepresenterSpec.js @@ -77,9 +77,9 @@ define( }); // Update the selection - mockScope.selection.push(testObject); + mockScope.selection.select(testObject); expect(mockScope.$watchCollection.mostRecentCall.args[0]) - .toEqual('selection'); // Make sure we're using right watch + .toEqual('selection.all()'); // Make sure we're using right watch mockScope.$watchCollection.mostRecentCall.args[1]([testObject]); // Update the state @@ -105,9 +105,9 @@ define( }); // Update the selection - mockScope.selection.push(testObject); + mockScope.selection.select(testObject); expect(mockScope.$watchCollection.mostRecentCall.args[0]) - .toEqual('selection'); // Make sure we're using right watch + .toEqual('selection.all()'); // Make sure we're using right watch mockScope.$watchCollection.mostRecentCall.args[1]([testObject]); // Invoke the first watch (assumed to be for toolbar state) diff --git a/platform/commonUI/edit/test/representers/EditToolbarSelectionSpec.js b/platform/commonUI/edit/test/representers/EditToolbarSelectionSpec.js new file mode 100644 index 0000000000..7b5c2b775a --- /dev/null +++ b/platform/commonUI/edit/test/representers/EditToolbarSelectionSpec.js @@ -0,0 +1,77 @@ +/*global define,describe,it,expect,beforeEach,jasmine,xit*/ + +define( + ['../../src/representers/EditToolbarSelection'], + function (EditToolbarSelection) { + "use strict"; + + describe("The Edit mode selection manager", function () { + var testProxy, + testElement, + otherElement, + selection; + + beforeEach(function () { + testProxy = { someKey: "some value" }; + testElement = { someOtherKey: "some other value" }; + otherElement = { yetAnotherKey: 42 }; + selection = new EditToolbarSelection(); + selection.proxy(testProxy); + }); + + it("adds the proxy to the selection array", function () { + expect(selection.all()).toEqual([testProxy]); + }); + + it("exposes view proxy", function () { + expect(selection.proxy()).toBe(testProxy); + }); + + it("includes selected objects alongside the proxy", function () { + selection.select(testElement); + expect(selection.all()).toEqual([testProxy, testElement]); + }); + + it("allows elements to be deselected", function () { + selection.select(testElement); + selection.deselect(); + expect(selection.all()).toEqual([testProxy]); + }); + + it("replaces old selections with new ones", function () { + selection.select(testElement); + selection.select(otherElement); + expect(selection.all()).toEqual([testProxy, otherElement]); + }); + + it("allows retrieval of the current selection", function () { + selection.select(testElement); + expect(selection.get()).toBe(testElement); + selection.select(otherElement); + expect(selection.get()).toBe(otherElement); + }); + + it("can check if an element is selected", function () { + selection.select(testElement); + expect(selection.selected(testElement)).toBeTruthy(); + expect(selection.selected(otherElement)).toBeFalsy(); + selection.select(otherElement); + expect(selection.selected(testElement)).toBeFalsy(); + expect(selection.selected(otherElement)).toBeTruthy(); + }); + + it("considers the proxy to be selected", function () { + expect(selection.selected(testProxy)).toBeTruthy(); + selection.select(testElement); + // Even when something else is selected... + expect(selection.selected(testProxy)).toBeTruthy(); + }); + + it("treats selection of the proxy as a no-op", function () { + selection.select(testProxy); + expect(selection.all()).toEqual([testProxy]); + }); + + }); + } +); diff --git a/platform/commonUI/edit/test/suite.json b/platform/commonUI/edit/test/suite.json index 5744ff8ea8..7c4d892f81 100644 --- a/platform/commonUI/edit/test/suite.json +++ b/platform/commonUI/edit/test/suite.json @@ -17,5 +17,6 @@ "objects/EditableDomainObjectCache", "objects/EditableModelCache", "representers/EditToolbar", - "representers/EditToolbarRepresenter" + "representers/EditToolbarRepresenter", + "representers/EditToolbarSelection" ] \ No newline at end of file