rangeutil_specs.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. });
  13. describe("Can get range text described", () => {
  14. it('should handle simple old expression with only amount and unit', () => {
  15. var info = rangeUtil.describeTextRange('5m');
  16. expect(info.display).to.be('Last 5 minutes');
  17. });
  18. it('should have singular when amount is 1', () => {
  19. var info = rangeUtil.describeTextRange('1h');
  20. expect(info.display).to.be('Last 1 hour');
  21. });
  22. it('should handle non default amount', () => {
  23. var info = rangeUtil.describeTextRange('13h');
  24. expect(info.display).to.be('Last 13 hours');
  25. expect(info.from).to.be('now-13h');
  26. });
  27. it('should handle non default future amount', () => {
  28. var info = rangeUtil.describeTextRange('+3h');
  29. expect(info.display).to.be('Next 3 hours');
  30. expect(info.from).to.be('now');
  31. expect(info.to).to.be('now+3h');
  32. });
  33. it('should handle now/d', () => {
  34. var info = rangeUtil.describeTextRange('now/d');
  35. expect(info.display).to.be('Today so far');
  36. });
  37. it('should handle now/w', () => {
  38. var info = rangeUtil.describeTextRange('now/w');
  39. expect(info.display).to.be('This week so far');
  40. });
  41. });
  42. describe("Can get date range described", () => {
  43. it('Date range with simple ranges', () => {
  44. var text = rangeUtil.describeTimeRange({from: 'now-1h', to: 'now'});
  45. expect(text).to.be('Last 1 hour');
  46. });
  47. it('Date range with rounding ranges', () => {
  48. var text = rangeUtil.describeTimeRange({from: 'now/d+6h', to: 'now'});
  49. expect(text).to.be('now/d+6h to now');
  50. });
  51. it('Date range with absolute to now', () => {
  52. var text = rangeUtil.describeTimeRange({from: moment([2014,10,10,2,3,4]), to: 'now'});
  53. expect(text).to.be('Nov 10, 2014 02:03:04 to a few seconds ago');
  54. });
  55. it('Date range with absolute to relative', () => {
  56. var text = rangeUtil.describeTimeRange({from: moment([2014,10,10,2,3,4]), to: 'now-1d'});
  57. expect(text).to.be('Nov 10, 2014 02:03:04 to a day ago');
  58. });
  59. it('Date range with relative to absolute', () => {
  60. var text = rangeUtil.describeTimeRange({from: 'now-7d', to: moment([2014,10,10,2,3,4])});
  61. expect(text).to.be('7 days ago to Nov 10, 2014 02:03:04');
  62. });
  63. it('Date range with non matching default ranges', () => {
  64. var text = rangeUtil.describeTimeRange({from: 'now-13h', to: 'now'});
  65. expect(text).to.be('Last 13 hours');
  66. });
  67. it('Date range with from and to both are in now-* format', () => {
  68. var text = rangeUtil.describeTimeRange({from: 'now-6h', to: 'now-3h'});
  69. expect(text).to.be('now-6h to now-3h');
  70. });
  71. it('Date range with from and to both are either in now-* or now/* format', () => {
  72. var text = rangeUtil.describeTimeRange({from: 'now/d+6h', to: 'now-3h'});
  73. expect(text).to.be('now/d+6h to now-3h');
  74. });
  75. it('Date range with from and to both are either in now-* or now+* format', () => {
  76. var text = rangeUtil.describeTimeRange({from: 'now-6h', to: 'now+1h'});
  77. expect(text).to.be('now-6h to now+1h');
  78. });
  79. });
  80. });