utils.spec.jsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {
  2. canIncludePreviousPeriod,
  3. getDiffInMinutes,
  4. getInterval,
  5. getSeriesApiInterval,
  6. lightenHexToRgb,
  7. processTableResults,
  8. } from 'sentry/components/charts/utils';
  9. describe('Chart Utils', function () {
  10. describe('getInterval()', function () {
  11. describe('with high fidelity', function () {
  12. it('greater than 24 hours', function () {
  13. expect(getInterval({period: '25h'}, 'high')).toBe('30m');
  14. });
  15. it('less than 30 minutes', function () {
  16. expect(getInterval({period: '20m'}, 'high')).toBe('1m');
  17. });
  18. it('between 30 minutes and 24 hours', function () {
  19. expect(getInterval({period: '12h'}, 'high')).toBe('5m');
  20. });
  21. it('more than 14 days', function () {
  22. expect(getInterval({period: '14d'}, 'high')).toBe('30m');
  23. });
  24. it('more than 30 days', function () {
  25. expect(getInterval({period: '30d'}, 'high')).toBe('1h');
  26. });
  27. it('more than 60 days', function () {
  28. expect(getInterval({period: '90d'}, 'high')).toBe('4h');
  29. });
  30. });
  31. describe('with medium fidelity', function () {
  32. it('greater than 24 hours', function () {
  33. expect(getInterval({period: '25h'})).toBe('1h');
  34. expect(getInterval({period: '25h'}, 'medium')).toBe('1h');
  35. });
  36. it('less than 30 minutes', function () {
  37. expect(getInterval({period: '20m'})).toBe('5m');
  38. expect(getInterval({period: '20m'}, 'medium')).toBe('5m');
  39. });
  40. it('between 30 minutes and 24 hours', function () {
  41. expect(getInterval({period: '12h'})).toBe('15m');
  42. expect(getInterval({period: '12h'}, 'medium')).toBe('15m');
  43. });
  44. it('more than 14 days', function () {
  45. expect(getInterval({period: '14d'})).toBe('1h');
  46. expect(getInterval({period: '14d'}, 'medium')).toBe('1h');
  47. });
  48. it('more than 30 days', function () {
  49. expect(getInterval({period: '30d'})).toBe('4h');
  50. expect(getInterval({period: '30d'}, 'medium')).toBe('4h');
  51. });
  52. it('more than 90 days', function () {
  53. expect(getInterval({period: '90d'})).toBe('1d');
  54. expect(getInterval({period: '90d'}, 'medium')).toBe('1d');
  55. });
  56. });
  57. describe('with low fidelity', function () {
  58. it('greater than 24 hours', function () {
  59. expect(getInterval({period: '25h'}, 'low')).toBe('6h');
  60. });
  61. it('less than 30 minutes', function () {
  62. expect(getInterval({period: '20m'}, 'low')).toBe('10m');
  63. });
  64. it('between 30 minutes and 24 hours', function () {
  65. expect(getInterval({period: '12h'}, 'low')).toBe('1h');
  66. });
  67. it('more than 14 days', function () {
  68. expect(getInterval({period: '14d'}, 'low')).toBe('12h');
  69. });
  70. it('more than 30 days', function () {
  71. expect(getInterval({period: '30d'}, 'low')).toBe('1d');
  72. });
  73. it('more than 90 days', function () {
  74. expect(getInterval({period: '90d'}, 'low')).toBe('2d');
  75. });
  76. });
  77. });
  78. describe('getUsageInterval', function () {
  79. it('calculates intervals for a period', function () {
  80. expect(getSeriesApiInterval({period: '90d'})).toBe('1d');
  81. expect(getSeriesApiInterval({period: '60d'})).toBe('1d');
  82. expect(getSeriesApiInterval({period: '59d'})).toBe('4h');
  83. expect(getSeriesApiInterval({period: '30d'})).toBe('4h');
  84. expect(getSeriesApiInterval({period: '29d'})).toBe('1h');
  85. expect(getSeriesApiInterval({period: '7h'})).toBe('1h');
  86. expect(getSeriesApiInterval({period: '6h'})).toBe('1h');
  87. expect(getSeriesApiInterval({period: '1h'})).toBe('1h');
  88. });
  89. });
  90. describe('getDiffInMinutes()', function () {
  91. describe('with period string', function () {
  92. it('can parse a period string in seconds', function () {
  93. expect(getDiffInMinutes({period: '30s'})).toBe(0.5);
  94. });
  95. it('can parse a period string in minutes', function () {
  96. expect(getDiffInMinutes({period: '15m'})).toBe(15);
  97. });
  98. it('can parse a period string in hours', function () {
  99. expect(getDiffInMinutes({period: '1h'})).toBe(60);
  100. });
  101. it('can parse a period string in days', function () {
  102. expect(getDiffInMinutes({period: '5d'})).toBe(7200);
  103. });
  104. it('can parse a period string in weeks', function () {
  105. expect(getDiffInMinutes({period: '1w'})).toBe(10080);
  106. });
  107. });
  108. // This uses moment so we probably don't need to test it too extensively
  109. describe('with absolute dates', function () {});
  110. });
  111. describe('canIncludePreviousPeriod()', function () {
  112. it('does not include if `includePrevious` is false', function () {
  113. expect(canIncludePreviousPeriod(false, '7d')).toBe(false);
  114. });
  115. it('is true if period is less than or equal to 45 days', function () {
  116. expect(canIncludePreviousPeriod(true, '45d')).toBe(true);
  117. });
  118. it('is false if period is greater than 45d', function () {
  119. expect(canIncludePreviousPeriod(true, '46d')).toBe(false);
  120. });
  121. it('returns value of `includePrevious` if no period', function () {
  122. expect(canIncludePreviousPeriod(true)).toBe(true);
  123. expect(canIncludePreviousPeriod(false)).toBe(false);
  124. });
  125. });
  126. describe('lightenHexToRgb', function () {
  127. it('converts hex to rgb and lightens values', function () {
  128. expect(lightenHexToRgb(['#2f2936', '#f0f0f0'])).toEqual([
  129. 'rgb(77, 71, 84)',
  130. 'rgb(255, 255, 255)',
  131. ]);
  132. });
  133. });
  134. describe('processTableResults', function () {
  135. it('transforms TableDataWithTitle array to chartable data', function () {
  136. const tableData = [
  137. {
  138. data: [
  139. {
  140. 'geo.country_code': 'PE',
  141. count: 9215,
  142. },
  143. {
  144. 'geo.country_code': 'VI',
  145. count: 1,
  146. },
  147. ],
  148. meta: {
  149. 'geo.country_code': 'string',
  150. count: 'integer',
  151. },
  152. title: 'Country',
  153. },
  154. ];
  155. const result = {
  156. title: 'Country',
  157. data: [
  158. {
  159. name: 'PE',
  160. value: 9215,
  161. },
  162. {
  163. name: 'VI',
  164. value: 1,
  165. },
  166. ],
  167. };
  168. expect(processTableResults(tableData)).toEqual(result);
  169. });
  170. });
  171. });