[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.
This commit is contained in:
Victor Woeltjen 2015-10-28 16:35:08 -07:00
parent 01b6fda1f2
commit ca62cc9066

View File

@ -38,8 +38,9 @@ define(
testViews, testViews,
mockRepresenters, mockRepresenters,
mockQ, mockQ,
mockSce, mockLinker,
mockLog, mockLog,
mockChangeTemplate,
mockScope, mockScope,
mockElement, mockElement,
mockDomainObject, 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 () { beforeEach(function () {
testRepresentations = [ testRepresentations = [
{ {
@ -96,45 +105,38 @@ define(
}); });
mockQ = { when: mockPromise }; mockQ = { when: mockPromise };
mockSce = jasmine.createSpyObj( mockLinker = jasmine.createSpyObj('templateLinker', ['link']);
'$sce', mockChangeTemplate = jasmine.createSpy('changeTemplate');
['trustAsResourceUrl']
);
mockLog = jasmine.createSpyObj("$log", LOG_FUNCTIONS); mockLog = jasmine.createSpyObj("$log", LOG_FUNCTIONS);
mockSce.trustAsResourceUrl.andCallFake(function (url) {
return url;
});
mockScope = jasmine.createSpyObj("scope", [ "$watch", "$on" ]); mockScope = jasmine.createSpyObj("scope", [ "$watch", "$on" ]);
mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS); mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS);
mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS); mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS);
mockDomainObject.getModel.andReturn(testModel); mockDomainObject.getModel.andReturn(testModel);
mockLinker.link.andReturn(mockChangeTemplate);
mctRepresentation = new MCTRepresentation( mctRepresentation = new MCTRepresentation(
testRepresentations, testRepresentations,
testViews, testViews,
mockRepresenters, mockRepresenters,
mockQ, mockQ,
mockSce, mockLinker,
mockLog mockLog
); );
}); mctRepresentation.link(mockScope, mockElement);
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);
}); });
it("is restricted to elements", function () { it("is restricted to elements", function () {
expect(mctRepresentation.restrict).toEqual("E"); expect(mctRepresentation.restrict).toEqual("E");
}); });
it("exposes templates via the templateLinker", function () {
expect(mockLinker.link)
.toHaveBeenCalledWith(mockScope, mockElement);
});
it("watches scope when linked", function () { it("watches scope when linked", function () {
mctRepresentation.link(mockScope, mockElement);
expect(mockScope.$watch).toHaveBeenCalledWith( expect(mockScope.$watch).toHaveBeenCalledWith(
"key", "key",
jasmine.any(Function) jasmine.any(Function)
@ -150,42 +152,46 @@ define(
}); });
it("recognizes keys for representations", function () { it("recognizes keys for representations", function () {
mctRepresentation.link(mockScope, mockElement);
mockScope.key = "abc"; mockScope.key = "abc";
mockScope.domainObject = mockDomainObject;
// Trigger the watch // 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 () { it("recognizes keys for views", function () {
mctRepresentation.link(mockScope, mockElement);
mockScope.key = "xyz"; mockScope.key = "xyz";
mockScope.domainObject = mockDomainObject;
// Trigger the watch // Trigger the watches
mockScope.$watch.calls[0].args[1](); fireWatch('key', mockScope.key);
fireWatch('domainObject', mockDomainObject);
expect(mockScope.inclusion).toEqual("x/y/z/template.html"); expect(mockChangeTemplate)
});
it("trusts template URLs", function () {
mctRepresentation.link(mockScope, mockElement);
mockScope.key = "xyz";
// Trigger the watch
mockScope.$watch.calls[0].args[1]();
expect(mockSce.trustAsResourceUrl)
.toHaveBeenCalledWith("x/y/z/template.html"); .toHaveBeenCalledWith("x/y/z/template.html");
}); });
it("loads declared capabilities", function () { it("does not load templates until there is an object", function () {
mctRepresentation.link(mockScope, mockElement); 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.key = "def";
mockScope.domainObject = mockDomainObject; mockScope.domainObject = mockDomainObject;
@ -199,8 +205,6 @@ define(
}); });
it("logs when no representation is available for a key", function () { it("logs when no representation is available for a key", function () {
mctRepresentation.link(mockScope, mockElement);
mockScope.key = "someUnknownThing"; mockScope.key = "someUnknownThing";
// Verify precondition // Verify precondition
@ -214,8 +218,6 @@ define(
}); });
it("clears out obsolete peroperties from scope", function () { it("clears out obsolete peroperties from scope", function () {
mctRepresentation.link(mockScope, mockElement);
mockScope.key = "def"; mockScope.key = "def";
mockScope.domainObject = mockDomainObject; mockScope.domainObject = mockDomainObject;
mockDomainObject.useCapability.andReturn("some value"); mockDomainObject.useCapability.andReturn("some value");