2015-09-15 17:00:41 +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.
|
|
|
|
*****************************************************************************/
|
|
|
|
/*global define,Promise,describe,it,expect,beforeEach,waitsFor,jasmine*/
|
|
|
|
|
|
|
|
define(
|
2015-10-21 22:38:58 +00:00
|
|
|
["../../src/controllers/TimeRangeController", "moment"],
|
|
|
|
function (TimeRangeController, moment) {
|
2015-09-15 17:00:41 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-10-09 14:58:10 +00:00
|
|
|
var SEC = 1000,
|
|
|
|
MIN = 60 * SEC,
|
|
|
|
HOUR = 60 * MIN,
|
|
|
|
DAY = 24 * HOUR;
|
|
|
|
|
2015-09-15 17:00:41 +00:00
|
|
|
describe("The TimeRangeController", function () {
|
|
|
|
var mockScope,
|
2015-10-27 21:05:55 +00:00
|
|
|
mockFormatService,
|
2015-10-30 20:53:33 +00:00
|
|
|
testDefaultFormat,
|
2015-09-15 17:00:41 +00:00
|
|
|
mockNow,
|
2015-10-27 21:05:55 +00:00
|
|
|
mockFormat,
|
2015-09-15 17:00:41 +00:00
|
|
|
controller;
|
|
|
|
|
|
|
|
function fireWatch(expr, value) {
|
|
|
|
mockScope.$watch.calls.forEach(function (call) {
|
|
|
|
if (call.args[0] === expr) {
|
|
|
|
call.args[1](value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function fireWatchCollection(expr, value) {
|
|
|
|
mockScope.$watchCollection.calls.forEach(function (call) {
|
|
|
|
if (call.args[0] === expr) {
|
|
|
|
call.args[1](value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockScope = jasmine.createSpyObj(
|
|
|
|
"$scope",
|
|
|
|
[ "$apply", "$watch", "$watchCollection" ]
|
|
|
|
);
|
2015-10-27 21:05:55 +00:00
|
|
|
mockFormatService = jasmine.createSpyObj(
|
|
|
|
"formatService",
|
|
|
|
[ "getFormat" ]
|
|
|
|
);
|
2015-10-30 20:53:33 +00:00
|
|
|
testDefaultFormat = 'utc';
|
2015-10-27 21:05:55 +00:00
|
|
|
mockFormat = jasmine.createSpyObj(
|
|
|
|
"format",
|
2015-10-22 19:17:08 +00:00
|
|
|
[ "validate", "format", "parse" ]
|
|
|
|
);
|
2015-10-27 21:05:55 +00:00
|
|
|
|
|
|
|
mockFormatService.getFormat.andReturn(mockFormat);
|
|
|
|
|
|
|
|
mockFormat.format.andCallFake(function (value) {
|
2015-10-22 19:17:08 +00:00
|
|
|
return moment.utc(value).format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
});
|
2015-10-27 21:05:55 +00:00
|
|
|
|
2015-09-15 17:00:41 +00:00
|
|
|
mockNow = jasmine.createSpy('now');
|
2015-10-22 19:17:08 +00:00
|
|
|
|
|
|
|
controller = new TimeRangeController(
|
|
|
|
mockScope,
|
2015-10-27 21:05:55 +00:00
|
|
|
mockFormatService,
|
2015-10-30 20:53:33 +00:00
|
|
|
testDefaultFormat,
|
2015-10-22 19:17:08 +00:00
|
|
|
mockNow
|
|
|
|
);
|
2015-09-15 17:00:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("watches the model that was passed in", function () {
|
|
|
|
expect(mockScope.$watchCollection)
|
|
|
|
.toHaveBeenCalledWith("ngModel", jasmine.any(Function));
|
|
|
|
});
|
|
|
|
|
2016-01-12 00:41:20 +00:00
|
|
|
it("exposes start time validator", function () {
|
|
|
|
var testValue = 42000000;
|
2016-01-12 02:28:44 +00:00
|
|
|
mockScope.formModel = { end: testValue };
|
2016-04-13 20:23:33 +00:00
|
|
|
expect(controller.validateStart(testValue + 1))
|
2016-01-12 00:41:20 +00:00
|
|
|
.toBe(false);
|
2016-04-13 20:23:33 +00:00
|
|
|
expect(controller.validateStart(testValue - 60 * 60 * 1000 - 1))
|
2016-01-12 00:41:20 +00:00
|
|
|
.toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("exposes end time validator", function () {
|
|
|
|
var testValue = 42000000;
|
2016-01-12 02:28:44 +00:00
|
|
|
mockScope.formModel = { start: testValue };
|
2016-04-13 20:23:33 +00:00
|
|
|
expect(controller.validateEnd(testValue - 1))
|
2016-01-12 00:41:20 +00:00
|
|
|
.toBe(false);
|
2016-04-13 20:23:33 +00:00
|
|
|
expect(controller.validateEnd(testValue + 60 * 60 * 1000 + 1))
|
2016-01-12 00:41:20 +00:00
|
|
|
.toBe(true);
|
|
|
|
});
|
|
|
|
|
2015-11-27 22:33:35 +00:00
|
|
|
describe("when changes are made via form entry", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
mockScope.ngModel = {
|
|
|
|
outer: { start: DAY * 2, end: DAY * 3 },
|
|
|
|
inner: { start: DAY * 2.25, end: DAY * 2.75 }
|
|
|
|
};
|
|
|
|
mockScope.formModel = {
|
|
|
|
start: DAY * 10000,
|
|
|
|
end: DAY * 11000
|
|
|
|
};
|
|
|
|
// These watches may not exist, but Angular would fire
|
|
|
|
// them if they did.
|
|
|
|
fireWatchCollection("formModel", mockScope.formModel);
|
|
|
|
fireWatch("formModel.start", mockScope.formModel.start);
|
|
|
|
fireWatch("formModel.end", mockScope.formModel.end);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not immediately make changes to the model", function () {
|
|
|
|
expect(mockScope.ngModel.outer.start)
|
|
|
|
.not.toEqual(mockScope.formModel.start);
|
|
|
|
expect(mockScope.ngModel.outer.end)
|
|
|
|
.not.toEqual(mockScope.formModel.end);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates model bounds on request", function () {
|
2016-04-13 20:23:33 +00:00
|
|
|
controller.updateBoundsFromForm();
|
2015-11-27 22:33:35 +00:00
|
|
|
expect(mockScope.ngModel.outer.start)
|
|
|
|
.toEqual(mockScope.formModel.start);
|
|
|
|
expect(mockScope.ngModel.outer.end)
|
|
|
|
.toEqual(mockScope.formModel.end);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-09 14:58:10 +00:00
|
|
|
describe("when dragged", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
mockScope.ngModel = {
|
|
|
|
outer: {
|
|
|
|
start: DAY * 1000,
|
|
|
|
end: DAY * 1001
|
|
|
|
},
|
|
|
|
inner: {
|
|
|
|
start: DAY * 1000 + HOUR * 3,
|
|
|
|
end: DAY * 1001 - HOUR * 3
|
|
|
|
}
|
|
|
|
};
|
|
|
|
mockScope.spanWidth = 1000;
|
|
|
|
fireWatch("spanWidth", mockScope.spanWidth);
|
|
|
|
fireWatchCollection("ngModel", mockScope.ngModel);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates the start time for left drags", function () {
|
2016-04-13 20:23:33 +00:00
|
|
|
controller.startLeftDrag();
|
|
|
|
controller.leftDrag(250);
|
2015-10-09 14:58:10 +00:00
|
|
|
expect(mockScope.ngModel.inner.start)
|
|
|
|
.toEqual(DAY * 1000 + HOUR * 9);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates the end time for right drags", function () {
|
2016-04-13 20:23:33 +00:00
|
|
|
controller.startRightDrag();
|
|
|
|
controller.rightDrag(-250);
|
2015-10-09 14:58:10 +00:00
|
|
|
expect(mockScope.ngModel.inner.end)
|
|
|
|
.toEqual(DAY * 1000 + HOUR * 15);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates both start and end for middle drags", function () {
|
2016-04-13 20:23:33 +00:00
|
|
|
controller.startMiddleDrag();
|
|
|
|
controller.middleDrag(-125);
|
2015-10-09 14:58:10 +00:00
|
|
|
expect(mockScope.ngModel.inner).toEqual({
|
|
|
|
start: DAY * 1000,
|
|
|
|
end: DAY * 1000 + HOUR * 18
|
|
|
|
});
|
2016-04-13 20:23:33 +00:00
|
|
|
controller.middleDrag(250);
|
2015-10-09 14:58:10 +00:00
|
|
|
expect(mockScope.ngModel.inner).toEqual({
|
|
|
|
start: DAY * 1000 + HOUR * 6,
|
|
|
|
end: DAY * 1001
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-09 16:27:59 +00:00
|
|
|
it("enforces a minimum inner span", function () {
|
2016-04-13 20:23:33 +00:00
|
|
|
controller.startRightDrag();
|
|
|
|
controller.rightDrag(-9999999);
|
2015-10-09 16:27:59 +00:00
|
|
|
expect(mockScope.ngModel.inner.end)
|
|
|
|
.toBeGreaterThan(mockScope.ngModel.inner.start);
|
|
|
|
});
|
2015-10-09 14:58:10 +00:00
|
|
|
});
|
|
|
|
|
2015-10-09 16:27:59 +00:00
|
|
|
describe("when outer bounds are changed", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
mockScope.ngModel = {
|
|
|
|
outer: {
|
|
|
|
start: DAY * 1000,
|
|
|
|
end: DAY * 1001
|
|
|
|
},
|
|
|
|
inner: {
|
|
|
|
start: DAY * 1000 + HOUR * 3,
|
|
|
|
end: DAY * 1001 - HOUR * 3
|
|
|
|
}
|
|
|
|
};
|
|
|
|
mockScope.spanWidth = 1000;
|
|
|
|
fireWatch("spanWidth", mockScope.spanWidth);
|
|
|
|
fireWatchCollection("ngModel", mockScope.ngModel);
|
|
|
|
});
|
2015-10-09 14:58:10 +00:00
|
|
|
|
2015-10-09 16:27:59 +00:00
|
|
|
it("enforces a minimum inner span when outer span changes", function () {
|
|
|
|
mockScope.ngModel.outer.end =
|
|
|
|
mockScope.ngModel.outer.start - DAY * 100;
|
|
|
|
fireWatch(
|
|
|
|
"ngModel.outer.end",
|
|
|
|
mockScope.ngModel.outer.end
|
|
|
|
);
|
|
|
|
expect(mockScope.ngModel.inner.end)
|
|
|
|
.toBeGreaterThan(mockScope.ngModel.inner.start);
|
|
|
|
});
|
2015-10-21 22:38:58 +00:00
|
|
|
|
2015-10-09 14:58:10 +00:00
|
|
|
});
|
2015-09-15 17:00:41 +00:00
|
|
|
|
2015-10-27 22:57:44 +00:00
|
|
|
it("watches for changes in format selection", function () {
|
|
|
|
expect(mockFormatService.getFormat)
|
|
|
|
.not.toHaveBeenCalledWith('test-format');
|
|
|
|
fireWatch("parameters.format", 'test-format');
|
|
|
|
expect(mockFormatService.getFormat)
|
|
|
|
.toHaveBeenCalledWith('test-format');
|
|
|
|
});
|
2015-10-21 22:38:58 +00:00
|
|
|
|
2015-10-30 20:53:33 +00:00
|
|
|
it("throws an error for unknown formats", function () {
|
|
|
|
mockFormatService.getFormat.andReturn(undefined);
|
|
|
|
expect(function () {
|
|
|
|
fireWatch("parameters.format", "some-format");
|
|
|
|
}).toThrow();
|
|
|
|
});
|
2015-10-21 22:38:58 +00:00
|
|
|
|
2015-09-15 17:00:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|