From 22b7bb07aa0ca2a71d23c33f6cbd7638c8c64fe4 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Mon, 4 Jan 2016 12:19:33 -0800 Subject: [PATCH] [Common UI] Add mct-split-pane test cases ...sufficient for full line coverage. WTD-1400 --- .../test/directives/MCTSplitPaneSpec.js | 119 +++++++++++++++++- 1 file changed, 116 insertions(+), 3 deletions(-) diff --git a/platform/commonUI/general/test/directives/MCTSplitPaneSpec.js b/platform/commonUI/general/test/directives/MCTSplitPaneSpec.js index 2d5d5ac5a0..0743f1a584 100644 --- a/platform/commonUI/general/test/directives/MCTSplitPaneSpec.js +++ b/platform/commonUI/general/test/directives/MCTSplitPaneSpec.js @@ -30,13 +30,16 @@ define( 'on', 'addClass', 'children', - 'eq' + 'eq', + 'toggleClass', + 'css' ]; describe("The mct-split-pane directive", function () { var mockParse, mockLog, mockInterval, + mockParsed, mctSplitPane; beforeEach(function () { @@ -45,6 +48,11 @@ define( jasmine.createSpyObj('$log', ['warn', 'info', 'debug']); mockInterval = jasmine.createSpy('$interval'); mockInterval.cancel = jasmine.createSpy('mockCancel'); + mockParsed = jasmine.createSpy('parsed'); + mockParsed.assign = jasmine.createSpy('assign'); + + mockParse.andReturn(mockParsed); + mctSplitPane = new MCTSplitPane( mockParse, mockLog, @@ -61,8 +69,19 @@ define( mockElement, testAttrs, mockChildren, + mockFirstPane, + mockSplitter, + mockSecondPane, controller; + function fireOn(eventType) { + mockScope.$on.calls.forEach(function (call) { + if (call.args[0] === eventType) { + call.args[1](); + } + }); + } + beforeEach(function () { mockScope = jasmine.createSpyObj('$scope', ['$apply', '$watch', '$on']); @@ -71,10 +90,33 @@ define( testAttrs = {}; mockChildren = jasmine.createSpyObj('children', JQLITE_METHODS); + mockFirstPane = + jasmine.createSpyObj('firstPane', JQLITE_METHODS); + mockSplitter = + jasmine.createSpyObj('splitter', JQLITE_METHODS); + mockSecondPane = + jasmine.createSpyObj('secondPane', JQLITE_METHODS); mockElement.children.andReturn(mockChildren); - mockChildren.eq.andReturn(mockChildren); - mockChildren[0] = {}; + mockElement[0] = { + offsetWidth: 12321, + offsetHeight: 45654 + }; + mockChildren.eq.andCallFake(function (i) { + return [mockFirstPane, mockSplitter, mockSecondPane][i]; + }); + mockFirstPane[0] = { offsetWidth: 123, offsetHeight: 456 }; + mockSplitter[0] = { + nodeName: 'mct-splitter', + offsetWidth: 10, + offsetHeight: 456 + }; + mockSecondPane[0] = { offsetWidth: 10, offsetHeight: 456 }; + + mockChildren[0] = mockFirstPane[0]; + mockChildren[1] = mockSplitter[0]; + mockChildren[3] = mockSecondPane[0]; + mockChildren.length = 3; controller = mctSplitPane.controller[3]( mockScope, @@ -87,6 +129,77 @@ define( expect(mockInterval.mostRecentCall.args[3]).toBe(false); }); + it("exposes its splitter's initial position", function () { + expect(controller.position()).toEqual( + mockFirstPane[0].offsetWidth + mockSplitter[0].offsetWidth + ); + }); + + it("exposes the current anchoring mode", function () { + expect(controller.anchor()).toEqual({ + edge : 'left', + opposite : 'right', + dimension : 'width', + orientation : 'vertical' + }); + }); + + it("allows classes to be toggled on contained elements", function () { + controller.toggleClass('resizing'); + expect(mockChildren.toggleClass) + .toHaveBeenCalledWith('resizing'); + }); + + it("allows positions to be set", function () { + var testValue = mockChildren[0].offsetWidth + 50; + controller.position(testValue); + expect(mockFirstPane.css).toHaveBeenCalledWith( + 'width', + (testValue - mockSplitter[0].offsetWidth) + 'px' + ); + }); + + it("issues no warnings under nominal usage", function () { + expect(mockLog.warn).not.toHaveBeenCalled(); + }); + + it("warns if no mct-splitter is present", function () { + mockSplitter[0].nodeName = "not-mct-splitter"; + controller = mctSplitPane.controller[3]( + mockScope, + mockElement, + testAttrs + ); + expect(mockLog.warn).toHaveBeenCalled(); + }); + + it("warns if an unknown anchor key is given", function () { + testAttrs.anchor = "middle"; + controller = mctSplitPane.controller[3]( + mockScope, + mockElement, + testAttrs + ); + expect(mockLog.warn).toHaveBeenCalled(); + }); + + it("updates positions on a timer", function () { + mockFirstPane[0].offsetWidth += 100; + // Should not reflect the change yet + expect(controller.position()).not.toEqual( + mockFirstPane[0].offsetWidth + mockSplitter[0].offsetWidth + ); + mockInterval.mostRecentCall.args[0](); + expect(controller.position()).toEqual( + mockFirstPane[0].offsetWidth + mockSplitter[0].offsetWidth + ); + }); + + it("cancels the active interval when scope is destroyed", function () { + expect(mockInterval.cancel).not.toHaveBeenCalled(); + fireOn('$destroy'); + expect(mockInterval.cancel).toHaveBeenCalled(); + }); }); });