rangeutil.jest.ts 3.8 KB

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