From 8d6b2186469521a86d6ac3c85cecc900ba90692f Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 17 Feb 2015 09:03:23 -0800 Subject: [PATCH] [Fixed Position] Add initial spec Add initial spec for FixedController to satisfy minimum coverage requirements, in preparation for review. WTD-889. --- .../layout/test/FixedControllerSpec.js | 162 ++++++++++++++++++ platform/features/layout/test/suite.json | 1 + 2 files changed, 163 insertions(+) create mode 100644 platform/features/layout/test/FixedControllerSpec.js diff --git a/platform/features/layout/test/FixedControllerSpec.js b/platform/features/layout/test/FixedControllerSpec.js new file mode 100644 index 0000000000..0293905909 --- /dev/null +++ b/platform/features/layout/test/FixedControllerSpec.js @@ -0,0 +1,162 @@ +/*global define,describe,it,expect,beforeEach,jasmine*/ + +define( + ["../src/FixedController"], + function (FixedController) { + "use strict"; + + describe("The Fixed Position controller", function () { + var mockScope, + mockSubscriber, + mockFormatter, + mockDomainObject, + mockSubscription, + testGrid, + testModel, + testValues, + controller; + + // Utility function; find a watch for a given expression + function findWatch(expr) { + var watch; + mockScope.$watch.calls.forEach(function (call) { + if (call.args[0] === expr) { + watch = call.args[1]; + } + }); + return watch; + } + + function makeMockDomainObject(id) { + var mockObject = jasmine.createSpyObj( + 'domainObject-' + id, + [ 'getId' ] + ); + mockObject.getId.andReturn(id); + return mockObject; + } + + beforeEach(function () { + mockScope = jasmine.createSpyObj( + '$scope', + [ "$on", "$watch" ] + ); + mockSubscriber = jasmine.createSpyObj( + 'telemetrySubscriber', + [ 'subscribe' ] + ); + mockFormatter = jasmine.createSpyObj( + 'telemetryFormatter', + [ 'formatDomainValue', 'formatRangeValue' ] + ); + mockDomainObject = jasmine.createSpyObj( + 'domainObject', + [ 'getId', 'getModel', 'getCapability' ] + ); + mockSubscription = jasmine.createSpyObj( + 'subscription', + [ 'unsubscribe', 'getTelemetryObjects', 'getRangeValue' ] + ); + + testGrid = [ 123, 456 ]; + testModel = { + composition: ['a', 'b', 'c'], + layoutGrid: testGrid + }; + testValues = { a: 10, b: 42, c: 31.42 }; + + mockSubscriber.subscribe.andReturn(mockSubscription); + mockSubscription.getTelemetryObjects.andReturn( + testModel.composition.map(makeMockDomainObject) + ); + mockSubscription.getRangeValue.andCallFake(function (o) { + return testValues[o.getId()]; + }); + mockFormatter.formatRangeValue.andCallFake(function (v) { + return "Formatted " + v; + }); + + controller = new FixedController( + mockScope, + mockSubscriber, + mockFormatter + ); + }); + + it("provides styles for cells", function () { + expect(controller.getCellStyles()) + .toEqual(jasmine.any(Array)); + }); + + it("subscribes when a domain object is available", function () { + mockScope.domainObject = mockDomainObject; + findWatch("domainObject")(mockDomainObject); + expect(mockSubscriber.subscribe).toHaveBeenCalledWith( + mockDomainObject, + jasmine.any(Function) + ); + }); + + it("releases subscriptions when domain objects change", function () { + mockScope.domainObject = mockDomainObject; + + // First pass - should simply should subscribe + findWatch("domainObject")(mockDomainObject); + expect(mockSubscription.unsubscribe).not.toHaveBeenCalled(); + expect(mockSubscriber.subscribe.calls.length).toEqual(1); + + // Object changes - should unsubscribe then resubscribe + findWatch("domainObject")(mockDomainObject); + expect(mockSubscription.unsubscribe).toHaveBeenCalled(); + expect(mockSubscriber.subscribe.calls.length).toEqual(2); + }); + + it("configures view based on model", function () { + mockScope.model = testModel; + findWatch("model")(mockScope.model); + // Should have styles for all elements of composition + expect(controller.getStyle('a')).toBeDefined(); + expect(controller.getStyle('b')).toBeDefined(); + expect(controller.getStyle('c')).toBeDefined(); + expect(controller.getStyle('d')).not.toBeDefined(); + }); + + it("provides values for telemetry elements", function () { + // Initialize + mockScope.domainObject = mockDomainObject; + mockScope.model = testModel; + findWatch("domainObject")(mockDomainObject); + findWatch("model")(mockScope.model); + + // Invoke the subscription callback + mockSubscriber.subscribe.mostRecentCall.args[1](); + + // Formatted values should be available + expect(controller.getValue('a')).toEqual("Formatted 10"); + expect(controller.getValue('b')).toEqual("Formatted 42"); + expect(controller.getValue('c')).toEqual("Formatted 31.42"); + }); + + it("adds grid cells to fill boundaries", function () { + var s1 = { + width: testGrid[0] * 8, + height: testGrid[1] * 4 + }, + s2 = { + width: testGrid[0] * 10, + height: testGrid[1] * 6 + }; + + mockScope.model = testModel; + findWatch("model")(mockScope.model); + + // Set first bounds + controller.setBounds(s1); + expect(controller.getCellStyles().length).toEqual(32); // 8 * 4 + // Set new bounds + controller.setBounds(s2); + expect(controller.getCellStyles().length).toEqual(60); // 10 * 6 + }); + }); + } +); \ No newline at end of file diff --git a/platform/features/layout/test/suite.json b/platform/features/layout/test/suite.json index 9fbd76b85f..6e62994ff5 100644 --- a/platform/features/layout/test/suite.json +++ b/platform/features/layout/test/suite.json @@ -1,4 +1,5 @@ [ + "FixedController", "LayoutController", "LayoutDrag" ] \ No newline at end of file