From 4a572decf3e46bc281b689af9b6ade0f1e60a3d4 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 16 Dec 2014 14:06:19 -0800 Subject: [PATCH] [Indicators] Fill in specs Fill in specs to ensure code coverage for scripts introduced to support indicators as a category of extension. WTD-608. --- .../general/test/BottomBarControllerSpec.js | 42 ++++++++ platform/persistence/src/CouchIndicator.js | 2 +- .../persistence/test/CouchIndicatorSpec.js | 99 +++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) diff --git a/platform/commonUI/general/test/BottomBarControllerSpec.js b/platform/commonUI/general/test/BottomBarControllerSpec.js index 1f228e0628..c163105416 100644 --- a/platform/commonUI/general/test/BottomBarControllerSpec.js +++ b/platform/commonUI/general/test/BottomBarControllerSpec.js @@ -7,7 +7,49 @@ define( describe("The bottom bar controller", function () { var testIndicators, + testIndicatorA, + testIndicatorB, + testIndicatorC, + mockIndicator, controller; + + beforeEach(function () { + mockIndicator = jasmine.createSpyObj( + "indicator", + [ "getGlyph", "getText" ] + ); + + testIndicatorA = {}; + testIndicatorB = function () { return mockIndicator; }; + testIndicatorC = { template: "someTemplate" }; + + testIndicators = [ + testIndicatorA, + testIndicatorB, + testIndicatorC + ]; + + controller = new BottomBarController(testIndicators); + }); + + it("exposes one indicator description per extension", function () { + expect(controller.getIndicators().length) + .toEqual(testIndicators.length); + }); + + it("uses template field provided, or its own default", function () { + // "indicator" is the default; + // only testIndicatorC overrides this. + var indicators = controller.getIndicators(); + expect(indicators[0].template).toEqual("indicator"); + expect(indicators[1].template).toEqual("indicator"); + expect(indicators[2].template).toEqual("someTemplate"); + }); + + it("instantiates indicators given as constructors", function () { + // testIndicatorB constructs to mockIndicator + expect(controller.getIndicators()[1].ngModel).toBe(mockIndicator); + }); }); } ); \ No newline at end of file diff --git a/platform/persistence/src/CouchIndicator.js b/platform/persistence/src/CouchIndicator.js index 5cc13a251f..c5367f7cb7 100644 --- a/platform/persistence/src/CouchIndicator.js +++ b/platform/persistence/src/CouchIndicator.js @@ -48,7 +48,7 @@ define( // report an error, so check for that. function handleResponse(response) { var data = response.data; - state = data.error ? DISCONNECTED : CONNECTED; + state = data.error ? SEMICONNECTED : CONNECTED; } // Try to connect to CouchDB, and update the indicator. diff --git a/platform/persistence/test/CouchIndicatorSpec.js b/platform/persistence/test/CouchIndicatorSpec.js index 855e20c892..1b8f5b521d 100644 --- a/platform/persistence/test/CouchIndicatorSpec.js +++ b/platform/persistence/test/CouchIndicatorSpec.js @@ -6,6 +6,105 @@ define( "use strict"; describe("The CouchDB status indicator", function () { + var mockHttp, + mockInterval, + testPath, + testInterval, + mockPromise, + indicator; + + beforeEach(function () { + mockHttp = jasmine.createSpyObj("$http", [ "get" ]); + mockInterval = jasmine.createSpy("$interval"); + mockPromise = jasmine.createSpyObj("promise", [ "then" ]); + testPath = "/test/path"; + testInterval = 12321; // Some number + + mockHttp.get.andReturn(mockPromise); + + indicator = new CouchIndicator( + mockHttp, + mockInterval, + testPath, + testInterval + ); + }); + + it("polls for changes", function () { + expect(mockInterval).toHaveBeenCalledWith( + jasmine.any(Function), + testInterval + ); + }); + + it("has a database icon", function () { + expect(indicator.getGlyph()).toEqual("D"); + }); + + it("consults the database at the configured path", function () { + expect(mockHttp.get).toHaveBeenCalledWith(testPath); + }); + + it("changes when the database connection is nominal", function () { + var initialText = indicator.getText(), + initialDescrption = indicator.getDescription(), + initialGlyphClass = indicator.getGlyphClass(); + + // Nominal just means getting back an objeect, without + // an error field. + mockPromise.then.mostRecentCall.args[0]({ data: {} }); + + // Verify that these values changed; + // don't test for specific text. + expect(indicator.getText()).not.toEqual(initialText); + expect(indicator.getGlyphClass()).not.toEqual(initialGlyphClass); + expect(indicator.getDescription()).not.toEqual(initialDescrption); + + // Do check for specific class + expect(indicator.getGlyphClass()).toEqual("ok"); + }); + + it("changes when the server reports an error", function () { + var initialText = indicator.getText(), + initialDescrption = indicator.getDescription(), + initialGlyphClass = indicator.getGlyphClass(); + + // Nominal just means getting back an objeect, with + // an error field. + mockPromise.then.mostRecentCall.args[0]( + { data: { error: "Uh oh." } } + ); + + // Verify that these values changed; + // don't test for specific text. + expect(indicator.getText()).not.toEqual(initialText); + expect(indicator.getGlyphClass()).not.toEqual(initialGlyphClass); + expect(indicator.getDescription()).not.toEqual(initialDescrption); + + // Do check for specific class + expect(indicator.getGlyphClass()).toEqual("caution"); + + }); + + it("changes when the server cannot be reached", function () { + var initialText = indicator.getText(), + initialDescrption = indicator.getDescription(), + initialGlyphClass = indicator.getGlyphClass(); + + // Nominal just means getting back an objeect, without + // an error field. + mockPromise.then.mostRecentCall.args[1]({ data: {} }); + + // Verify that these values changed; + // don't test for specific text. + expect(indicator.getText()).not.toEqual(initialText); + expect(indicator.getGlyphClass()).not.toEqual(initialGlyphClass); + expect(indicator.getDescription()).not.toEqual(initialDescrption); + + // Do check for specific class + expect(indicator.getGlyphClass()).toEqual("err"); + }); + }); }