datemath_specs.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'
  2. import dateMath = require('app/core/utils/datemath')
  3. import _ = require('lodash')
  4. import moment = require('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.only('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. });
  76. export = {};