openmct/platform/features/imagery/test/controllers/ImageryControllerSpec.js
2017-06-01 18:34:26 -07:00

167 lines
6.7 KiB
JavaScript

/*****************************************************************************
* Open MCT, Copyright (c) 2014-2017, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT 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 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.
*****************************************************************************/
define(
["../../src/controllers/ImageryController"],
function (ImageryController) {
describe("The Imagery controller", function () {
var $scope,
openmct,
oldDomainObject,
newDomainObject,
unsubscribe,
callback,
metadata,
prefix,
controller;
beforeEach(function () {
$scope = jasmine.createSpyObj('$scope', ['$on', '$watch']);
oldDomainObject = jasmine.createSpyObj(
'domainObject',
['getId']
);
newDomainObject = { name: 'foo' };
oldDomainObject.getId.andReturn('testID');
openmct = {
objects: jasmine.createSpyObj('objectAPI', [
'get'
]),
time: jasmine.createSpyObj('timeAPI', [
'timeSystem'
]),
telemetry: jasmine.createSpyObj('telemetryAPI', [
'subscribe',
'getValueFormatter',
'getMetadata'
])
};
metadata = jasmine.createSpyObj('metadata', [
'value',
'valuesForHints'
]);
prefix = "formatted ";
unsubscribe = jasmine.createSpy('unsubscribe');
openmct.telemetry.subscribe.andReturn(unsubscribe);
openmct.time.timeSystem.andReturn({
key: 'testKey'
});
$scope.domainObject = oldDomainObject;
openmct.objects.get.andReturn(Promise.resolve(newDomainObject));
openmct.telemetry.getMetadata.andReturn(metadata);
openmct.telemetry.getValueFormatter.andCallFake(function (property) {
var formatter =
jasmine.createSpyObj("formatter-" + property, ['format']);
var isTime = (property === "timestamp");
formatter.format.andCallFake(function (datum) {
return (isTime ? prefix : "") + datum[property];
});
return formatter;
});
metadata.value.andReturn("timestamp");
metadata.valuesForHints.andReturn(["value"]);
controller = new ImageryController($scope, openmct);
waitsFor(function () {
return openmct.telemetry.subscribe.calls.length > 0;
}, 100);
runs(function () {
callback =
openmct.telemetry.subscribe.mostRecentCall.args[1];
});
});
it("subscribes to telemetry", function () {
expect(openmct.telemetry.subscribe).toHaveBeenCalledWith(
newDomainObject,
jasmine.any(Function)
);
});
it("unsubscribes when scope is destroyed", function () {
expect(unsubscribe).not.toHaveBeenCalled();
// Find the $destroy listener and call it
$scope.$on.calls.forEach(function (call) {
if (call.args[0] === '$destroy') {
call.args[1]();
}
});
expect(unsubscribe).toHaveBeenCalled();
});
it("exposes the latest telemetry values", function () {
// 06/18/2015 4:04am UTC
var testTimestamp = 1434600258123,
testUrl = "some/url",
nextTimestamp = 1434600259456, // 4:05.456
nextUrl = "some/other/url";
// Call back with telemetry data
callback({ timestamp: testTimestamp, value: testUrl });
expect(controller.getTime()).toEqual(prefix + testTimestamp);
expect(controller.getImageUrl()).toEqual(testUrl);
callback({ timestamp: nextTimestamp, value: nextUrl });
expect(controller.getTime()).toEqual(prefix + nextTimestamp);
expect(controller.getImageUrl()).toEqual(nextUrl);
});
it("allows updates to be paused", function () {
// 06/18/2015 4:04am UTC
var testTimestamp = 1434600258123,
testUrl = "some/url",
nextTimestamp = 1434600259456, // 4:05.456
nextUrl = "some/other/url";
// Call back with telemetry data
callback({ timestamp: testTimestamp, value: testUrl });
expect(controller.getTime()).toEqual(prefix + testTimestamp);
expect(controller.getImageUrl()).toEqual(testUrl);
expect(controller.paused()).toBeFalsy();
controller.paused(true); // Pause!
expect(controller.paused()).toBeTruthy();
callback({ timestamp: nextTimestamp, value: nextUrl });
expect(controller.getTime()).toEqual(prefix + testTimestamp);
expect(controller.getImageUrl()).toEqual(testUrl);
});
it("initially shows an empty string for date/time", function () {
// Do not invoke callback...
expect(controller.getTime()).toEqual("");
expect(controller.getImageUrl()).toEqual("");
});
});
}
);