From ca62cc9066d6e4e7c9e68e204ce2da575d0d72b1 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 28 Oct 2015 16:35:08 -0700 Subject: [PATCH] [Representation] Update spec for mct-representation ...to account for usage of templateLinker to add or remove the whole element from the DOM when there is or is not a template to show. --- .../test/MCTRepresentationSpec.js | 90 ++++++++++--------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/platform/representation/test/MCTRepresentationSpec.js b/platform/representation/test/MCTRepresentationSpec.js index a50347df70..1f2d55b4ab 100644 --- a/platform/representation/test/MCTRepresentationSpec.js +++ b/platform/representation/test/MCTRepresentationSpec.js @@ -38,8 +38,9 @@ define( testViews, mockRepresenters, mockQ, - mockSce, + mockLinker, mockLog, + mockChangeTemplate, mockScope, mockElement, mockDomainObject, @@ -54,6 +55,14 @@ define( }; } + function fireWatch(expr, value) { + mockScope.$watch.calls.forEach(function (call) { + if (call.args[0] === expr) { + call.args[1](value); + } + }); + } + beforeEach(function () { testRepresentations = [ { @@ -96,45 +105,38 @@ define( }); mockQ = { when: mockPromise }; - mockSce = jasmine.createSpyObj( - '$sce', - ['trustAsResourceUrl'] - ); + mockLinker = jasmine.createSpyObj('templateLinker', ['link']); + mockChangeTemplate = jasmine.createSpy('changeTemplate'); mockLog = jasmine.createSpyObj("$log", LOG_FUNCTIONS); - - mockSce.trustAsResourceUrl.andCallFake(function (url) { - return url; - }); mockScope = jasmine.createSpyObj("scope", [ "$watch", "$on" ]); mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS); mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS); mockDomainObject.getModel.andReturn(testModel); + mockLinker.link.andReturn(mockChangeTemplate); mctRepresentation = new MCTRepresentation( testRepresentations, testViews, mockRepresenters, mockQ, - mockSce, + mockLinker, mockLog ); - }); - - - it("has a built-in template, with ng-include src=inclusion", function () { - // Not rigorous, but should detect many cases when template is broken. - expect(mctRepresentation.template.indexOf("ng-include")).not.toEqual(-1); - expect(mctRepresentation.template.indexOf("inclusion")).not.toEqual(-1); + mctRepresentation.link(mockScope, mockElement); }); it("is restricted to elements", function () { expect(mctRepresentation.restrict).toEqual("E"); }); + it("exposes templates via the templateLinker", function () { + expect(mockLinker.link) + .toHaveBeenCalledWith(mockScope, mockElement); + }); + it("watches scope when linked", function () { - mctRepresentation.link(mockScope, mockElement); expect(mockScope.$watch).toHaveBeenCalledWith( "key", jasmine.any(Function) @@ -150,42 +152,46 @@ define( }); it("recognizes keys for representations", function () { - mctRepresentation.link(mockScope, mockElement); - mockScope.key = "abc"; + mockScope.domainObject = mockDomainObject; // Trigger the watch - mockScope.$watch.calls[0].args[1](); + fireWatch('key', mockScope.key); + fireWatch('domainObject', mockDomainObject); - expect(mockScope.inclusion).toEqual("a/b/c/template.html"); + expect(mockChangeTemplate) + .toHaveBeenCalledWith("a/b/c/template.html"); }); it("recognizes keys for views", function () { - mctRepresentation.link(mockScope, mockElement); - mockScope.key = "xyz"; + mockScope.domainObject = mockDomainObject; - // Trigger the watch - mockScope.$watch.calls[0].args[1](); + // Trigger the watches + fireWatch('key', mockScope.key); + fireWatch('domainObject', mockDomainObject); - expect(mockScope.inclusion).toEqual("x/y/z/template.html"); - }); - - it("trusts template URLs", function () { - mctRepresentation.link(mockScope, mockElement); - - mockScope.key = "xyz"; - - // Trigger the watch - mockScope.$watch.calls[0].args[1](); - - expect(mockSce.trustAsResourceUrl) + expect(mockChangeTemplate) .toHaveBeenCalledWith("x/y/z/template.html"); }); - it("loads declared capabilities", function () { - mctRepresentation.link(mockScope, mockElement); + it("does not load templates until there is an object", function () { + mockScope.key = "xyz"; + // Trigger the watch + fireWatch('key', mockScope.key); + + expect(mockChangeTemplate) + .not.toHaveBeenCalledWith(jasmine.any(String)); + + mockScope.domainObject = mockDomainObject; + fireWatch('domainObject', mockDomainObject); + + expect(mockChangeTemplate) + .toHaveBeenCalledWith(jasmine.any(String)); + }); + + it("loads declared capabilities", function () { mockScope.key = "def"; mockScope.domainObject = mockDomainObject; @@ -199,8 +205,6 @@ define( }); it("logs when no representation is available for a key", function () { - mctRepresentation.link(mockScope, mockElement); - mockScope.key = "someUnknownThing"; // Verify precondition @@ -214,8 +218,6 @@ define( }); it("clears out obsolete peroperties from scope", function () { - mctRepresentation.link(mockScope, mockElement); - mockScope.key = "def"; mockScope.domainObject = mockDomainObject; mockDomainObject.useCapability.andReturn("some value");