[Representation] Update spec for mct-include

...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:11:00 -07:00
parent ae0cb63a92
commit 01b6fda1f2

View File

@ -31,9 +31,20 @@ define(
describe("The mct-include directive", function () { describe("The mct-include directive", function () {
var testTemplates, var testTemplates,
mockSce, mockLinker,
mockScope,
mockElement,
mockChangeTemplate,
mctInclude; mctInclude;
function fireWatch(expr, value) {
mockScope.$watch.calls.forEach(function (call) {
if (call.args[0] === expr) {
call.args[1](value);
}
});
}
beforeEach(function () { beforeEach(function () {
testTemplates = [ testTemplates = [
{ {
@ -47,42 +58,37 @@ define(
templateUrl: "z/template.html" templateUrl: "z/template.html"
} }
]; ];
mockSce = jasmine.createSpyObj( mockLinker = jasmine.createSpyObj('templateLinker', ['link']);
'$sce', mockScope = jasmine.createSpyObj('$scope', ['$watch', '$on']);
['trustAsResourceUrl'] mockElement = jasmine.createSpyObj('element', ['empty']);
); mockChangeTemplate = jasmine.createSpy('changeTemplate');
mockSce.trustAsResourceUrl.andCallFake(function (url) { mockLinker.link.andReturn(mockChangeTemplate);
return url; mctInclude = new MCTInclude(testTemplates, mockLinker);
}); mctInclude.link(mockScope, mockElement, {});
mctInclude = new MCTInclude(testTemplates, mockSce);
});
it("has a built-in template, with ng-include src=inclusion", function () {
// Not rigorous, but should detect many cases when template is broken.
expect(mctInclude.template.indexOf("ng-include")).not.toEqual(-1);
expect(mctInclude.template.indexOf("inclusion")).not.toEqual(-1);
}); });
it("is restricted to elements", function () { it("is restricted to elements", function () {
expect(mctInclude.restrict).toEqual("E"); expect(mctInclude.restrict).toEqual("E");
}); });
it("reads a template location from a scope's key variable", function () { it("exposes templates via the templateLinker", function () {
var scope = { key: "abc" }; expect(mockLinker.link)
mctInclude.controller(scope); .toHaveBeenCalledWith(mockScope, mockElement);
expect(scope.inclusion).toEqual("a/b/c/template.html");
scope = { key: "xyz" };
mctInclude.controller(scope);
expect(scope.inclusion).toEqual("x/y/z/template.html");
}); });
it("trusts template URLs", function () { it("reads a template location from a scope's key variable", function () {
mctInclude.controller({ key: "xyz" }); mockScope.key = 'abc';
expect(mockSce.trustAsResourceUrl) fireWatch('key', mockScope.key);
expect(mockChangeTemplate)
.toHaveBeenCalledWith("a/b/c/template.html");
mockScope.key = 'xyz';
fireWatch('key', mockScope.key);
expect(mockChangeTemplate)
.toHaveBeenCalledWith("x/y/z/template.html"); .toHaveBeenCalledWith("x/y/z/template.html");
}); });
}); });
} }
); );