From 9506d309b0f8cc4de02446b72be2a0bdb0db4954 Mon Sep 17 00:00:00 2001 From: Even Stensberg Date: Thu, 9 Feb 2017 12:00:50 +0100 Subject: [PATCH] [TEST] Add Unit Test for URLIndicator Adds the first test for the URLIndicator. [DOCS] fix docs api indenting fix linting fix linting fix docs --- API.md | 26 +-- .../URLIndicatorPlugin/URLIndicatorSpec.js | 158 ++++++++++++++++++ 2 files changed, 173 insertions(+), 11 deletions(-) create mode 100644 src/plugins/URLIndicatorPlugin/URLIndicatorSpec.js diff --git a/API.md b/API.md index e9147e1679..58f0f608c3 100644 --- a/API.md +++ b/API.md @@ -879,6 +879,21 @@ openmct.install(openmct.plugins.CouchDB('http://localhost:9200')) * `openmct.plugins.Espresso` and `openmct.plugins.Snow` are two different themes (dark and light) available for Open MCT. Note that at least one of these themes must be installed for Open MCT to appear correctly. +* `openmct.plugins.URLIndicatorPlugin` adds an indicator which shows the +availability of a URL with the following options: + - `url` : URL to indicate the status of + - `icon`: Icon to show in the status bar, defaults to `database` + - `interval`: Interval between checking the connection, defaults to `10000` + - `label` Name showing up as text in the status bar, defaults to url +```javascript +openmct.install(openmct.plugins.URLIndicatorPlugin({ + url: 'http://google.com', + icon: 'check', + interval: 10000, + label: 'Google' + }) +); +``` * `openmct.plugins.LocalStorage` provides persistence of user-created objects in browser-local storage. This is particularly useful in development environments. @@ -886,17 +901,6 @@ openmct.install(openmct.plugins.CouchDB('http://localhost:9200')) when the application is first started, providing a place for a user to store created items. * `openmct.plugins.UTCTimeSystem` provides a default time system for Open MCT. -* `openmct.plugins.URLIndicatorPlugin({ - url: 'http://google.com', - icon: 'check', - interval: 10000, - label: 'Google' - })` adds an indicator which shows the - availability of a URL with the following options: - - `url` : URL to indicate the status of - - `icon`: Icon to show in the status bar, defaults to `database` - - `interval`: Interval between checking the connection, defaults to `10000` - - `label` Name showing up as text in the status bar, defaults to url Generally, you will want to either install these plugins, or install different plugins that provide persistence and an initial folder diff --git a/src/plugins/URLIndicatorPlugin/URLIndicatorSpec.js b/src/plugins/URLIndicatorPlugin/URLIndicatorSpec.js new file mode 100644 index 0000000000..c3d7d27084 --- /dev/null +++ b/src/plugins/URLIndicatorPlugin/URLIndicatorSpec.js @@ -0,0 +1,158 @@ +/***************************************************************************** + * Open MCT, Copyright (c) 2014-2016, United States Government + * as represented by the Administrator of the National Aeronautics and Space + * Administration. All rights reserved. + * + * Open MCT 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 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. + *****************************************************************************/ + +define( + ["./URLIndicator"], + function (URLIndicator) { + + describe("The URLIndicator", function () { + var mockHttp, + mockInterval, + mockPromise, + opts, + Indicator, + indicatorWrapper; + + beforeEach(function () { + mockHttp = jasmine.createSpyObj("$http", ["get"]); + mockInterval = jasmine.createSpy("$interval"); + mockPromise = jasmine.createSpyObj("promise", ["then"]); + opts = { + url: "http://localhost:8080", + interval: 1337 //some number + }; + mockHttp.get.andReturn(mockPromise); + Indicator = function () { + this.options = opts; + URLIndicator.call(this, mockHttp, mockInterval); + }; + Indicator.prototype = Object.create(URLIndicator.prototype); + indicatorWrapper = new Indicator(); + }); + it("polls for changes", function () { + expect(mockInterval).toHaveBeenCalledWith( + jasmine.any(Function), + opts.interval, + 0, + false + ); + }); + + it("has a database icon as default", function () { + expect(indicatorWrapper.getCssClass()).toEqual("icon-database"); + }); + + it("consults the url with the path supplied", function () { + expect(mockHttp.get).toHaveBeenCalledWith(opts.url); + }); + + it("changes when the database connection is nominal", function () { + var initialText = indicatorWrapper.getText(), + initialDescrption = indicatorWrapper.getDescription(), + initialGlyphClass = indicatorWrapper.getGlyphClass(); + + // Nominal just means getting back an object, without + // an error field. + mockPromise.then.mostRecentCall.args[0]({ data: {} }); + + // Verify that these values changed; + // don't test for specific text. + expect(indicatorWrapper.getText()).not.toEqual(initialText); + expect(indicatorWrapper.getGlyphClass()).not.toEqual(initialGlyphClass); + expect(indicatorWrapper.getDescription()).not.toEqual(initialDescrption); + + // Do check for specific class + expect(indicatorWrapper.getGlyphClass()).toEqual("ok"); + }); + + it("changes when the server cannot be reached", function () { + var initialText = indicatorWrapper.getText(), + initialDescrption = indicatorWrapper.getDescription(), + initialGlyphClass = indicatorWrapper.getGlyphClass(); + + // Nominal just means getting back an object, without + // an error field. + mockPromise.then.mostRecentCall.args[1]({ data: {} }); + + // Verify that these values changed; + // don't test for specific text. + expect(indicatorWrapper.getText()).not.toEqual(initialText); + expect(indicatorWrapper.getGlyphClass()).not.toEqual(initialGlyphClass); + expect(indicatorWrapper.getDescription()).not.toEqual(initialDescrption); + + // Do check for specific class + expect(indicatorWrapper.getGlyphClass()).toEqual("err"); + }); + it("has a customized icon if supplied in initialization", function () { + opts = { + url: "http://localhost:8080", + icon: "checked", + interval: 10000 + }; + indicatorWrapper = new Indicator(); + expect(indicatorWrapper.getCssClass()).toEqual("icon-checked"); + }); + it("has a customized interval if supplied in initialization", function () { + opts = { + url: "http://localhost:8080", + interval: 1814 + }; + indicatorWrapper = new Indicator(); + expect(mockInterval).toHaveBeenCalledWith( + jasmine.any(Function), + 1814, + 0, + false + ); + }); + it("has a custom label if supplied in initialization", function () { + opts = { + url: "http://localhost:8080", + label: "Localhost" + }; + indicatorWrapper = new Indicator(); + expect(indicatorWrapper.getText()).toEqual("Checking status of Localhost please stand by..."); + }); + it("has a default label if not supplied in initialization", function () { + opts = { + url: "http://localhost:8080" + }; + indicatorWrapper = new Indicator(); + expect(indicatorWrapper.getText()).toEqual( + "Checking status of http://localhost:8080 please stand by..." + ); + }); + it("has a default interval if not supplied in initialization", function () { + opts = { + url: "http://localhost:8080" + }; + indicatorWrapper = new Indicator(); + expect(mockInterval).toHaveBeenCalledWith( + jasmine.any(Function), + 10000, + 0, + false + ); + }); + }); + } +);