2017-12-19 09:06:54 -06:00
|
|
|
import sinon from "sinon";
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
import * as dateMath from "app/core/utils/datemath";
|
|
|
|
import moment from "moment";
|
|
|
|
import _ from "lodash";
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2015-09-16 12:49:05 -05:00
|
|
|
describe("DateMath", () => {
|
2017-12-19 09:06:54 -06:00
|
|
|
var spans = ["s", "m", "h", "d", "w", "M", "y"];
|
|
|
|
var anchor = "2014-01-01T06:06:06.666Z";
|
2015-09-16 11:48:41 -05:00
|
|
|
var unix = moment(anchor).valueOf();
|
2017-12-19 09:06:54 -06:00
|
|
|
var format = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
2015-09-16 11:48:41 -05:00
|
|
|
var clock;
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("errors", () => {
|
|
|
|
it("should return undefined if passed something falsy", () => {
|
2017-10-22 00:03:26 -05:00
|
|
|
expect(dateMath.parse(false)).toBe(undefined);
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should return undefined if I pass an operator besides [+-/]", () => {
|
|
|
|
expect(dateMath.parse("now&1d")).toBe(undefined);
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it(
|
|
|
|
"should return undefined if I pass a unit besides" + spans.toString(),
|
|
|
|
() => {
|
|
|
|
expect(dateMath.parse("now+5f")).toBe(undefined);
|
|
|
|
}
|
|
|
|
);
|
2015-09-16 11:48:41 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should return undefined if rounding unit is not 1", () => {
|
|
|
|
expect(dateMath.parse("now/2y")).toBe(undefined);
|
|
|
|
expect(dateMath.parse("now/0.5y")).toBe(undefined);
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should not go into an infinite loop when missing a unit", () => {
|
|
|
|
expect(dateMath.parse("now-0")).toBe(undefined);
|
|
|
|
expect(dateMath.parse("now-00")).toBe(undefined);
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("now/d should set to start of current day", () => {
|
|
|
|
var expected = new Date();
|
|
|
|
expected.setHours(0);
|
|
|
|
expected.setMinutes(0);
|
|
|
|
expected.setSeconds(0);
|
|
|
|
expected.setMilliseconds(0);
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
var startOfDay = dateMath.parse("now/d", false).valueOf();
|
2017-10-22 00:03:26 -05:00
|
|
|
expect(startOfDay).toBe(expected.getTime());
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
|
2017-07-24 11:55:57 -05:00
|
|
|
it("now/d on a utc dashboard should be start of the current day in UTC time", () => {
|
|
|
|
var today = new Date();
|
2017-12-19 09:06:54 -06:00
|
|
|
var expected = new Date(
|
|
|
|
Date.UTC(
|
|
|
|
today.getUTCFullYear(),
|
|
|
|
today.getUTCMonth(),
|
|
|
|
today.getUTCDate(),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
var startOfDay = dateMath.parse("now/d", false, "utc").valueOf();
|
2017-10-22 00:03:26 -05:00
|
|
|
expect(startOfDay).toBe(expected.getTime());
|
2017-07-24 11:55:57 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("subtraction", () => {
|
2015-09-16 11:48:41 -05:00
|
|
|
var now;
|
|
|
|
var anchored;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
clock = sinon.useFakeTimers(unix);
|
|
|
|
now = moment();
|
|
|
|
anchored = moment(anchor);
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
_.each(spans, span => {
|
|
|
|
var nowEx = "now-5" + span;
|
|
|
|
var thenEx = anchor + "||-5" + span;
|
2015-09-16 11:48:41 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should return 5" + span + " ago", () => {
|
|
|
|
expect(dateMath.parse(nowEx).format(format)).toEqual(
|
|
|
|
now.subtract(5, span).format(format)
|
|
|
|
);
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should return 5" + span + " before " + anchor, () => {
|
|
|
|
expect(dateMath.parse(thenEx).format(format)).toEqual(
|
|
|
|
anchored.subtract(5, span).format(format)
|
|
|
|
);
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
});
|
2017-09-21 06:34:51 -05:00
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
clock.restore();
|
|
|
|
});
|
2015-09-15 06:23:36 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("rounding", () => {
|
2015-09-16 11:48:41 -05:00
|
|
|
var now;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
clock = sinon.useFakeTimers(unix);
|
|
|
|
now = moment();
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
_.each(spans, span => {
|
|
|
|
it("should round now to the beginning of the " + span, function() {
|
|
|
|
expect(dateMath.parse("now/" + span).format(format)).toEqual(
|
|
|
|
now.startOf(span).format(format)
|
|
|
|
);
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should round now to the end of the " + span, function() {
|
|
|
|
expect(dateMath.parse("now/" + span, true).format(format)).toEqual(
|
|
|
|
now.endOf(span).format(format)
|
|
|
|
);
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
|
|
|
});
|
2017-09-21 06:34:51 -05:00
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
clock.restore();
|
|
|
|
});
|
2015-09-17 04:21:38 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("isValid", () => {
|
|
|
|
it("should return false when invalid date text", () => {
|
|
|
|
expect(dateMath.isValid("asd")).toBe(false);
|
2015-09-17 04:21:38 -05:00
|
|
|
});
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should return true when valid date text", () => {
|
|
|
|
expect(dateMath.isValid("now-1h")).toBe(true);
|
2015-09-17 04:21:38 -05:00
|
|
|
});
|
|
|
|
});
|
2015-09-16 11:48:41 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("relative time to date parsing", function() {
|
|
|
|
it("should handle negative time", function() {
|
|
|
|
var date = dateMath.parseDateMath("-2d", moment([2014, 1, 5]));
|
2017-10-22 00:03:26 -05:00
|
|
|
expect(date.valueOf()).toEqual(moment([2014, 1, 3]).valueOf());
|
2015-09-17 04:21:38 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should handle multiple math expressions", function() {
|
|
|
|
var date = dateMath.parseDateMath("-2d-6h", moment([2014, 1, 5]));
|
2017-10-22 00:03:26 -05:00
|
|
|
expect(date.valueOf()).toEqual(moment([2014, 1, 2, 18]).valueOf());
|
2015-09-17 04:21:38 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should return false when invalid expression", function() {
|
|
|
|
var date = dateMath.parseDateMath("2", moment([2014, 1, 5]));
|
2017-10-22 00:03:26 -05:00
|
|
|
expect(date).toEqual(undefined);
|
2015-09-17 04:21:38 -05:00
|
|
|
});
|
2015-09-16 11:48:41 -05:00
|
|
|
});
|
2015-09-15 06:23:36 -05:00
|
|
|
});
|