datemath.test.ts 4.1 KB

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