utils.spec.tsx 6.4 KB

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