[Edit] Update EditToolbar spec

Update EditToolbar spec to reflect changes for WTD-881.
This commit is contained in:
Victor Woeltjen 2015-02-23 19:47:56 -08:00
parent 5ee0f9372b
commit 307fbe2151
2 changed files with 58 additions and 81 deletions

View File

@ -16,7 +16,6 @@ define(
* the current selection. * the current selection.
* *
* @param structure toolbar structure, as provided by view definition * @param structure toolbar structure, as provided by view definition
* @param {Array} selection the current selection state
* @param {Function} commit callback to invoke after changes * @param {Function} commit callback to invoke after changes
* @constructor * @constructor
*/ */
@ -109,7 +108,7 @@ define(
function isApplicable(item) { function isApplicable(item) {
var property = (item || {}).property, var property = (item || {}).property,
method = (item || {}).method, method = (item || {}).method,
exclusive = (item || {}).exclusive; exclusive = !!(item || {}).exclusive;
// Check if a selected item defines this property // Check if a selected item defines this property
function hasProperty(selected) { function hasProperty(selected) {
@ -151,11 +150,6 @@ define(
return converted; return converted;
} }
// Used to filter out sections that have become empty
function nonEmpty(section) {
return section && section.items && section.items.length > 0;
}
// Prepare a toolbar section // Prepare a toolbar section
function convertSection(section) { function convertSection(section) {
var converted = Object.create(section || {}); var converted = Object.create(section || {});
@ -188,9 +182,7 @@ define(
} }
toolbarStructure.sections = toolbarStructure.sections =
((structure || {}).sections || []) ((structure || {}).sections || []).map(convertSection);
.map(convertSection)
.filter(nonEmpty);
toolbarState = []; toolbarState = [];

View File

@ -6,34 +6,41 @@ define(
"use strict"; "use strict";
describe("An Edit mode toolbar", function () { describe("An Edit mode toolbar", function () {
var testStructure, var mockCommit,
testStructure,
testAB, testAB,
testABC, testABC,
testABC2, testABC2,
testABCXYZ, testABCXYZ,
testABCYZ, testABCYZ,
testM; testM,
toolbar;
function getVisibility(obj) {
return !obj.hidden;
}
beforeEach(function () { beforeEach(function () {
mockCommit = jasmine.createSpy('commit');
testStructure = { testStructure = {
sections: [ sections: [
{ {
items: [ items: [
{ name: "A", property: "a" }, { name: "A", property: "a", exclusive: true },
{ name: "B", property: "b" }, { name: "B", property: "b", exclusive: true },
{ name: "C", property: "c" } { name: "C", property: "c", exclusive: true }
] ]
}, },
{ {
items: [ items: [
{ name: "X", property: "x", inclusive: true }, { name: "X", property: "x" },
{ name: "Y", property: "y" }, { name: "Y", property: "y", exclusive: true },
{ name: "Z", property: "z" } { name: "Z", property: "z", exclusive: true }
] ]
}, },
{ {
items: [ items: [
{ name: "M", method: "m" } { name: "M", method: "m", exclusive: true }
] ]
} }
] ]
@ -44,6 +51,8 @@ define(
testABCXYZ = { a: 0, b: 1, c: 2, x: 'X!', y: 'Y!', z: 'Z!' }; testABCXYZ = { a: 0, b: 1, c: 2, x: 'X!', y: 'Y!', z: 'Z!' };
testABCYZ = { a: 0, b: 1, c: 2, y: 'Y!', z: 'Z!' }; testABCYZ = { a: 0, b: 1, c: 2, y: 'Y!', z: 'Z!' };
testM = { m: jasmine.createSpy("method") }; testM = { m: jasmine.createSpy("method") };
toolbar = new EditToolbar(testStructure, mockCommit);
}); });
it("provides properties from the original structure", function () { it("provides properties from the original structure", function () {
@ -67,27 +76,25 @@ define(
).not.toBeUndefined(); ).not.toBeUndefined();
}); });
it("prunes empty sections", function () { it("marks empty sections as hidden", function () {
// Verify that all sections are included when applicable... // Verify that all sections are included when applicable...
expect( toolbar.setSelection([ testABCXYZ ]);
new EditToolbar(testStructure, [ testABCXYZ ]) expect(toolbar.getStructure().sections.map(getVisibility))
.getStructure() .toEqual([ true, true, false ]);
.sections
.length
).toEqual(2);
// ...but omitted when only some are applicable // ...but omitted when only some are applicable
expect( toolbar.setSelection([ testABC ]);
new EditToolbar(testStructure, [ testABC ]) expect(toolbar.getStructure().sections.map(getVisibility))
.getStructure() .toEqual([ true, false, false ]);
.sections
.length
).toEqual(1);
}); });
it("reads properties from selections", function () { it("reads properties from selections", function () {
var toolbar = new EditToolbar(testStructure, [ testABC ]), var structure, state;
structure = toolbar.getStructure(),
state = toolbar.getState(); toolbar.setSelection([ testABC ]);
structure = toolbar.getStructure();
state = toolbar.getState();
expect(state[structure.sections[0].items[0].key]) expect(state[structure.sections[0].items[0].key])
.toEqual(testABC.a); .toEqual(testABC.a);
@ -98,11 +105,11 @@ define(
}); });
it("reads properties from getters", function () { it("reads properties from getters", function () {
var toolbar, structure, state; var structure, state;
testABC.a = function () { return "from a getter!"; }; testABC.a = function () { return "from a getter!"; };
toolbar = new EditToolbar(testStructure, [ testABC ]); toolbar.setSelection([ testABC ]);
structure = toolbar.getStructure(); structure = toolbar.getStructure();
state = toolbar.getState(); state = toolbar.getState();
@ -111,10 +118,9 @@ define(
}); });
it("sets properties on update", function () { it("sets properties on update", function () {
var toolbar = new EditToolbar(testStructure, [ testABC ]), toolbar.setSelection([ testABC ]);
structure = toolbar.getStructure();
toolbar.updateState( toolbar.updateState(
structure.sections[0].items[0].key, toolbar.getStructure().sections[0].items[0].key,
"new value" "new value"
); );
// Should have updated the underlying object // Should have updated the underlying object
@ -122,11 +128,11 @@ define(
}); });
it("invokes setters on update", function () { it("invokes setters on update", function () {
var toolbar, structure, state; var structure, state;
testABC.a = jasmine.createSpy('a'); testABC.a = jasmine.createSpy('a');
toolbar = new EditToolbar(testStructure, [ testABC ]); toolbar.setSelection([ testABC ]);
structure = toolbar.getStructure(); structure = toolbar.getStructure();
toolbar.updateState( toolbar.updateState(
@ -139,68 +145,47 @@ define(
it("removes inapplicable items", function () { it("removes inapplicable items", function () {
// First, verify with all items // First, verify with all items
expect( toolbar.setSelection([ testABC ]);
new EditToolbar(testStructure, [ testABC ]) expect(toolbar.getStructure().sections[0].items.map(getVisibility))
.getStructure() .toEqual([ true, true, true ]);
.sections[0]
.items
.length
).toEqual(3);
// Then, try with some items omitted // Then, try with some items omitted
expect( toolbar.setSelection([ testABC, testAB ]);
new EditToolbar(testStructure, [ testABC, testAB ]) expect(toolbar.getStructure().sections[0].items.map(getVisibility))
.getStructure() .toEqual([ true, true, false ]);
.sections[0]
.items
.length
).toEqual(2);
}); });
it("removes inconsistent states", function () { it("removes inconsistent states", function () {
// Only two of three values match among these selections // Only two of three values match among these selections
expect( toolbar.setSelection([ testABC, testABC2 ]);
new EditToolbar(testStructure, [ testABC, testABC2 ]) expect(toolbar.getStructure().sections[0].items.map(getVisibility))
.getStructure() .toEqual([ false, true, true ]);
.sections[0]
.items
.length
).toEqual(2);
}); });
it("allows inclusive items", function () { it("allows inclusive items", function () {
// One inclusive item is in the set, property 'x' of the // One inclusive item is in the set, property 'x' of the
// second section; make sure items are pruned down // second section; make sure items are pruned down
// when only some of the selection has x,y,z properties // when only some of the selection has x,y,z properties
expect( toolbar.setSelection([ testABC, testABCXYZ ]);
new EditToolbar(testStructure, [ testABC, testABCXYZ ]) expect(toolbar.getStructure().sections[1].items.map(getVisibility))
.getStructure() .toEqual([ true, false, false ]);
.sections[1]
.items
.length
).toEqual(1);
}); });
it("removes inclusive items when there are no matches", function () { it("removes inclusive items when there are no matches", function () {
expect( toolbar.setSelection([ testABCYZ ]);
new EditToolbar(testStructure, [ testABCYZ ]) expect(toolbar.getStructure().sections[1].items.map(getVisibility))
.getStructure() .toEqual([ false, true, true ]);
.sections[1]
.items
.length
).toEqual(2);
}); });
it("adds click functions when a method is specified", function () { it("adds click functions when a method is specified", function () {
var testCommit = jasmine.createSpy('commit'), toolbar.setSelection([testM]);
toolbar = new EditToolbar(testStructure, [ testM ], testCommit);
// Verify precondition // Verify precondition
expect(testM.m).not.toHaveBeenCalled(); expect(testM.m).not.toHaveBeenCalled();
// Click! // Click!
toolbar.getStructure().sections[0].items[0].click(); toolbar.getStructure().sections[2].items[0].click();
// Should have called the underlying function // Should have called the underlying function
expect(testM.m).toHaveBeenCalled(); expect(testM.m).toHaveBeenCalled();
// Should also have committed the change // Should also have committed the change
expect(testCommit).toHaveBeenCalled(); expect(mockCommit).toHaveBeenCalled();
}); });
}); });
} }