datemath.jest.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 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)).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(
  19. 'should return undefined if I pass a unit besides' + spans.toString(),
  20. () => {
  21. expect(dateMath.parse('now+5f')).toBe(undefined);
  22. }
  23. );
  24. it('should return undefined if rounding unit is not 1', () => {
  25. expect(dateMath.parse('now/2y')).toBe(undefined);
  26. expect(dateMath.parse('now/0.5y')).toBe(undefined);
  27. });
  28. it('should not go into an infinite loop when missing a unit', () => {
  29. expect(dateMath.parse('now-0')).toBe(undefined);
  30. expect(dateMath.parse('now-00')).toBe(undefined);
  31. });
  32. });
  33. it('now/d should set to start of current day', () => {
  34. var expected = new Date();
  35. expected.setHours(0);
  36. expected.setMinutes(0);
  37. expected.setSeconds(0);
  38. expected.setMilliseconds(0);
  39. var startOfDay = dateMath.parse('now/d', false).valueOf();
  40. expect(startOfDay).toBe(expected.getTime());
  41. });
  42. it('now/d on a utc dashboard should be start of the current day in UTC time', () => {
  43. var today = new Date();
  44. var expected = new Date(
  45. Date.UTC(
  46. today.getUTCFullYear(),
  47. today.getUTCMonth(),
  48. today.getUTCDate(),
  49. 0,
  50. 0,
  51. 0,
  52. 0
  53. )
  54. );
  55. var startOfDay = dateMath.parse('now/d', false, 'utc').valueOf();
  56. expect(startOfDay).toBe(expected.getTime());
  57. });
  58. describe('subtraction', () => {
  59. var now;
  60. var anchored;
  61. beforeEach(() => {
  62. clock = sinon.useFakeTimers(unix);
  63. now = moment();
  64. anchored = moment(anchor);
  65. });
  66. _.each(spans, span => {
  67. var nowEx = 'now-5' + span;
  68. var thenEx = anchor + '||-5' + span;
  69. it('should return 5' + span + ' ago', () => {
  70. expect(dateMath.parse(nowEx).format(format)).toEqual(
  71. now.subtract(5, span).format(format)
  72. );
  73. });
  74. it('should return 5' + span + ' before ' + anchor, () => {
  75. expect(dateMath.parse(thenEx).format(format)).toEqual(
  76. anchored.subtract(5, span).format(format)
  77. );
  78. });
  79. });
  80. afterEach(() => {
  81. clock.restore();
  82. });
  83. });
  84. describe('rounding', () => {
  85. var now;
  86. beforeEach(() => {
  87. clock = sinon.useFakeTimers(unix);
  88. now = moment();
  89. });
  90. _.each(spans, span => {
  91. it('should round now to the beginning of the ' + span, function() {
  92. expect(dateMath.parse('now/' + span).format(format)).toEqual(
  93. now.startOf(span).format(format)
  94. );
  95. });
  96. it('should round now to the end of the ' + span, function() {
  97. expect(dateMath.parse('now/' + span, true).format(format)).toEqual(
  98. now.endOf(span).format(format)
  99. );
  100. });
  101. });
  102. afterEach(() => {
  103. clock.restore();
  104. });
  105. });
  106. describe('isValid', () => {
  107. it('should return false when invalid date text', () => {
  108. expect(dateMath.isValid('asd')).toBe(false);
  109. });
  110. it('should return true when valid date text', () => {
  111. expect(dateMath.isValid('now-1h')).toBe(true);
  112. });
  113. });
  114. describe('relative time to date parsing', function() {
  115. it('should handle negative time', function() {
  116. var date = dateMath.parseDateMath('-2d', moment([2014, 1, 5]));
  117. expect(date.valueOf()).toEqual(moment([2014, 1, 3]).valueOf());
  118. });
  119. it('should handle multiple math expressions', function() {
  120. var date = dateMath.parseDateMath('-2d-6h', moment([2014, 1, 5]));
  121. expect(date.valueOf()).toEqual(moment([2014, 1, 2, 18]).valueOf());
  122. });
  123. it('should return false when invalid expression', function() {
  124. var date = dateMath.parseDateMath('2', moment([2014, 1, 5]));
  125. expect(date).toEqual(undefined);
  126. });
  127. });
  128. });