[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,
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");