From eee87906826ee4d8efd7c41d9f1d02ba1fbdb704 Mon Sep 17 00:00:00 2001 From: slhale Date: Mon, 17 Aug 2015 13:46:28 -0700 Subject: [PATCH] [Search] Add tests Added tests for SearchMenuController. --- .../src/controllers/SearchMenuController.js | 2 +- .../controllers/SearchMenuControllerSpec.js | 133 ++++++++++++++++++ platform/search/test/suite.json | 1 + 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 platform/search/test/controllers/SearchMenuControllerSpec.js diff --git a/platform/search/src/controllers/SearchMenuController.js b/platform/search/src/controllers/SearchMenuController.js index c43f5d3b2f..248264a37a 100644 --- a/platform/search/src/controllers/SearchMenuController.js +++ b/platform/search/src/controllers/SearchMenuController.js @@ -60,7 +60,7 @@ define(function () { for (type in $scope.ngModel.checked) { if ($scope.ngModel.checked[type]) { $scope.ngModel.checkAll = false; - } + } } } diff --git a/platform/search/test/controllers/SearchMenuControllerSpec.js b/platform/search/test/controllers/SearchMenuControllerSpec.js new file mode 100644 index 0000000000..9e31f461c5 --- /dev/null +++ b/platform/search/test/controllers/SearchMenuControllerSpec.js @@ -0,0 +1,133 @@ +/***************************************************************************** + * 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. + *****************************************************************************/ +/*global define,describe,it,expect,beforeEach,jasmine*/ + +/** + * SearchSpec. Created by shale on 08/17/2015. + */ +define( + ["../../src/controllers/SearchMenuController"], + function (SearchMenuController) { + "use strict"; + + describe("The search menu controller", function () { + var mockScope, + mockPromise, + mockTypes, + controller; + + beforeEach(function () { + mockScope = jasmine.createSpyObj( + "$scope", + [ "" ] + ); + + mockTypes = [ + {key: 'mock.type.1', name: 'Mock Type 1', glyph: 'a'}, + {key: 'mock.type.2', name: 'Mock Type 2', glyph: 'b'} + ]; + + mockScope.ngModel = {}; + mockScope.ngModel.checked = {}; + mockScope.ngModel.checked['mock.type.1'] = false; + mockScope.ngModel.checked['mock.type.2'] = false; + mockScope.ngModel.checkAll = true; + mockScope.ngModel.filter = jasmine.createSpy('$scope.ngModel.filter'); + mockScope.ngModel.filtersString = ''; + + controller = new SearchMenuController(mockScope, mockTypes); + }); + + it("gets types on initiliztion", function () { + expect(mockScope.ngModel.types).toBeDefined(); + }); + + it("refilters results when options are updated", function () { + controller.updateOptions(); + expect(mockScope.ngModel.filter).toHaveBeenCalled(); + + controller.checkAll(); + expect(mockScope.ngModel.filter).toHaveBeenCalled(); + }); + + it("updates the filters string when options are updated", function () { + controller.updateOptions(); + expect(mockScope.ngModel.filtersString).toEqual(''); + + mockScope.ngModel.checked['mock.type.1'] = true; + + controller.updateOptions(); + expect(mockScope.ngModel.filtersString).not.toEqual(''); + }); + + it("changing checkAll status updates the filter string", function () { + controller.checkAll(); + expect(mockScope.ngModel.filtersString).toEqual(''); + + mockScope.ngModel.checkAll = false; + + controller.checkAll(); + expect(mockScope.ngModel.filtersString).toEqual('NONE'); + }); + + it("checking checkAll option resets other options", function () { + var type; + + mockScope.ngModel.checked['mock.type.1'] = true; + mockScope.ngModel.checked['mock.type.2'] = true; + + controller.checkAll(); + + for (type in mockScope.ngModel.checked) { + expect(mockScope.ngModel.checked[type]).toBeFalsy(); + } + }); + + it("tells the user when no options are checked", function () { + var type; + + for (type in mockScope.ngModel.checked) { + mockScope.ngModel.checked[type] = false; + } + mockScope.ngModel.checkAll = false; + + controller.updateOptions(); + + expect(mockScope.ngModel.filtersString).toEqual('NONE'); + }); + + it("tells the user when options are checked", function () { + var type; + + mockScope.ngModel.checkAll = false; + for (type in mockScope.ngModel.checked) { + mockScope.ngModel.checked[type] = true; + } + + controller.updateOptions(); + + expect(mockScope.ngModel.filtersString).not.toEqual('NONE'); + expect(mockScope.ngModel.filtersString).not.toEqual(''); + }); + }); + } +); \ No newline at end of file diff --git a/platform/search/test/suite.json b/platform/search/test/suite.json index f9d5372a89..8dda6b5086 100644 --- a/platform/search/test/suite.json +++ b/platform/search/test/suite.json @@ -1,5 +1,6 @@ [ "controllers/SearchController", + "controllers/SearchMenuController", "controllers/SearchItemController", "controllers/ClickAwayController", "services/SearchAggregator",