2015-10-28 23:49:19 +00:00
|
|
|
/*****************************************************************************
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2016, United States Government
|
2015-10-28 23:49:19 +00:00
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
2015-10-28 23:49:19 +00:00
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT includes source code licensed under additional open source
|
2015-10-28 23:49:19 +00:00
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
define(
|
|
|
|
["../src/TemplateLinker"],
|
|
|
|
function (TemplateLinker) {
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
var JQLITE_METHODS = ['replaceWith', 'empty', 'html', 'contents'],
|
|
|
|
SCOPE_METHODS = ['$on', '$new', '$destroy'];
|
2015-10-28 23:49:19 +00:00
|
|
|
|
|
|
|
describe("TemplateLinker", function () {
|
2015-10-30 19:22:43 +00:00
|
|
|
var mockTemplateRequest,
|
|
|
|
mockSce,
|
2015-10-28 23:49:19 +00:00
|
|
|
mockCompile,
|
|
|
|
mockLog,
|
|
|
|
mockScope,
|
|
|
|
mockElement,
|
|
|
|
mockTemplates,
|
|
|
|
mockElements,
|
2015-10-30 23:37:47 +00:00
|
|
|
mockContents,
|
2015-10-30 23:01:24 +00:00
|
|
|
mockNewScope,
|
2015-10-30 19:22:43 +00:00
|
|
|
mockPromise,
|
2015-10-28 23:49:19 +00:00
|
|
|
linker;
|
|
|
|
|
2016-01-29 19:42:14 +00:00
|
|
|
function testExtension(path, res, templatePath) {
|
|
|
|
return {
|
|
|
|
bundle: {
|
|
|
|
path: path,
|
|
|
|
resources: res
|
|
|
|
},
|
|
|
|
templateUrl: templatePath
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-10-28 23:49:19 +00:00
|
|
|
beforeEach(function () {
|
2015-10-30 19:22:43 +00:00
|
|
|
mockTemplateRequest = jasmine.createSpy('$templateRequest');
|
|
|
|
mockSce = jasmine.createSpyObj('$sce', ['trustAsResourceUrl']);
|
2015-10-28 23:49:19 +00:00
|
|
|
mockCompile = jasmine.createSpy('$compile');
|
|
|
|
mockLog = jasmine.createSpyObj('$log', ['error', 'warn']);
|
2015-10-30 23:01:24 +00:00
|
|
|
mockScope = jasmine.createSpyObj('$scope', SCOPE_METHODS);
|
|
|
|
mockNewScope = jasmine.createSpyObj('$scope', SCOPE_METHODS);
|
2015-10-28 23:49:19 +00:00
|
|
|
mockElement = jasmine.createSpyObj('element', JQLITE_METHODS);
|
2015-10-30 19:22:43 +00:00
|
|
|
mockPromise = jasmine.createSpyObj('promise', ['then']);
|
2015-10-28 23:49:19 +00:00
|
|
|
mockTemplates = {};
|
|
|
|
mockElements = {};
|
2015-10-30 23:37:47 +00:00
|
|
|
mockContents = {};
|
2015-10-28 23:49:19 +00:00
|
|
|
|
2015-10-30 19:22:43 +00:00
|
|
|
mockTemplateRequest.andReturn(mockPromise);
|
2015-10-30 23:37:47 +00:00
|
|
|
mockCompile.andCallFake(function (toCompile) {
|
|
|
|
var html = typeof toCompile === 'string' ?
|
|
|
|
toCompile : toCompile.testHtml;
|
2015-10-28 23:49:19 +00:00
|
|
|
mockTemplates[html] = jasmine.createSpy('template');
|
|
|
|
mockElements[html] =
|
|
|
|
jasmine.createSpyObj('templateEl', JQLITE_METHODS);
|
|
|
|
mockTemplates[html].andReturn(mockElements[html]);
|
|
|
|
return mockTemplates[html];
|
|
|
|
});
|
2015-10-30 19:22:43 +00:00
|
|
|
mockSce.trustAsResourceUrl.andCallFake(function (url) {
|
|
|
|
return { trusted: url };
|
|
|
|
});
|
2015-10-30 23:01:24 +00:00
|
|
|
mockScope.$new.andReturn(mockNewScope);
|
2015-10-30 23:37:47 +00:00
|
|
|
mockElement.html.andCallFake(function (html) {
|
|
|
|
mockContents[html] =
|
|
|
|
jasmine.createSpyObj('contentsEl', JQLITE_METHODS);
|
|
|
|
mockContents[html].testHtml = html;
|
|
|
|
});
|
|
|
|
mockElement.contents.andCallFake(function () {
|
|
|
|
return mockContents[
|
|
|
|
mockElement.html.mostRecentCall.args[0]
|
|
|
|
];
|
|
|
|
});
|
2015-10-28 23:49:19 +00:00
|
|
|
|
|
|
|
linker = new TemplateLinker(
|
2015-10-30 19:22:43 +00:00
|
|
|
mockTemplateRequest,
|
|
|
|
mockSce,
|
2015-10-28 23:49:19 +00:00
|
|
|
mockCompile,
|
|
|
|
mockLog
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-10-30 18:02:13 +00:00
|
|
|
it("resolves extension paths", function () {
|
2016-01-29 19:42:14 +00:00
|
|
|
var testExt = testExtension('a', 'b', 'c/d.html');
|
|
|
|
expect(linker.getPath(testExt)).toEqual('a/b/c/d.html');
|
2015-10-30 18:02:13 +00:00
|
|
|
});
|
|
|
|
|
2015-10-28 23:49:19 +00:00
|
|
|
describe("when linking elements", function () {
|
|
|
|
var changeTemplate,
|
|
|
|
commentElement;
|
|
|
|
|
|
|
|
function findCommentElement() {
|
|
|
|
mockCompile.calls.forEach(function (call) {
|
|
|
|
var html = call.args[0];
|
|
|
|
if (html.indexOf("<!--") > -1) {
|
|
|
|
commentElement = mockElements[html];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
changeTemplate = linker.link(mockScope, mockElement);
|
|
|
|
findCommentElement();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("compiles a comment to use to replace element", function () {
|
|
|
|
expect(commentElement).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("initially replaces elements with comments", function () {
|
|
|
|
expect(mockElement.replaceWith)
|
|
|
|
.toHaveBeenCalledWith(commentElement);
|
|
|
|
});
|
2015-10-29 00:01:13 +00:00
|
|
|
|
|
|
|
it("provides a function to change templates", function () {
|
|
|
|
expect(changeTemplate).toEqual(jasmine.any(Function));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and then changing templates", function () {
|
2016-01-29 19:42:14 +00:00
|
|
|
var testExt,
|
|
|
|
testUrl,
|
2015-10-29 00:01:13 +00:00
|
|
|
testTemplate;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2016-01-29 19:42:14 +00:00
|
|
|
testExt = testExtension('some', 'url', 'template.html');
|
|
|
|
testUrl = linker.getPath(testExt);
|
2015-10-29 00:01:13 +00:00
|
|
|
testTemplate = "<div>Some template!</div>";
|
2016-01-29 19:42:14 +00:00
|
|
|
changeTemplate(testExt);
|
2015-10-30 19:22:43 +00:00
|
|
|
mockPromise.then.mostRecentCall
|
|
|
|
.args[0](testTemplate);
|
2015-10-29 00:01:13 +00:00
|
|
|
});
|
|
|
|
|
2015-10-30 19:22:43 +00:00
|
|
|
it("loads templates using $templateRequest", function () {
|
|
|
|
expect(mockTemplateRequest).toHaveBeenCalledWith({
|
|
|
|
trusted: testUrl
|
|
|
|
}, false);
|
2015-10-29 00:01:13 +00:00
|
|
|
});
|
|
|
|
|
2015-10-30 23:37:47 +00:00
|
|
|
it("compiles element contents with a new scope", function () {
|
|
|
|
expect(mockCompile)
|
|
|
|
.toHaveBeenCalledWith(mockContents[testTemplate]);
|
2015-10-30 19:22:43 +00:00
|
|
|
expect(mockTemplates[testTemplate])
|
2015-10-30 23:01:24 +00:00
|
|
|
.toHaveBeenCalledWith(mockNewScope);
|
2015-10-29 00:01:13 +00:00
|
|
|
});
|
|
|
|
|
2015-10-29 15:08:01 +00:00
|
|
|
it("replaces comments with specified element", function () {
|
2015-10-29 00:01:13 +00:00
|
|
|
expect(commentElement.replaceWith)
|
|
|
|
.toHaveBeenCalledWith(mockElement);
|
|
|
|
});
|
2015-10-29 15:08:01 +00:00
|
|
|
|
2015-10-30 23:37:47 +00:00
|
|
|
it("inserts HTML content into the specified element", function () {
|
|
|
|
expect(mockElement.html)
|
|
|
|
.toHaveBeenCalledWith(testTemplate);
|
2015-10-29 15:08:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("clears templates when called with undefined", function () {
|
|
|
|
expect(mockElement.replaceWith.callCount)
|
|
|
|
.toEqual(1);
|
|
|
|
changeTemplate(undefined);
|
|
|
|
expect(mockElement.replaceWith.callCount)
|
|
|
|
.toEqual(2);
|
|
|
|
expect(mockElement.replaceWith.mostRecentCall.args[0])
|
|
|
|
.toEqual(commentElement);
|
|
|
|
});
|
|
|
|
|
2015-10-30 19:22:43 +00:00
|
|
|
it("logs no warnings for nominal changes", function () {
|
2015-10-29 15:08:01 +00:00
|
|
|
expect(mockLog.warn).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("which cannot be found", function () {
|
|
|
|
beforeEach(function () {
|
2016-01-29 19:42:14 +00:00
|
|
|
changeTemplate(
|
|
|
|
testExtension("some", "bad", "template.html")
|
|
|
|
);
|
2015-10-30 19:22:43 +00:00
|
|
|
// Reject the template promise
|
|
|
|
mockPromise.then.mostRecentCall.args[1]();
|
2015-10-29 15:08:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("removes the element from the DOM", function () {
|
|
|
|
expect(mockElement.replaceWith.callCount)
|
|
|
|
.toEqual(2);
|
|
|
|
expect(
|
|
|
|
mockElement.replaceWith.mostRecentCall.args[0]
|
|
|
|
).toEqual(commentElement);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("logs a warning", function () {
|
|
|
|
expect(mockLog.warn)
|
|
|
|
.toHaveBeenCalledWith(jasmine.any(String));
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2015-10-29 00:01:13 +00:00
|
|
|
});
|
|
|
|
|
2015-10-28 23:49:19 +00:00
|
|
|
});
|
2015-10-29 15:27:20 +00:00
|
|
|
|
|
|
|
describe("when an initial template URL is provided", function () {
|
2016-01-29 19:42:14 +00:00
|
|
|
var testExt,
|
|
|
|
testUrl;
|
2015-10-29 15:27:20 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2016-01-29 19:42:14 +00:00
|
|
|
testExt = testExtension('some', 'test', 'template.html');
|
|
|
|
testUrl = linker.getPath(testExt);
|
|
|
|
linker.link(mockScope, mockElement, testExt);
|
2015-10-29 15:27:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does not remove the element initially", function () {
|
|
|
|
expect(mockElement.replaceWith)
|
|
|
|
.not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("loads the specified template", function () {
|
2015-10-30 19:22:43 +00:00
|
|
|
expect(mockTemplateRequest).toHaveBeenCalledWith({
|
|
|
|
trusted: testUrl
|
|
|
|
}, false);
|
2015-10-29 15:27:20 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-28 23:49:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|