rangeutil.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { rangeUtil } from '@grafana/data';
  2. import _ from 'lodash';
  3. import { dateTime } from '@grafana/data';
  4. describe('rangeUtil', () => {
  5. describe('Can get range grouped list of ranges', () => {
  6. it('when custom settings should return default range list', () => {
  7. const groups: any = 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. const info = rangeUtil.describeTextRange('5m');
  15. expect(info.display).toBe('Last 5 minutes');
  16. });
  17. it('should have singular when amount is 1', () => {
  18. const info = rangeUtil.describeTextRange('1h');
  19. expect(info.display).toBe('Last 1 hour');
  20. });
  21. it('should handle non default amount', () => {
  22. const 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. const 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. const info = rangeUtil.describeTextRange('now/d');
  34. expect(info.display).toBe('Today so far');
  35. });
  36. it('should handle now/w', () => {
  37. const info = rangeUtil.describeTextRange('now/w');
  38. expect(info.display).toBe('This week so far');
  39. });
  40. it('should handle now/M', () => {
  41. const info = rangeUtil.describeTextRange('now/M');
  42. expect(info.display).toBe('This month so far');
  43. });
  44. it('should handle now/y', () => {
  45. const 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. const text = rangeUtil.describeTimeRange({ from: 'now-1h', to: 'now' });
  52. expect(text).toBe('Last 1 hour');
  53. });
  54. it('Date range with rounding ranges', () => {
  55. const 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. const text = rangeUtil.describeTimeRange({
  60. from: dateTime([2014, 10, 10, 2, 3, 4]),
  61. to: 'now',
  62. });
  63. expect(text).toBe('2014-11-10 02:03:04 to a few seconds ago');
  64. });
  65. it('Date range with absolute to relative', () => {
  66. const text = rangeUtil.describeTimeRange({
  67. from: dateTime([2014, 10, 10, 2, 3, 4]),
  68. to: 'now-1d',
  69. });
  70. expect(text).toBe('2014-11-10 02:03:04 to a day ago');
  71. });
  72. it('Date range with relative to absolute', () => {
  73. const text = rangeUtil.describeTimeRange({
  74. from: 'now-7d',
  75. to: dateTime([2014, 10, 10, 2, 3, 4]),
  76. });
  77. expect(text).toBe('7 days ago to 2014-11-10 02:03:04');
  78. });
  79. it('Date range with non matching default ranges', () => {
  80. const text = rangeUtil.describeTimeRange({ from: 'now-13h', to: 'now' });
  81. expect(text).toBe('Last 13 hours');
  82. });
  83. it('Date range with from and to both are in now-* format', () => {
  84. const text = rangeUtil.describeTimeRange({ from: 'now-6h', to: 'now-3h' });
  85. expect(text).toBe('now-6h to now-3h');
  86. });
  87. it('Date range with from and to both are either in now-* or now/* format', () => {
  88. const text = rangeUtil.describeTimeRange({
  89. from: 'now/d+6h',
  90. to: 'now-3h',
  91. });
  92. expect(text).toBe('now/d+6h to now-3h');
  93. });
  94. it('Date range with from and to both are either in now-* or now+* format', () => {
  95. const text = rangeUtil.describeTimeRange({ from: 'now-6h', to: 'now+1h' });
  96. expect(text).toBe('now-6h to now+1h');
  97. });
  98. });
  99. });