mirror of
https://github.com/grafana/grafana.git
synced 2026-07-29 15:59:50 -05:00
feat(timepicker2): added date math tests
This commit is contained in:
@@ -11,7 +11,7 @@ var unitsDesc = unitsAsc.reverse();
|
||||
|
||||
export class DateMath {
|
||||
|
||||
static parse(text, roundUp) {
|
||||
static parse(text, roundUp?) {
|
||||
if (!text) { return undefined; }
|
||||
if (moment.isMoment(text)) { return text; }
|
||||
if (_.isDate(text)) { return moment(text); }
|
||||
@@ -44,7 +44,7 @@ export class DateMath {
|
||||
return DateMath.parseDateMath(mathString, time, roundUp);
|
||||
}
|
||||
|
||||
static parseDateMath(mathString, time, roundUp) {
|
||||
static parseDateMath(mathString, time, roundUp?) {
|
||||
var dateTime = time;
|
||||
var i = 0;
|
||||
var len = mathString.length;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
///<reference path="../../app/headers/common.d.ts" />
|
||||
|
||||
var _global = <any>(window);
|
||||
var beforeEach = _global.beforeEach;
|
||||
var describe = _global.describe;
|
||||
var it = _global.it;
|
||||
var sinon = _global.sinon;
|
||||
var expect = _global.expect;
|
||||
|
||||
export {
|
||||
beforeEach,
|
||||
describe,
|
||||
it,
|
||||
sinon,
|
||||
expect
|
||||
}
|
||||
@@ -1,15 +1,96 @@
|
||||
import {DateMath} from 'app/core/utils/datemath'
|
||||
import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'
|
||||
|
||||
declare var describe: any;
|
||||
declare var expect: any;
|
||||
declare var it: any;
|
||||
import _ = require('lodash')
|
||||
import moment = require('moment')
|
||||
|
||||
describe("test", () => {
|
||||
describe.only("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;
|
||||
|
||||
it("hello", () => {
|
||||
expect(true).to.be(true);
|
||||
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('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 = {};
|
||||
|
||||
@@ -103,79 +103,30 @@ function file2moduleName(filePath) {
|
||||
}
|
||||
|
||||
require([
|
||||
'lodash',
|
||||
'angular',
|
||||
'angularMocks',
|
||||
'app/app',
|
||||
], function(angular) {
|
||||
], function(_, angular) {
|
||||
'use strict';
|
||||
|
||||
var specs = [];
|
||||
|
||||
for (var file in window.__karma__.files) {
|
||||
if (/base\/test\/specs.*/.test(file)) {
|
||||
console.log(file);
|
||||
file = file2moduleName(file);
|
||||
console.log(file);
|
||||
specs.push(file);
|
||||
//file = file.replace(/^\/base\/test/, '');
|
||||
//specs.push(file);
|
||||
//window.tests.push();
|
||||
}
|
||||
}
|
||||
|
||||
require(specs, function() {
|
||||
window.__karma__.start();
|
||||
});
|
||||
|
||||
angular.module('grafana', ['ngRoute']);
|
||||
angular.module('grafana.services', ['ngRoute', '$strap.directives']);
|
||||
angular.module('grafana.panels', []);
|
||||
angular.module('grafana.filters', []);
|
||||
angular.module('grafana.routes', ['ngRoute']);
|
||||
|
||||
// var specs = [
|
||||
// 'specs/lexer-specs',
|
||||
// 'specs/parser-specs',
|
||||
// 'specs/gfunc-specs',
|
||||
// 'specs/timeSeries-specs',
|
||||
// 'specs/row-ctrl-specs',
|
||||
// 'specs/graphiteTargetCtrl-specs',
|
||||
// 'specs/graphiteDatasource-specs',
|
||||
// 'specs/influxSeries-specs',
|
||||
// 'specs/influxSeries08-specs',
|
||||
// 'specs/influxQueryBuilder-specs',
|
||||
// 'specs/influx09-querybuilder-specs',
|
||||
// // 'specs/influxdb-datasource-specs',
|
||||
// 'specs/influxdbQueryCtrl-specs',
|
||||
// // 'specs/kairosdb-datasource-specs',
|
||||
// 'specs/graph-ctrl-specs',
|
||||
// 'specs/graph-specs',
|
||||
// 'specs/graph-tooltip-specs',
|
||||
// 'specs/seriesOverridesCtrl-specs',
|
||||
// 'specs/shareModalCtrl-specs',
|
||||
// 'specs/timeSrv-specs',
|
||||
// 'specs/panelSrv-specs',
|
||||
// 'specs/templateSrv-specs',
|
||||
// 'specs/templateValuesSrv-specs',
|
||||
// 'specs/kbn-format-specs',
|
||||
// 'specs/dashboardSrv-specs',
|
||||
// 'specs/dashboardViewStateSrv-specs',
|
||||
// 'specs/singlestat-specs',
|
||||
// 'specs/dynamicDashboardSrv-specs',
|
||||
// 'specs/unsavedChangesSrv-specs',
|
||||
// 'specs/value_select_dropdown_specs',
|
||||
// 'specs/opentsdbDatasource-specs',
|
||||
// 'specs/cloudwatch-datasource-specs',
|
||||
// 'specs/elasticsearch-specs',
|
||||
// 'specs/elasticsearch-querybuilder-specs',
|
||||
// 'specs/elasticsearch-queryctrl-specs',
|
||||
// 'specs/elasticsearch-indexPattern-specs',
|
||||
// 'specs/elasticsearch-response-specs',
|
||||
// 'specs/core/utils/datemath_specs',
|
||||
// ];
|
||||
//
|
||||
// var pluginSpecs = (config.plugins.specs || []).map(function (spec) {
|
||||
// return '../plugins/' + spec;
|
||||
// });
|
||||
require(specs, function() {
|
||||
window.__karma__.start();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user