| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'
- import dateMath = require('app/core/utils/datemath')
- import _ = require('lodash')
- import moment = require('moment')
- describe("DateMath", () => {
- var spans = ['s', 'm', 'h', 'd', 'w', 'M', 'y'];
- var anchor = '2014-01-01T06:06:06.666Z';
- var unix = moment(anchor).valueOf();
- var format = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
- var clock;
- describe('errors', () => {
- it('should return undefined if passed something falsy', () => {
- expect(dateMath.parse(false)).to.be(undefined);
- });
- it('should return undefined if I pass an operator besides [+-/]', () => {
- expect(dateMath.parse('now&1d')).to.be(undefined);
- });
- it('should return undefined if I pass a unit besides' + spans.toString(), () => {
- expect(dateMath.parse('now+5f')).to.be(undefined);
- });
- it('should return undefined if rounding unit is not 1', () => {
- expect(dateMath.parse('now/2y')).to.be(undefined);
- expect(dateMath.parse('now/0.5y')).to.be(undefined);
- });
- it('should not go into an infinite loop when missing a unit', () => {
- expect(dateMath.parse('now-0')).to.be(undefined);
- expect(dateMath.parse('now-00')).to.be(undefined);
- });
- });
- 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);
- var startOfDay = dateMath.parse('now/d', false).valueOf()
- expect(startOfDay).to.be(expected.getTime());
- });
- describe('subtraction', () => {
- var now;
- var anchored;
- beforeEach(() => {
- clock = sinon.useFakeTimers(unix);
- now = moment();
- anchored = moment(anchor);
- });
- _.each(spans, (span) => {
- var nowEx = 'now-5' + span;
- var thenEx = anchor + '||-5' + span;
- it('should return 5' + span + ' ago', () => {
- expect(dateMath.parse(nowEx).format(format)).to.eql(now.subtract(5, span).format(format));
- });
- it('should return 5' + span + ' before ' + anchor, () => {
- expect(dateMath.parse(thenEx).format(format)).to.eql(anchored.subtract(5, span).format(format));
- });
- });
- });
- describe.only('rounding', () => {
- var now;
- var anchored;
- beforeEach(() => {
- clock = sinon.useFakeTimers(unix);
- now = moment();
- anchored = moment(anchor);
- });
- _.each(spans, (span) => {
- it('should round now to the beginning of the ' + span, function () {
- expect(dateMath.parse('now/' + span).format(format)).to.eql(now.startOf(span).format(format));
- });
- it('should round now to the end of the ' + span, function () {
- expect(dateMath.parse('now/' + span, true).format(format)).to.eql(now.endOf(span).format(format));
- });
- });
- });
- });
- export = {};
|