From f11eced6b90a87cc074e8b2c34360f0efd1fce1b Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 25 Feb 2015 15:46:36 -0800 Subject: [PATCH] [Common UI] Add spec for MCT scroll directive parent Add spec for MCTScroll, which will act as a parent class for mct-scroll-x and mct-scroll-y directives, WTD-920. --- .../general/src/directives/MCTScroll.js | 0 .../general/test/directives/MCTScrollSpec.js | 90 +++++++++++++++++++ platform/commonUI/general/test/suite.json | 1 + 3 files changed, 91 insertions(+) create mode 100644 platform/commonUI/general/src/directives/MCTScroll.js create mode 100644 platform/commonUI/general/test/directives/MCTScrollSpec.js diff --git a/platform/commonUI/general/src/directives/MCTScroll.js b/platform/commonUI/general/src/directives/MCTScroll.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/platform/commonUI/general/test/directives/MCTScrollSpec.js b/platform/commonUI/general/test/directives/MCTScrollSpec.js new file mode 100644 index 0000000000..7b5dfff77b --- /dev/null +++ b/platform/commonUI/general/test/directives/MCTScrollSpec.js @@ -0,0 +1,90 @@ +/*global define,describe,it,expect,jasmine,beforeEach*/ +define( + ['../../src/directives/MCTScroll'], + function (MCTScroll) { + "use strict"; + + var EVENT_PROPERTY = "testProperty", + ATTRIBUTE = "testAttribute", + EXPRESSION = "some.expression"; + + + // MCTScroll is the commonality between mct-scroll-x and + // mct-scroll-y; it gets the event property to watch and + // the attribute which contains the associated assignable + // expression. + describe("An mct-scroll-* directive", function () { + var mockParse, + mockParsed, + mockScope, + mockElement, + testAttrs, + mctScroll; + + beforeEach(function () { + mockParse = jasmine.createSpy('$parse'); + mockParsed = jasmine.createSpy('parsed'); + mockParsed.assign = jasmine.createSpy('assign'); + + mockScope = jasmine.createSpyObj('$scope', ['$watch']); + mockElement = [{ scrollLeft: 42 }]; + mockElement.on = jasmine.createSpy('on'); + + mockParse.andReturn(mockParsed); + + testAttrs = {}; + testAttrs[ATTRIBUTE] = EXPRESSION; + + mctScroll = new MCTScroll( + mockParse, + EVENT_PROPERTY, + ATTRIBUTE + ); + mctScroll.link(mockScope, mockElement, testAttrs); + }); + + it("is available for attributes", function () { + expect(mctScroll.restrict).toEqual('A'); + }); + + it("does not create an isolate scope", function () { + expect(mctScroll.scope).toBeUndefined(); + }); + + it("watches for changes in observed expression", function () { + expect(mockScope.$watch).toHaveBeenCalledWith( + EXPRESSION, + jasmine.any(Function) + ); + // Should have been only watch (other tests need this to be true) + expect(mockScope.$watch.calls.length).toEqual(0); + }); + + it("listens for scroll events", function () { + expect(mockElement.on).toHaveBeenCalledWith( + 'scroll', + jasmine.any(Function) + ); + // Should have been only listener (other tests need this to be true) + expect(mockElement.on.calls.length).toEqual(0); + }); + + it("publishes initial scroll state", function () { + expect(mockParse).toHaveBeenCalledWith(EXPRESSION); + expect(mockParsed.assign).toHaveBeenCalledWith(42); + }); + + it("updates scroll state when scope changes", function () { + mockScope.$watch.mostRecentCall.args[1](64); + expect(mockElement[0].scrollLeft).toEqual(64); + }); + + it("updates scope when scroll state changes", function () { + mockElement[0].scrollLeft = 12321; + mockElement.on.mostRecentCall.args({ target: mockElement[0] }); + expect(mockParsed.assign).toHaveBeenCalledWith(12321); + }); + + }); + } +); \ No newline at end of file diff --git a/platform/commonUI/general/test/suite.json b/platform/commonUI/general/test/suite.json index ae3a54f37b..58d94a4d95 100644 --- a/platform/commonUI/general/test/suite.json +++ b/platform/commonUI/general/test/suite.json @@ -11,5 +11,6 @@ "directives/MCTContainer", "directives/MCTDrag", "directives/MCTResize", + "directives/MCTScroll", "StyleSheetLoader" ] \ No newline at end of file