2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
2020-09-14 18:17:31 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2020, United States Government
|
2015-05-13 23:42:35 +00:00
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
2015-05-13 23:42:35 +00:00
|
|
|
* "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.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT includes source code licensed under additional open source
|
2015-05-13 23:42:35 +00:00
|
|
|
* 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-24 02:52:53 +00:00
|
|
|
|
|
|
|
define(
|
|
|
|
["../../src/controllers/DialogButtonController"],
|
|
|
|
function (DialogButtonController) {
|
|
|
|
|
|
|
|
describe("A dialog button controller", function () {
|
|
|
|
var mockScope,
|
|
|
|
mockDialogService,
|
|
|
|
mockPromise,
|
|
|
|
testStructure,
|
|
|
|
controller;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockScope = jasmine.createSpyObj(
|
|
|
|
'$scope',
|
2016-05-19 18:29:13 +00:00
|
|
|
['$watch']
|
2015-02-24 02:52:53 +00:00
|
|
|
);
|
|
|
|
mockDialogService = jasmine.createSpyObj(
|
|
|
|
'dialogService',
|
2016-05-19 18:29:13 +00:00
|
|
|
['getUserInput']
|
2015-02-24 02:52:53 +00:00
|
|
|
);
|
|
|
|
mockPromise = jasmine.createSpyObj(
|
|
|
|
'promise',
|
2016-05-19 18:29:13 +00:00
|
|
|
['then']
|
2015-02-24 02:52:53 +00:00
|
|
|
);
|
|
|
|
testStructure = {
|
|
|
|
name: "A Test",
|
2017-02-15 23:02:39 +00:00
|
|
|
cssClass: "icon-T",
|
2015-02-24 02:52:53 +00:00
|
|
|
description: "Test description",
|
|
|
|
control: "dialog-button",
|
|
|
|
title: "Test title",
|
|
|
|
dialog: {
|
|
|
|
"control": "textfield",
|
|
|
|
"name": "Inner control"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-24 03:06:09 +00:00
|
|
|
mockScope.field = "testKey";
|
2015-02-24 02:52:53 +00:00
|
|
|
mockScope.ngModel = { testKey: "initial test value" };
|
|
|
|
mockScope.structure = testStructure;
|
|
|
|
|
2018-06-30 00:32:59 +00:00
|
|
|
mockDialogService.getUserInput.and.returnValue(mockPromise);
|
2015-02-24 02:52:53 +00:00
|
|
|
|
|
|
|
controller = new DialogButtonController(
|
|
|
|
mockScope,
|
|
|
|
mockDialogService
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("provides a structure for a button control", function () {
|
|
|
|
var buttonStructure;
|
|
|
|
|
|
|
|
// Template is just a mct-control pointing to a button
|
|
|
|
// control, so this controller needs to set up all the
|
|
|
|
// logic for showing a dialog and collecting user input
|
|
|
|
// when that button gets clicked.
|
|
|
|
expect(mockScope.$watch).toHaveBeenCalledWith(
|
|
|
|
"structure", // As passed in via mct-control
|
|
|
|
jasmine.any(Function)
|
|
|
|
);
|
|
|
|
|
2018-06-30 00:32:59 +00:00
|
|
|
mockScope.$watch.calls.mostRecent().args[1](testStructure);
|
2015-02-24 02:52:53 +00:00
|
|
|
|
|
|
|
buttonStructure = controller.getButtonStructure();
|
2017-02-15 23:02:39 +00:00
|
|
|
expect(buttonStructure.cssClass).toEqual(testStructure.cssClass);
|
2015-02-24 02:52:53 +00:00
|
|
|
expect(buttonStructure.description).toEqual(testStructure.description);
|
|
|
|
expect(buttonStructure.name).toEqual(testStructure.name);
|
|
|
|
expect(buttonStructure.click).toEqual(jasmine.any(Function));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows a dialog when clicked", function () {
|
2018-06-30 00:32:59 +00:00
|
|
|
mockScope.$watch.calls.mostRecent().args[1](testStructure);
|
2015-02-24 02:52:53 +00:00
|
|
|
// Verify precondition - no dialog shown
|
|
|
|
expect(mockDialogService.getUserInput).not.toHaveBeenCalled();
|
|
|
|
// Click!
|
|
|
|
controller.getButtonStructure().click();
|
|
|
|
// Should have shown a dialog
|
|
|
|
expect(mockDialogService.getUserInput).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("stores user input to the model", function () {
|
|
|
|
var key, input = {};
|
|
|
|
// Show dialog, click...
|
2018-06-30 00:32:59 +00:00
|
|
|
mockScope.$watch.calls.mostRecent().args[1](testStructure);
|
2015-02-24 02:52:53 +00:00
|
|
|
controller.getButtonStructure().click();
|
|
|
|
// Should be listening to 'then'
|
|
|
|
expect(mockPromise.then)
|
|
|
|
.toHaveBeenCalledWith(jasmine.any(Function));
|
|
|
|
// Find the key that the dialog should return
|
2018-06-30 00:32:59 +00:00
|
|
|
key = mockDialogService.getUserInput.calls.mostRecent()
|
2015-02-24 02:52:53 +00:00
|
|
|
.args[0].sections[0].rows[0].key;
|
|
|
|
// Provide 'user input'
|
|
|
|
input[key] = "test user input";
|
|
|
|
// Resolve the promise with it
|
2018-06-30 00:32:59 +00:00
|
|
|
mockPromise.then.calls.mostRecent().args[0](input);
|
2015-02-24 02:52:53 +00:00
|
|
|
// ... should have been placed into the model
|
|
|
|
expect(mockScope.ngModel.testKey).toEqual("test user input");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("supplies initial model state to the dialog", function () {
|
|
|
|
var key, state;
|
2018-06-30 00:32:59 +00:00
|
|
|
mockScope.$watch.calls.mostRecent().args[1](testStructure);
|
2015-02-24 02:52:53 +00:00
|
|
|
controller.getButtonStructure().click();
|
|
|
|
// Find the key that the dialog should return
|
2018-06-30 00:32:59 +00:00
|
|
|
key = mockDialogService.getUserInput.calls.mostRecent()
|
2015-02-24 02:52:53 +00:00
|
|
|
.args[0].sections[0].rows[0].key;
|
|
|
|
// Get the initial state provided to the dialog
|
2018-06-30 00:32:59 +00:00
|
|
|
state = mockDialogService.getUserInput.calls.mostRecent().args[1];
|
2015-02-24 02:52:53 +00:00
|
|
|
// Should have had value from ngModel stored to that key
|
|
|
|
expect(state[key]).toEqual("initial test value");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-05-19 18:29:13 +00:00
|
|
|
);
|