utils.spec.tsx 7.3 KB

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