datemath_specs.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {describe, beforeEach, afterEach, it, sinon, expect} from 'test/lib/common';
  2. import * as dateMath from 'app/core/utils/datemath';
  3. import moment from 'moment';
  4. import _ from 'lodash';
  5. describe("DateMath", () => {
  6. var spans = ['s', 'm', 'h', 'd', 'w', 'M', 'y'];
  7. var anchor = '2014-01-01T06:06:06.666Z';
  8. var unix = moment(anchor).valueOf();
  9. var format = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
  10. var clock;
  11. describe('errors', () => {
  12. it('should return undefined if passed something falsy', () => {
  13. expect(dateMath.parse(false)).to.be(undefined);
  14. });
  15. it('should return undefined if I pass an operator besides [+-/]', () => {
  16. expect(dateMath.parse('now&1d')).to.be(undefined);
  17. });
  18. it('should return undefined if I pass a unit besides' + spans.toString(), () => {
  19. expect(dateMath.parse('now+5f')).to.be(undefined);
  20. });
  21. it('should return undefined if rounding unit is not 1', () => {
  22. expect(dateMath.parse('now/2y')).to.be(undefined);
  23. expect(dateMath.parse('now/0.5y')).to.be(undefined);
  24. });
  25. it('should not go into an infinite loop when missing a unit', () => {
  26. expect(dateMath.parse('now-0')).to.be(undefined);
  27. expect(dateMath.parse('now-00')).to.be(undefined);
  28. });
  29. });
  30. it("now/d should set to start of current day", () => {
  31. var expected = new Date();
  32. expected.setHours(0);
  33. expected.setMinutes(0);
  34. expected.setSeconds(0);
  35. expected.setMilliseconds(0);
  36. var startOfDay = dateMath.parse('now/d', false).valueOf();
  37. expect(startOfDay).to.be(expected.getTime());
  38. });
  39. it("now/d on a utc dashboard should be start of the current day in UTC time", () => {
  40. var today = new Date();
  41. var expected = new Date(Date.UTC(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0));
  42. var startOfDay = dateMath.parse('now/d', false, 'utc').valueOf();
  43. expect(startOfDay).to.be(expected.getTime());
  44. });
  45. describe('subtraction', () => {
  46. var now;
  47. var anchored;
  48. beforeEach(() => {
  49. clock = sinon.useFakeTimers(unix);
  50. now = moment();
  51. anchored = moment(anchor);
  52. });
  53. _.each(spans, (span) => {
  54. var nowEx = 'now-5' + span;
  55. var thenEx = anchor + '||-5' + span;
  56. it('should return 5' + span + ' ago', () => {
  57. expect(dateMath.parse(nowEx).format(format)).to.eql(now.subtract(5, span).format(format));
  58. });
  59. it('should return 5' + span + ' before ' + anchor, () => {
  60. expect(dateMath.parse(thenEx).format(format)).to.eql(anchored.subtract(5, span).format(format));
  61. });
  62. });
  63. afterEach(() => {
  64. clock.restore();
  65. });
  66. });
  67. describe('rounding', () => {
  68. var now;
  69. var anchored;
  70. beforeEach(() => {
  71. clock = sinon.useFakeTimers(unix);
  72. now = moment();
  73. anchored = moment(anchor);
  74. });
  75. _.each(spans, (span) => {
  76. it('should round now to the beginning of the ' + span, function () {
  77. expect(dateMath.parse('now/' + span).format(format)).to.eql(now.startOf(span).format(format));
  78. });
  79. it('should round now to the end of the ' + span, function () {
  80. expect(dateMath.parse('now/' + span, true).format(format)).to.eql(now.endOf(span).format(format));
  81. });
  82. });
  83. afterEach(() => {
  84. clock.restore();
  85. });
  86. });
  87. describe('isValid', () => {
  88. it('should return false when invalid date text', () => {
  89. expect(dateMath.isValid('asd')).to.be(false);
  90. });
  91. it('should return true when valid date text', () => {
  92. expect(dateMath.isValid('now-1h')).to.be(true);
  93. });
  94. });
  95. describe('relative time to date parsing', function() {
  96. it('should handle negative time', function() {
  97. var date = dateMath.parseDateMath('-2d', moment([2014, 1, 5]));
  98. expect(date.valueOf()).to.equal(moment([2014, 1, 3]).valueOf());
  99. });
  100. it('should handle multiple math expressions', function() {
  101. var date = dateMath.parseDateMath('-2d-6h', moment([2014, 1, 5]));
  102. expect(date.valueOf()).to.equal(moment([2014, 1, 2, 18]).valueOf());
  103. });
  104. it('should return false when invalid expression', function() {
  105. var date = dateMath.parseDateMath('2', moment([2014, 1, 5]));
  106. expect(date).to.equal(undefined);
  107. });
  108. });
  109. });