From 8f14f4e5ebca63615d5ee2aeb840f354225fb575 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 17 Feb 2015 12:58:36 -0800 Subject: [PATCH] [Edit] Add spec for toolbar Add spec for class that will convert toolbar definition provided by views to suitable input for mct-toolbar, WTD-878. --- platform/commonUI/edit/README.md | 3 +- .../edit/src/representers/EditToolbar.js | 25 +++ .../representers/EditToolbarRepresenter.js | 8 + .../edit/test/representers/EditToolbarSpec.js | 189 ++++++++++++++++++ platform/commonUI/edit/test/suite.json | 3 +- 5 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 platform/commonUI/edit/src/representers/EditToolbar.js create mode 100644 platform/commonUI/edit/src/representers/EditToolbarRepresenter.js create mode 100644 platform/commonUI/edit/test/representers/EditToolbarSpec.js diff --git a/platform/commonUI/edit/README.md b/platform/commonUI/edit/README.md index ae0a86286d..b594f3be02 100644 --- a/platform/commonUI/edit/README.md +++ b/platform/commonUI/edit/README.md @@ -29,4 +29,5 @@ if toolbar properties are meant to be view-global (as opposed to per-selection) then the view must include some object to act as its proxy in the current selection (in addition to whatever objects the user will conceive of as part of the current selection), typically -with `inclusive` set to `true`. \ No newline at end of file +with `inclusive` set to `true`. + diff --git a/platform/commonUI/edit/src/representers/EditToolbar.js b/platform/commonUI/edit/src/representers/EditToolbar.js new file mode 100644 index 0000000000..91133e98a6 --- /dev/null +++ b/platform/commonUI/edit/src/representers/EditToolbar.js @@ -0,0 +1,25 @@ +/*global define*/ +define( + [], + function () { + "use strict"; + + function EditToolbar(structure, selection) { + + return { + getStructure: function () { + + }, + getState: function () { + + }, + updateState: function (key) { + + } + }; + } + + return EditToolbar; + } +); + diff --git a/platform/commonUI/edit/src/representers/EditToolbarRepresenter.js b/platform/commonUI/edit/src/representers/EditToolbarRepresenter.js new file mode 100644 index 0000000000..84a8ff88ab --- /dev/null +++ b/platform/commonUI/edit/src/representers/EditToolbarRepresenter.js @@ -0,0 +1,8 @@ +/*global define*/ + +define( + [], + function () { + "use strict"; + } +); \ No newline at end of file diff --git a/platform/commonUI/edit/test/representers/EditToolbarSpec.js b/platform/commonUI/edit/test/representers/EditToolbarSpec.js new file mode 100644 index 0000000000..3348ae04f3 --- /dev/null +++ b/platform/commonUI/edit/test/representers/EditToolbarSpec.js @@ -0,0 +1,189 @@ +/*global define,describe,it,expect,beforeEach,jasmine*/ + +define( + ['../../src/representers/EditToolbar'], + function (EditToolbar) { + "use strict"; + + describe("An Edit mode toolbar", function () { + var testStructure, + testAB, + testABC, + testABC2, + testABCXYZ, + testABCYZ; + + beforeEach(function () { + testStructure = { + sections: [ + { + items: [ + { name: "A", property: "a" }, + { name: "B", property: "b" }, + { name: "C", property: "c" } + ] + }, + { + items: [ + { name: "X", property: "x", inclusive: true }, + { name: "Y", property: "y" }, + { name: "Z", property: "z" } + ] + } + ] + }; + testAB = { a: 0, b: 1 }; + testABC = { a: 0, b: 1, c: 2 }; + testABC2 = { a: 4, b: 1, c: 2 }; // For inconsistent-state checking + testABCXYZ = { a: 'A!', b: 'B!', c: 'C!', x: 'X!', y: 'Y!', z: 'Z!' }; + testABCYZ = { a: 'A!', b: 'B!', c: 'C!', y: 'Y!', z: 'Z!' }; + }); + + it("provides properties from the original structure", function () { + expect( + new EditToolbar(testStructure, [ testABC ]) + .getStructure() + .sections[0] + .items[1] + .name + ).toEqual("B"); + }); + + // This is needed by mct-toolbar + it("adds keys to form structure", function () { + expect( + new EditToolbar(testStructure, [ testABC ]) + .getStructure() + .sections[0] + .items[1] + .key + ).not.toBeUndefined(); + }); + + it("prunes empty sections", function () { + // Verify that all sections are included when applicable... + expect( + new EditToolbar(testStructure, [ testABCXYZ ]) + .getStructure() + .sections + .length + ).toEqual(2); + // ...but omitted when only some are applicable + expect( + new EditToolbar(testStructure, [ testABC ]) + .getStructure() + .sections + .length + ).toEqual(1); + }); + + it("reads properties from selections", function () { + var toolbar = new EditToolbar(testStructure, [ testABC ]), + structure = toolbar.getStructure(), + state = toolbar.getState(); + + expect(state[structure.sections[0].items[0].key]) + .toEqual(testABC.a); + expect(state[structure.sections[0].items[1].key]) + .toEqual(testABC.b); + expect(state[structure.sections[0].items[2].key]) + .toEqual(testABC.c); + }); + + it("reads properties from getters", function () { + var toolbar, structure, state; + + testABC.a = function () { return "from a getter!"; }; + + toolbar = new EditToolbar(testStructure, [ testABC ]); + structure = toolbar.getStructure(); + state = toolbar.getState(); + + expect(state[structure.sections[0].items[0].key]) + .toEqual("from a getter!"); + }); + + it("sets properties on update", function () { + var toolbar = new EditToolbar(testStructure, [ testABC ]), + structure = toolbar.getStructure(); + toolbar.updateState( + structure.sections[0].items[0].key, + "new value" + ); + // Should have updated the underlying object + expect(testABC.a).toEqual("new value"); + }); + + it("invokes setters on update", function () { + var toolbar, structure, state; + + testABC.a = jasmine.createSpy('a'); + + toolbar = new EditToolbar(testStructure, [ testABC ]); + structure = toolbar.getStructure(); + + toolbar.updateState( + structure.sections[0].items[0].key, + "new value" + ); + // Should have updated the underlying object + expect(testABC.a).toHaveBeenCalledWith("new value"); + }); + + it("removes inapplicable items", function () { + // First, verify with all items + expect( + new EditToolbar(testStructure, [ testABC ]) + .getStructure() + .sections[0] + .items + .length + ).toEqual(3); + // Then, try with some items omitted + expect( + new EditToolbar(testStructure, [ testABC, testAB ]) + .getStructure() + .sections[0] + .items + .length + ).toEqual(2); + }); + + it("removes inconsistent states", function () { + // Only two of three values match among these selections + expect( + new EditToolbar(testStructure, [ testABC, testABC2 ]) + .getStructure() + .sections[0] + .items + .length + ).toEqual(2); + }); + + it("allows inclusive items", function () { + // One inclusive item is in the set, property 'x' of the + // second section; make sure items are pruned down + // when only some of the selection has x,y,z properties + expect( + new EditToolbar(testStructure, [ testABC, testABCXYZ ]) + .getStructure() + .sections[1] + .items + .length + ).toEqual(1); + }); + + it("removes inclusive items when there are no matches", function () { + expect( + new EditToolbar(testStructure, [ testABCYZ ]) + .getStructure() + .sections[1] + .items + .length + ).toEqual(2); + }); + }); + } +); + + diff --git a/platform/commonUI/edit/test/suite.json b/platform/commonUI/edit/test/suite.json index e50fb236d4..834055331a 100644 --- a/platform/commonUI/edit/test/suite.json +++ b/platform/commonUI/edit/test/suite.json @@ -15,5 +15,6 @@ "capabilities/EditorCapability", "objects/EditableDomainObject", "objects/EditableDomainObjectCache", - "objects/EditableModelCache" + "objects/EditableModelCache", + "representers/EditToolbar" ] \ No newline at end of file