useChartInterval.spec.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {act, render} from 'sentry-test/reactTestingLibrary';
  2. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  3. import {parsePeriodToHours} from 'sentry/utils/duration/parsePeriodToHours';
  4. import {getIntervalOptionsForPageFilter, useChartInterval} from './useChartInterval';
  5. describe('useChartInterval', function () {
  6. beforeEach(() => {
  7. PageFiltersStore.reset();
  8. PageFiltersStore.init();
  9. });
  10. it('allows changing chart interval', async function () {
  11. let chartInterval!: ReturnType<typeof useChartInterval>[0];
  12. let setChartInterval!: ReturnType<typeof useChartInterval>[1];
  13. let intervalOptions!: ReturnType<typeof useChartInterval>[2];
  14. function TestPage() {
  15. [chartInterval, setChartInterval, intervalOptions] = useChartInterval();
  16. return null;
  17. }
  18. render(<TestPage />, {disableRouterMocks: true});
  19. expect(intervalOptions).toEqual([
  20. {value: '1h', label: '1 hour'},
  21. {value: '3h', label: '3 hours'},
  22. {value: '12h', label: '12 hours'},
  23. {value: '1d', label: '1 day'},
  24. ]);
  25. expect(chartInterval).toBe('3h'); // default
  26. await act(() => setChartInterval('1d'));
  27. expect(chartInterval).toBe('1d');
  28. // Update page filters to change interval options
  29. await act(() =>
  30. PageFiltersStore.updateDateTime({
  31. period: '1h',
  32. start: null,
  33. end: null,
  34. utc: true,
  35. })
  36. );
  37. expect(intervalOptions).toEqual([
  38. {value: '1m', label: '1 minute'},
  39. {value: '5m', label: '5 minutes'},
  40. {value: '15m', label: '15 minutes'},
  41. ]);
  42. await act(() => {
  43. setChartInterval('1m');
  44. });
  45. expect(chartInterval).toBe('1m');
  46. });
  47. });
  48. describe('getIntervalOptionsForPageFilter', function () {
  49. it.each([
  50. '1h',
  51. '23h',
  52. '1d',
  53. '6d',
  54. '7d',
  55. '13d',
  56. '14d',
  57. '29d',
  58. '30d',
  59. '59d',
  60. '60d',
  61. '89d',
  62. '90d',
  63. ])(
  64. 'returns interval options with resulting in less than 1000 buckets',
  65. function (period) {
  66. const periodInHours = parsePeriodToHours(period);
  67. const options = getIntervalOptionsForPageFilter({
  68. period,
  69. start: null,
  70. end: null,
  71. utc: null,
  72. });
  73. options.forEach(({value}) => {
  74. const intervalInHours = parsePeriodToHours(value);
  75. expect(periodInHours / intervalInHours).toBeLessThan(1000);
  76. });
  77. }
  78. );
  79. });