diff --git a/platform/commonUI/general/test/controllers/GetterSetterControllerSpec.js b/platform/commonUI/general/test/controllers/GetterSetterControllerSpec.js index 4532a23fc7..caa4e0c724 100644 --- a/platform/commonUI/general/test/controllers/GetterSetterControllerSpec.js +++ b/platform/commonUI/general/test/controllers/GetterSetterControllerSpec.js @@ -6,6 +6,58 @@ define( "use strict"; describe("The getter-setter controller", function () { + var mockScope, + mockModel, + controller; + + beforeEach(function () { + mockScope = jasmine.createSpyObj("$scope", ["$watch"]); + mockModel = jasmine.createSpy("ngModel"); + mockScope.ngModel = mockModel; + controller = new GetterSetterController(mockScope); + }); + + it("watches for changes to external and internal mode", function () { + expect(mockScope.$watch).toHaveBeenCalledWith( + "ngModel()", + jasmine.any(Function) + ); + expect(mockScope.$watch).toHaveBeenCalledWith( + "getterSetter.value", + jasmine.any(Function) + ); + }); + + it("updates an external function when changes are detected", function () { + mockScope.getterSetter.value = "some new value"; + // Verify precondition + expect(mockScope.ngModel) + .not.toHaveBeenCalledWith("some new value"); + // Fire the matching watcher + mockScope.$watch.calls.forEach(function (call) { + if (call.args[0] === "getterSetter.value") { + call.args[1](mockScope.getterSetter.value); + } + }); + // Verify getter-setter was notified + expect(mockScope.ngModel) + .toHaveBeenCalledWith("some new value"); + }); + + it("updates internal state when external changes are detected", function () { + mockScope.ngModel.andReturn("some other new value"); + // Verify precondition + expect(mockScope.getterSetter.value).toBeUndefined(); + // Fire the matching watcher + mockScope.$watch.calls.forEach(function (call) { + if (call.args[0] === "ngModel()") { + call.args[1]("some other new value"); + } + }); + // Verify state in scope was updated + expect(mockScope.getterSetter.value) + .toEqual("some other new value"); + }); }); } diff --git a/platform/commonUI/general/test/directives/MCTResizeSpec.js b/platform/commonUI/general/test/directives/MCTResizeSpec.js index 3869fdac8e..4f07701d50 100644 --- a/platform/commonUI/general/test/directives/MCTResizeSpec.js +++ b/platform/commonUI/general/test/directives/MCTResizeSpec.js @@ -6,6 +6,62 @@ define( "use strict"; describe("The mct-resize directive", function () { + var mockTimeout, + mockScope, + testElement, + testAttrs, + mctResize; + + beforeEach(function () { + mockTimeout = jasmine.createSpy("$timeout"); + mockScope = jasmine.createSpyObj("$scope", ["$eval"]); + + testElement = { offsetWidth: 100, offsetHeight: 200 }; + testAttrs = { mctResize: "some-expr" }; + + mctResize = new MCTResize(mockTimeout); + }); + + it("is applicable as an attribute only", function () { + expect(mctResize.restrict).toEqual("A"); + }); + + it("starts tracking size changes upon link", function () { + expect(mockTimeout).not.toHaveBeenCalled(); + mctResize.link(mockScope, [testElement], testAttrs); + expect(mockTimeout).toHaveBeenCalledWith( + jasmine.any(Function), + jasmine.any(Number) + ); + expect(mockScope.$eval).toHaveBeenCalledWith( + testAttrs.mctResize, + { bounds: { width: 100, height: 200 } } + ); + }); + + it("reports size changes on a timeout", function () { + mctResize.link(mockScope, [testElement], testAttrs); + + // Change the element's apparent size + testElement.offsetWidth = 300; + testElement.offsetHeight = 350; + + // Shouldn't know about this yet... + expect(mockScope.$eval).not.toHaveBeenCalledWith( + testAttrs.mctResize, + { bounds: { width: 300, height: 350 } } + ); + + // Fire the timeout + mockTimeout.mostRecentCall.args[0](); + + // Should have triggered an evaluation of mctResize + // with the new width & height + expect(mockScope.$eval).toHaveBeenCalledWith( + testAttrs.mctResize, + { bounds: { width: 300, height: 350 } } + ); + }); }); }