2021-02-11 06:45:25 -06:00
|
|
|
import { rangeUtil, dateTime } from '@grafana/data';
|
2015-09-17 02:44:51 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('rangeUtil', () => {
|
|
|
|
describe('Can get range text described', () => {
|
|
|
|
it('should handle simple old expression with only amount and unit', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const info = rangeUtil.describeTextRange('5m');
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(info.display).toBe('Last 5 minutes');
|
2015-09-17 02:44:51 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should have singular when amount is 1', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const info = rangeUtil.describeTextRange('1h');
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(info.display).toBe('Last 1 hour');
|
2015-09-17 02:44:51 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should handle non default amount', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const info = rangeUtil.describeTextRange('13h');
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(info.display).toBe('Last 13 hours');
|
|
|
|
expect(info.from).toBe('now-13h');
|
2015-09-17 05:40:04 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should handle non default future amount', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const info = rangeUtil.describeTextRange('+3h');
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(info.display).toBe('Next 3 hours');
|
|
|
|
expect(info.from).toBe('now');
|
|
|
|
expect(info.to).toBe('now+3h');
|
2016-10-18 05:11:46 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should handle now/d', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const info = rangeUtil.describeTextRange('now/d');
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(info.display).toBe('Today so far');
|
2015-09-17 02:44:51 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should handle now/w', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const info = rangeUtil.describeTextRange('now/w');
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(info.display).toBe('This week so far');
|
2015-09-17 02:44:51 -05:00
|
|
|
});
|
2017-05-17 06:52:39 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should handle now/M', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const info = rangeUtil.describeTextRange('now/M');
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(info.display).toBe('This month so far');
|
2017-05-17 06:52:39 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should handle now/y', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const info = rangeUtil.describeTextRange('now/y');
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(info.display).toBe('This year so far');
|
2017-05-17 06:52:39 -05:00
|
|
|
});
|
2015-09-17 05:40:04 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('Can get date range described', () => {
|
|
|
|
it('Date range with simple ranges', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({ from: 'now-1h', to: 'now' });
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(text).toBe('Last 1 hour');
|
2015-09-17 05:40:04 -05:00
|
|
|
});
|
2015-09-17 02:44:51 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('Date range with rounding ranges', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({ from: 'now/d+6h', to: 'now' });
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(text).toBe('now/d+6h to now');
|
2015-10-01 09:36:16 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('Date range with absolute to now', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({
|
2019-05-08 06:51:44 -05:00
|
|
|
from: dateTime([2014, 10, 10, 2, 3, 4]),
|
2017-12-20 05:33:33 -06:00
|
|
|
to: 'now',
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2019-06-24 07:39:59 -05:00
|
|
|
expect(text).toBe('2014-11-10 02:03:04 to a few seconds ago');
|
2015-09-18 06:54:31 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('Date range with absolute to relative', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({
|
2019-05-08 06:51:44 -05:00
|
|
|
from: dateTime([2014, 10, 10, 2, 3, 4]),
|
2017-12-20 05:33:33 -06:00
|
|
|
to: 'now-1d',
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2019-06-24 07:39:59 -05:00
|
|
|
expect(text).toBe('2014-11-10 02:03:04 to a day ago');
|
2015-09-18 06:54:31 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('Date range with relative to absolute', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({
|
2017-12-20 05:33:33 -06:00
|
|
|
from: 'now-7d',
|
2019-05-08 06:51:44 -05:00
|
|
|
to: dateTime([2014, 10, 10, 2, 3, 4]),
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2019-06-24 07:39:59 -05:00
|
|
|
expect(text).toBe('7 days ago to 2014-11-10 02:03:04');
|
2015-09-18 06:54:31 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('Date range with non matching default ranges', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({ from: 'now-13h', to: 'now' });
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(text).toBe('Last 13 hours');
|
2015-09-17 05:40:04 -05:00
|
|
|
});
|
2015-11-11 03:34:53 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('Date range with from and to both are in now-* format', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({ from: 'now-6h', to: 'now-3h' });
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(text).toBe('now-6h to now-3h');
|
2015-11-11 03:34:53 -06:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('Date range with from and to both are either in now-* or now/* format', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({
|
2017-12-20 05:33:33 -06:00
|
|
|
from: 'now/d+6h',
|
|
|
|
to: 'now-3h',
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(text).toBe('now/d+6h to now-3h');
|
2015-11-11 03:34:53 -06:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('Date range with from and to both are either in now-* or now+* format', () => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const text = rangeUtil.describeTimeRange({ from: 'now-6h', to: 'now+1h' });
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(text).toBe('now-6h to now+1h');
|
2015-11-11 03:34:53 -06:00
|
|
|
});
|
2015-09-17 02:44:51 -05:00
|
|
|
});
|
|
|
|
});
|