rangeutil_specs.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'
  2. import * as rangeUtil from 'app/core/utils/rangeutil';
  3. import _ from 'lodash';
  4. import moment from 'moment';
  5. describe("rangeUtil", () => {
  6. describe("Can get range grouped list of ranges", () => {
  7. it('when custom settings should return default range list', () => {
  8. var groups = rangeUtil.getRelativeTimesList({time_options: []}, 'Last 5 minutes');
  9. expect(_.keys(groups).length).to.be(4)
  10. expect(groups[3][0].active).to.be(true)
  11. });
  12. // it('should add custom options to right section', () => {
  13. // var groups = rangeUtil.getRelativeTimesList({
  14. // time_options: ['12m', '15d']
  15. // }, '');
  16. // var value = _.findWhere(groups["3"], {display: 'Last 12 minutes'});
  17. // expect(value).to.not.be(undefined)
  18. // });
  19. });
  20. describe("Can get range text described", () => {
  21. it('should handle simple old expression with only amount and unit', () => {
  22. var info = rangeUtil.describeTextRange('5m');
  23. expect(info.display).to.be('Last 5 minutes')
  24. });
  25. it('should have singular when amount is 1', () => {
  26. var info = rangeUtil.describeTextRange('1h');
  27. expect(info.display).to.be('Last 1 hour')
  28. });
  29. it('should handle non default amount', () => {
  30. var info = rangeUtil.describeTextRange('13h');
  31. expect(info.display).to.be('Last 13 hours')
  32. expect(info.from).to.be('now-13h')
  33. });
  34. it('should handle now/d', () => {
  35. var info = rangeUtil.describeTextRange('now/d');
  36. expect(info.display).to.be('Today so far');
  37. });
  38. it('should handle now/w', () => {
  39. var info = rangeUtil.describeTextRange('now/w');
  40. expect(info.display).to.be('This week so far');
  41. });
  42. });
  43. describe("Can get date range described", () => {
  44. it('Date range with simple ranges', () => {
  45. var text = rangeUtil.describeTimeRange({from: 'now-1h', to: 'now'});
  46. expect(text).to.be('Last 1 hour')
  47. });
  48. it('Date range with rounding ranges', () => {
  49. var text = rangeUtil.describeTimeRange({from: 'now/d+6h', to: 'now'});
  50. expect(text).to.be('now/d+6h to now')
  51. });
  52. it('Date range with absolute to now', () => {
  53. var text = rangeUtil.describeTimeRange({from: moment([2014,10,10,2,3,4]), to: 'now'});
  54. expect(text).to.be('Nov 10, 2014 02:03:04 to a few seconds ago')
  55. });
  56. it('Date range with absolute to relative', () => {
  57. var text = rangeUtil.describeTimeRange({from: moment([2014,10,10,2,3,4]), to: 'now-1d'});
  58. expect(text).to.be('Nov 10, 2014 02:03:04 to a day ago')
  59. });
  60. it('Date range with relative to absolute', () => {
  61. var text = rangeUtil.describeTimeRange({from: 'now-7d', to: moment([2014,10,10,2,3,4])});
  62. expect(text).to.be('7 days ago to Nov 10, 2014 02:03:04')
  63. });
  64. it('Date range with non matching default ranges', () => {
  65. var text = rangeUtil.describeTimeRange({from: 'now-13h', to: 'now'});
  66. expect(text).to.be('Last 13 hours')
  67. });
  68. it('Date range with from and to both are in now-* format', () => {
  69. var text = rangeUtil.describeTimeRange({from: 'now-6h', to: 'now-3h'});
  70. expect(text).to.be('now-6h to now-3h')
  71. });
  72. it('Date range with from and to both are either in now-* or now/* format', () => {
  73. var text = rangeUtil.describeTimeRange({from: 'now/d+6h', to: 'now-3h'});
  74. expect(text).to.be('now/d+6h to now-3h')
  75. });
  76. it('Date range with from and to both are either in now-* or now+* format', () => {
  77. var text = rangeUtil.describeTimeRange({from: 'now-6h', to: 'now+1h'});
  78. expect(text).to.be('now-6h to now+1h')
  79. });
  80. });
  81. });