2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
2015-02-17 20:58:36 +00:00
|
|
|
/*global define,describe,it,expect,beforeEach,jasmine*/
|
|
|
|
|
|
|
|
define(
|
|
|
|
['../../src/representers/EditToolbar'],
|
|
|
|
function (EditToolbar) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
describe("An Edit mode toolbar", function () {
|
2015-02-24 03:47:56 +00:00
|
|
|
var mockCommit,
|
|
|
|
testStructure,
|
2015-02-17 20:58:36 +00:00
|
|
|
testAB,
|
|
|
|
testABC,
|
|
|
|
testABC2,
|
|
|
|
testABCXYZ,
|
2015-02-19 20:44:00 +00:00
|
|
|
testABCYZ,
|
2015-02-24 03:47:56 +00:00
|
|
|
testM,
|
|
|
|
toolbar;
|
|
|
|
|
|
|
|
function getVisibility(obj) {
|
|
|
|
return !obj.hidden;
|
|
|
|
}
|
2015-02-17 20:58:36 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2015-02-24 03:47:56 +00:00
|
|
|
mockCommit = jasmine.createSpy('commit');
|
2015-02-17 20:58:36 +00:00
|
|
|
testStructure = {
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
items: [
|
2015-02-24 03:47:56 +00:00
|
|
|
{ name: "A", property: "a", exclusive: true },
|
|
|
|
{ name: "B", property: "b", exclusive: true },
|
|
|
|
{ name: "C", property: "c", exclusive: true }
|
2015-02-17 20:58:36 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
items: [
|
2015-02-24 03:47:56 +00:00
|
|
|
{ name: "X", property: "x" },
|
|
|
|
{ name: "Y", property: "y", exclusive: true },
|
|
|
|
{ name: "Z", property: "z", exclusive: true }
|
2015-02-17 20:58:36 +00:00
|
|
|
]
|
2015-02-19 20:44:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
items: [
|
2015-02-24 03:47:56 +00:00
|
|
|
{ name: "M", method: "m", exclusive: true }
|
2015-02-19 20:44:00 +00:00
|
|
|
]
|
2015-02-17 20:58:36 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
testAB = { a: 0, b: 1 };
|
|
|
|
testABC = { a: 0, b: 1, c: 2 };
|
|
|
|
testABC2 = { a: 4, b: 1, c: 2 }; // For inconsistent-state checking
|
2015-02-17 21:47:14 +00:00
|
|
|
testABCXYZ = { a: 0, b: 1, c: 2, x: 'X!', y: 'Y!', z: 'Z!' };
|
|
|
|
testABCYZ = { a: 0, b: 1, c: 2, y: 'Y!', z: 'Z!' };
|
2015-02-19 20:44:00 +00:00
|
|
|
testM = { m: jasmine.createSpy("method") };
|
2015-02-24 03:47:56 +00:00
|
|
|
|
|
|
|
toolbar = new EditToolbar(testStructure, mockCommit);
|
2015-02-17 20:58:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2015-02-24 03:47:56 +00:00
|
|
|
it("marks empty sections as hidden", function () {
|
2015-02-17 20:58:36 +00:00
|
|
|
// Verify that all sections are included when applicable...
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABCXYZ ]);
|
|
|
|
expect(toolbar.getStructure().sections.map(getVisibility))
|
|
|
|
.toEqual([ true, true, false ]);
|
|
|
|
|
2015-02-17 20:58:36 +00:00
|
|
|
// ...but omitted when only some are applicable
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABC ]);
|
|
|
|
expect(toolbar.getStructure().sections.map(getVisibility))
|
|
|
|
.toEqual([ true, false, false ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("reads properties from selections", function () {
|
2015-02-24 03:47:56 +00:00
|
|
|
var structure, state;
|
|
|
|
|
|
|
|
toolbar.setSelection([ testABC ]);
|
|
|
|
|
|
|
|
structure = toolbar.getStructure();
|
|
|
|
state = toolbar.getState();
|
2015-02-17 20:58:36 +00:00
|
|
|
|
|
|
|
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 () {
|
2015-02-24 03:47:56 +00:00
|
|
|
var structure, state;
|
2015-02-17 20:58:36 +00:00
|
|
|
|
|
|
|
testABC.a = function () { return "from a getter!"; };
|
|
|
|
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABC ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
structure = toolbar.getStructure();
|
|
|
|
state = toolbar.getState();
|
|
|
|
|
|
|
|
expect(state[structure.sections[0].items[0].key])
|
|
|
|
.toEqual("from a getter!");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets properties on update", function () {
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABC ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
toolbar.updateState(
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.getStructure().sections[0].items[0].key,
|
2015-02-17 20:58:36 +00:00
|
|
|
"new value"
|
|
|
|
);
|
|
|
|
// Should have updated the underlying object
|
|
|
|
expect(testABC.a).toEqual("new value");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("invokes setters on update", function () {
|
2015-02-24 03:47:56 +00:00
|
|
|
var structure, state;
|
2015-02-17 20:58:36 +00:00
|
|
|
|
|
|
|
testABC.a = jasmine.createSpy('a');
|
|
|
|
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABC ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2015-02-24 17:47:00 +00:00
|
|
|
it("provides a return value describing update status", function () {
|
|
|
|
// Should return true if actually updated, otherwise false
|
|
|
|
var key;
|
|
|
|
toolbar.setSelection([ testABC ]);
|
|
|
|
key = toolbar.getStructure().sections[0].items[0].key;
|
|
|
|
expect(toolbar.updateState(key, testABC.a)).toBeFalsy();
|
|
|
|
expect(toolbar.updateState(key, "new value")).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2015-02-17 20:58:36 +00:00
|
|
|
it("removes inapplicable items", function () {
|
|
|
|
// First, verify with all items
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABC ]);
|
|
|
|
expect(toolbar.getStructure().sections[0].items.map(getVisibility))
|
|
|
|
.toEqual([ true, true, true ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
// Then, try with some items omitted
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABC, testAB ]);
|
|
|
|
expect(toolbar.getStructure().sections[0].items.map(getVisibility))
|
|
|
|
.toEqual([ true, true, false ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("removes inconsistent states", function () {
|
|
|
|
// Only two of three values match among these selections
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABC, testABC2 ]);
|
|
|
|
expect(toolbar.getStructure().sections[0].items.map(getVisibility))
|
|
|
|
.toEqual([ false, true, true ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABC, testABCXYZ ]);
|
|
|
|
expect(toolbar.getStructure().sections[1].items.map(getVisibility))
|
|
|
|
.toEqual([ true, false, false ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("removes inclusive items when there are no matches", function () {
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([ testABCYZ ]);
|
|
|
|
expect(toolbar.getStructure().sections[1].items.map(getVisibility))
|
|
|
|
.toEqual([ false, true, true ]);
|
2015-02-17 20:58:36 +00:00
|
|
|
});
|
2015-02-19 20:44:00 +00:00
|
|
|
|
|
|
|
it("adds click functions when a method is specified", function () {
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.setSelection([testM]);
|
2015-02-19 20:44:00 +00:00
|
|
|
// Verify precondition
|
|
|
|
expect(testM.m).not.toHaveBeenCalled();
|
|
|
|
// Click!
|
2015-02-24 03:47:56 +00:00
|
|
|
toolbar.getStructure().sections[2].items[0].click();
|
2015-02-19 20:44:00 +00:00
|
|
|
// Should have called the underlying function
|
|
|
|
expect(testM.m).toHaveBeenCalled();
|
|
|
|
// Should also have committed the change
|
2015-02-24 03:47:56 +00:00
|
|
|
expect(mockCommit).toHaveBeenCalled();
|
2015-02-19 20:44:00 +00:00
|
|
|
});
|
2015-02-17 20:58:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|