2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "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.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
2014-11-22 17:36:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MCTRepresentationSpec. Created by vwoeltje on 11/6/14.
|
|
|
|
*/
|
|
|
|
define(
|
|
|
|
["../src/MCTRepresentation"],
|
|
|
|
function (MCTRepresentation) {
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
var JQLITE_FUNCTIONS = ["on", "off", "attr", "removeAttr"],
|
|
|
|
LOG_FUNCTIONS = ["error", "warn", "info", "debug"],
|
|
|
|
DOMAIN_OBJECT_METHODS = ["getId", "getModel", "getCapability", "hasCapability", "useCapability"];
|
2014-11-22 17:36:30 +00:00
|
|
|
|
2014-11-22 19:35:57 +00:00
|
|
|
describe("The mct-representation directive", function () {
|
|
|
|
var testRepresentations,
|
|
|
|
testViews,
|
2015-10-30 18:02:13 +00:00
|
|
|
testUrls,
|
2014-12-05 23:48:13 +00:00
|
|
|
mockRepresenters,
|
2016-05-12 23:14:31 +00:00
|
|
|
mockStatusCapability,
|
2014-11-22 19:35:57 +00:00
|
|
|
mockQ,
|
2015-10-28 23:35:08 +00:00
|
|
|
mockLinker,
|
2014-11-22 19:35:57 +00:00
|
|
|
mockLog,
|
2015-10-28 23:35:08 +00:00
|
|
|
mockChangeTemplate,
|
2014-11-22 19:35:57 +00:00
|
|
|
mockScope,
|
|
|
|
mockElement,
|
|
|
|
mockDomainObject,
|
2014-12-05 23:48:13 +00:00
|
|
|
testModel,
|
2014-11-22 19:35:57 +00:00
|
|
|
mctRepresentation;
|
|
|
|
|
|
|
|
function mockPromise(value) {
|
|
|
|
return (value && value.then) ? value : {
|
|
|
|
then: function (callback) {
|
|
|
|
return mockPromise(callback(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-10-28 23:35:08 +00:00
|
|
|
function fireWatch(expr, value) {
|
|
|
|
mockScope.$watch.calls.forEach(function (call) {
|
|
|
|
if (call.args[0] === expr) {
|
|
|
|
call.args[1](value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-22 19:35:57 +00:00
|
|
|
beforeEach(function () {
|
2015-10-30 18:02:13 +00:00
|
|
|
testUrls = {};
|
|
|
|
|
2014-11-22 19:35:57 +00:00
|
|
|
testRepresentations = [
|
|
|
|
{
|
|
|
|
key: "abc",
|
|
|
|
bundle: { path: "a", resources: "b" },
|
|
|
|
templateUrl: "c/template.html"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "def",
|
|
|
|
bundle: { path: "d", resources: "e" },
|
|
|
|
templateUrl: "f/template.html",
|
2016-05-19 18:29:13 +00:00
|
|
|
uses: ["testCapability", "otherTestCapability"]
|
2014-11-22 19:35:57 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
testViews = [
|
|
|
|
{
|
|
|
|
key: "uvw",
|
|
|
|
bundle: { path: "u", resources: "v" },
|
|
|
|
templateUrl: "w/template.html",
|
2016-05-19 18:29:13 +00:00
|
|
|
gestures: ["testGesture", "otherTestGesture"]
|
2014-11-22 19:35:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "xyz",
|
|
|
|
bundle: { path: "x", resources: "y" },
|
|
|
|
templateUrl: "z/template.html"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2014-12-05 23:48:13 +00:00
|
|
|
testModel = { someKey: "some value" };
|
2014-11-22 19:35:57 +00:00
|
|
|
|
2015-10-30 18:02:13 +00:00
|
|
|
testUrls = {};
|
|
|
|
testViews.concat(testRepresentations).forEach(function (t, i) {
|
|
|
|
testUrls[t.key] = "some URL " + String(i);
|
|
|
|
});
|
|
|
|
|
2014-12-05 23:48:13 +00:00
|
|
|
mockRepresenters = ["A", "B"].map(function (name) {
|
|
|
|
var constructor = jasmine.createSpy("Representer" + name),
|
|
|
|
representer = jasmine.createSpyObj(
|
|
|
|
"representer" + name,
|
2016-05-19 18:29:13 +00:00
|
|
|
["represent", "destroy"]
|
2014-12-05 23:48:13 +00:00
|
|
|
);
|
|
|
|
constructor.andReturn(representer);
|
|
|
|
return constructor;
|
|
|
|
});
|
2014-11-22 19:35:57 +00:00
|
|
|
|
|
|
|
mockQ = { when: mockPromise };
|
2015-10-30 18:02:13 +00:00
|
|
|
mockLinker = jasmine.createSpyObj(
|
|
|
|
'templateLinker',
|
|
|
|
['link', 'getPath']
|
|
|
|
);
|
2015-10-28 23:35:08 +00:00
|
|
|
mockChangeTemplate = jasmine.createSpy('changeTemplate');
|
2014-11-22 19:35:57 +00:00
|
|
|
mockLog = jasmine.createSpyObj("$log", LOG_FUNCTIONS);
|
|
|
|
|
2016-05-12 23:14:31 +00:00
|
|
|
mockStatusCapability = jasmine.createSpyObj("statusCapability", ["listen"]);
|
|
|
|
|
2016-05-19 18:29:13 +00:00
|
|
|
mockScope = jasmine.createSpyObj("scope", ["$watch", "$on"]);
|
2014-11-22 19:35:57 +00:00
|
|
|
mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS);
|
|
|
|
mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS);
|
|
|
|
|
2014-12-05 23:48:13 +00:00
|
|
|
mockDomainObject.getModel.andReturn(testModel);
|
2015-10-28 23:35:08 +00:00
|
|
|
mockLinker.link.andReturn(mockChangeTemplate);
|
2015-10-30 18:02:13 +00:00
|
|
|
mockLinker.getPath.andCallFake(function (ext) {
|
|
|
|
return testUrls[ext.key];
|
|
|
|
});
|
2014-12-05 23:48:13 +00:00
|
|
|
|
2016-05-12 23:14:31 +00:00
|
|
|
mockDomainObject.getCapability.andCallFake(function (c) {
|
|
|
|
return c === 'status' && mockStatusCapability;
|
|
|
|
});
|
|
|
|
|
2014-11-22 19:35:57 +00:00
|
|
|
mctRepresentation = new MCTRepresentation(
|
|
|
|
testRepresentations,
|
|
|
|
testViews,
|
2014-12-05 23:48:13 +00:00
|
|
|
mockRepresenters,
|
2014-11-22 19:35:57 +00:00
|
|
|
mockQ,
|
2015-10-28 23:35:08 +00:00
|
|
|
mockLinker,
|
2014-11-22 19:35:57 +00:00
|
|
|
mockLog
|
|
|
|
);
|
2015-10-28 23:35:08 +00:00
|
|
|
mctRepresentation.link(mockScope, mockElement);
|
2014-11-22 19:35:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("is restricted to elements", function () {
|
|
|
|
expect(mctRepresentation.restrict).toEqual("E");
|
|
|
|
});
|
|
|
|
|
2015-10-28 23:35:08 +00:00
|
|
|
it("exposes templates via the templateLinker", function () {
|
|
|
|
expect(mockLinker.link)
|
|
|
|
.toHaveBeenCalledWith(mockScope, mockElement);
|
|
|
|
});
|
|
|
|
|
2014-11-22 19:35:57 +00:00
|
|
|
it("watches scope when linked", function () {
|
2015-06-18 21:43:14 +00:00
|
|
|
expect(mockScope.$watch).toHaveBeenCalledWith(
|
|
|
|
"key",
|
|
|
|
jasmine.any(Function)
|
|
|
|
);
|
|
|
|
expect(mockScope.$watch).toHaveBeenCalledWith(
|
|
|
|
"domainObject",
|
|
|
|
jasmine.any(Function)
|
|
|
|
);
|
|
|
|
expect(mockScope.$watch).toHaveBeenCalledWith(
|
|
|
|
"domainObject.getModel().modified",
|
|
|
|
jasmine.any(Function)
|
|
|
|
);
|
2014-11-22 19:35:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("recognizes keys for representations", function () {
|
|
|
|
mockScope.key = "abc";
|
2015-10-28 23:35:08 +00:00
|
|
|
mockScope.domainObject = mockDomainObject;
|
2014-11-22 19:35:57 +00:00
|
|
|
|
|
|
|
// Trigger the watch
|
2015-10-28 23:35:08 +00:00
|
|
|
fireWatch('key', mockScope.key);
|
|
|
|
fireWatch('domainObject', mockDomainObject);
|
2014-11-22 19:35:57 +00:00
|
|
|
|
2015-10-28 23:35:08 +00:00
|
|
|
expect(mockChangeTemplate)
|
2016-01-29 19:27:08 +00:00
|
|
|
.toHaveBeenCalledWith(testRepresentations[0]);
|
2014-11-22 19:35:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("recognizes keys for views", function () {
|
|
|
|
mockScope.key = "xyz";
|
2015-10-28 23:35:08 +00:00
|
|
|
mockScope.domainObject = mockDomainObject;
|
2014-11-22 19:35:57 +00:00
|
|
|
|
2015-10-28 23:35:08 +00:00
|
|
|
// Trigger the watches
|
|
|
|
fireWatch('key', mockScope.key);
|
|
|
|
fireWatch('domainObject', mockDomainObject);
|
2014-11-22 19:35:57 +00:00
|
|
|
|
2015-10-28 23:35:08 +00:00
|
|
|
expect(mockChangeTemplate)
|
2016-01-29 19:27:08 +00:00
|
|
|
.toHaveBeenCalledWith(testViews[1]);
|
2014-11-22 19:35:57 +00:00
|
|
|
});
|
|
|
|
|
2015-10-28 23:35:08 +00:00
|
|
|
it("does not load templates until there is an object", function () {
|
2015-06-18 21:43:14 +00:00
|
|
|
mockScope.key = "xyz";
|
|
|
|
|
|
|
|
// Trigger the watch
|
2015-10-28 23:35:08 +00:00
|
|
|
fireWatch('key', mockScope.key);
|
2015-06-18 21:43:14 +00:00
|
|
|
|
2015-10-28 23:35:08 +00:00
|
|
|
expect(mockChangeTemplate)
|
2016-01-29 19:27:08 +00:00
|
|
|
.not.toHaveBeenCalledWith(jasmine.any(Object));
|
2015-10-28 23:35:08 +00:00
|
|
|
|
|
|
|
mockScope.domainObject = mockDomainObject;
|
|
|
|
fireWatch('domainObject', mockDomainObject);
|
|
|
|
|
|
|
|
expect(mockChangeTemplate)
|
2016-01-29 19:27:08 +00:00
|
|
|
.toHaveBeenCalledWith(jasmine.any(Object));
|
2015-06-18 21:43:14 +00:00
|
|
|
});
|
|
|
|
|
2014-11-22 19:35:57 +00:00
|
|
|
it("loads declared capabilities", function () {
|
|
|
|
mockScope.key = "def";
|
|
|
|
mockScope.domainObject = mockDomainObject;
|
|
|
|
|
|
|
|
// Trigger the watch
|
2015-01-27 18:00:06 +00:00
|
|
|
mockScope.$watch.calls[0].args[1]();
|
2014-11-22 19:35:57 +00:00
|
|
|
|
|
|
|
expect(mockDomainObject.useCapability)
|
|
|
|
.toHaveBeenCalledWith("testCapability");
|
|
|
|
expect(mockDomainObject.useCapability)
|
|
|
|
.toHaveBeenCalledWith("otherTestCapability");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("logs when no representation is available for a key", function () {
|
|
|
|
mockScope.key = "someUnknownThing";
|
|
|
|
|
|
|
|
// Verify precondition
|
|
|
|
expect(mockLog.warn).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Trigger the watch
|
2015-01-27 18:00:06 +00:00
|
|
|
mockScope.$watch.calls[0].args[1]();
|
2014-11-22 19:35:57 +00:00
|
|
|
|
|
|
|
// Should have gotten a warning - that's an unknown key
|
|
|
|
expect(mockLog.warn).toHaveBeenCalled();
|
|
|
|
});
|
2015-05-20 23:39:49 +00:00
|
|
|
|
2016-05-12 21:20:16 +00:00
|
|
|
it("clears out obsolete properties from scope", function () {
|
2015-05-20 23:39:49 +00:00
|
|
|
mockScope.key = "def";
|
|
|
|
mockScope.domainObject = mockDomainObject;
|
|
|
|
mockDomainObject.useCapability.andReturn("some value");
|
|
|
|
|
|
|
|
// Trigger the watch
|
|
|
|
mockScope.$watch.calls[0].args[1]();
|
|
|
|
expect(mockScope.testCapability).toBeDefined();
|
|
|
|
|
|
|
|
// Change the view
|
|
|
|
mockScope.key = "xyz";
|
|
|
|
|
|
|
|
// Trigger the watch again; should clear capability from scope
|
|
|
|
mockScope.$watch.calls[0].args[1]();
|
|
|
|
expect(mockScope.testCapability).toBeUndefined();
|
|
|
|
});
|
2015-12-02 18:49:18 +00:00
|
|
|
|
2016-03-10 22:26:20 +00:00
|
|
|
describe("when a domain object has been observed", function () {
|
|
|
|
var mockContext,
|
|
|
|
mockContext2,
|
|
|
|
mockLink,
|
|
|
|
mockParent;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockContext = jasmine.createSpyObj('context', ['getPath']);
|
|
|
|
mockContext2 = jasmine.createSpyObj('context', ['getPath']);
|
2015-12-02 18:49:18 +00:00
|
|
|
mockLink = jasmine.createSpyObj(
|
|
|
|
'linkedObject',
|
|
|
|
DOMAIN_OBJECT_METHODS
|
2016-03-10 22:26:20 +00:00
|
|
|
);
|
2015-12-02 18:49:18 +00:00
|
|
|
mockParent = jasmine.createSpyObj(
|
|
|
|
'parentObject',
|
|
|
|
DOMAIN_OBJECT_METHODS
|
2016-03-10 22:26:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
mockDomainObject.getCapability.andCallFake(function (c) {
|
|
|
|
return c === 'context' && mockContext;
|
|
|
|
});
|
|
|
|
mockLink.getCapability.andCallFake(function (c) {
|
|
|
|
return c === 'context' && mockContext2;
|
|
|
|
});
|
|
|
|
mockDomainObject.hasCapability.andCallFake(function (c) {
|
|
|
|
return c === 'context';
|
|
|
|
});
|
|
|
|
mockLink.hasCapability.andCallFake(function (c) {
|
|
|
|
return c === 'context';
|
|
|
|
});
|
|
|
|
mockLink.getModel.andReturn({});
|
|
|
|
|
|
|
|
mockContext.getPath.andReturn([mockDomainObject]);
|
|
|
|
mockContext2.getPath.andReturn([mockParent, mockLink]);
|
|
|
|
|
|
|
|
mockLink.getId.andReturn('test-id');
|
|
|
|
mockDomainObject.getId.andReturn('test-id');
|
|
|
|
|
|
|
|
mockParent.getId.andReturn('parent-id');
|
|
|
|
|
|
|
|
mockScope.key = "abc";
|
|
|
|
mockScope.domainObject = mockDomainObject;
|
|
|
|
|
|
|
|
mockScope.$watch.calls[0].args[1]();
|
2015-12-02 18:49:18 +00:00
|
|
|
});
|
|
|
|
|
2016-03-10 22:26:20 +00:00
|
|
|
it("detects subsequent changes among linked instances", function () {
|
|
|
|
var callCount = mockChangeTemplate.calls.length;
|
2015-12-02 18:49:18 +00:00
|
|
|
|
2016-03-10 22:26:20 +00:00
|
|
|
mockScope.domainObject = mockLink;
|
|
|
|
mockScope.$watch.calls[0].args[1]();
|
2015-12-02 18:49:18 +00:00
|
|
|
|
2016-03-10 22:26:20 +00:00
|
|
|
expect(mockChangeTemplate.calls.length)
|
|
|
|
.toEqual(callCount + 1);
|
|
|
|
});
|
2015-12-02 18:49:18 +00:00
|
|
|
|
2016-03-10 22:26:20 +00:00
|
|
|
it("does not trigger excess template changes for same instances", function () {
|
|
|
|
var callCount = mockChangeTemplate.calls.length;
|
|
|
|
mockScope.$watch.calls[0].args[1]();
|
|
|
|
expect(mockChangeTemplate.calls.length).toEqual(callCount);
|
|
|
|
});
|
2016-05-12 21:20:16 +00:00
|
|
|
|
2016-03-10 22:26:20 +00:00
|
|
|
});
|
2015-12-02 18:49:18 +00:00
|
|
|
|
|
|
|
|
2014-11-22 17:36:30 +00:00
|
|
|
});
|
|
|
|
}
|
2015-06-18 21:43:14 +00:00
|
|
|
);
|