datemath_specs.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'
  2. import * as dateMath from 'app/core/utils/datemath';
  3. import * as _ from 'lodash';
  4. import moment from 'moment';
  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. describe('subtraction', () => {
  40. var now;
  41. var anchored;
  42. beforeEach(() => {
  43. clock = sinon.useFakeTimers(unix);
  44. now = moment();
  45. anchored = moment(anchor);
  46. });
  47. _.each(spans, (span) => {
  48. var nowEx = 'now-5' + span;
  49. var thenEx = anchor + '||-5' + span;
  50. it('should return 5' + span + ' ago', () => {
  51. expect(dateMath.parse(nowEx).format(format)).to.eql(now.subtract(5, span).format(format));
  52. });
  53. it('should return 5' + span + ' before ' + anchor, () => {
  54. expect(dateMath.parse(thenEx).format(format)).to.eql(anchored.subtract(5, span).format(format));
  55. });
  56. });
  57. });
  58. describe('rounding', () => {
  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. it('should round now to the beginning of the ' + span, function () {
  68. expect(dateMath.parse('now/' + span).format(format)).to.eql(now.startOf(span).format(format));
  69. });
  70. it('should round now to the end of the ' + span, function () {
  71. expect(dateMath.parse('now/' + span, true).format(format)).to.eql(now.endOf(span).format(format));
  72. });
  73. });
  74. });
  75. describe('isValid', () => {
  76. it('should return false when invalid date text', () => {
  77. expect(dateMath.isValid('asd')).to.be(false);
  78. });
  79. it('should return true when valid date text', () => {
  80. expect(dateMath.isValid('now-1h')).to.be(true);
  81. });
  82. });
  83. describe('relative time to date parsing', function() {
  84. it('should handle negative time', function() {
  85. var date = dateMath.parseDateMath('-2d', moment([2014, 1, 5]));
  86. expect(date.valueOf()).to.equal(moment([2014, 1, 3]).valueOf());
  87. });
  88. it('should handle multiple math expressions', function() {
  89. var date = dateMath.parseDateMath('-2d-6h', moment([2014, 1, 5]));
  90. expect(date.valueOf()).to.equal(moment([2014, 1, 2, 18]).valueOf());
  91. });
  92. it('should return false when invalid expression', function() {
  93. var date = dateMath.parseDateMath('2', moment([2014, 1, 5]));
  94. expect(date).to.equal(undefined);
  95. });
  96. });
  97. });