rangeutil.test.ts 3.6 KB

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