useSpanMetricsSeries.spec.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import type {ReactNode} from 'react';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {makeTestQueryClient} from 'sentry-test/queryClient';
  4. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  5. import {QueryClientProvider} from 'sentry/utils/queryClient';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useSpanMetricsSeries';
  10. import type {MetricsProperty} from 'sentry/views/starfish/types';
  11. jest.mock('sentry/utils/useLocation');
  12. jest.mock('sentry/utils/usePageFilters');
  13. jest.mock('sentry/utils/useOrganization');
  14. function Wrapper({children}: {children?: ReactNode}) {
  15. return (
  16. <QueryClientProvider client={makeTestQueryClient()}>{children}</QueryClientProvider>
  17. );
  18. }
  19. describe('useSpanMetricsSeries', () => {
  20. const organization = OrganizationFixture();
  21. jest.mocked(usePageFilters).mockReturnValue({
  22. isReady: true,
  23. desyncedFilters: new Set(),
  24. pinnedFilters: new Set(),
  25. shouldPersist: true,
  26. selection: {
  27. datetime: {
  28. period: '10d',
  29. start: null,
  30. end: null,
  31. utc: false,
  32. },
  33. environments: [],
  34. projects: [],
  35. },
  36. });
  37. jest.mocked(useLocation).mockReturnValue({
  38. pathname: '',
  39. search: '',
  40. query: {},
  41. hash: '',
  42. state: undefined,
  43. action: 'PUSH',
  44. key: '',
  45. });
  46. jest.mocked(useOrganization).mockReturnValue(organization);
  47. it('queries for current selection', async () => {
  48. const eventsRequest = MockApiClient.addMockResponse({
  49. url: `/organizations/${organization.slug}/events-stats/`,
  50. method: 'GET',
  51. body: {
  52. 'spm()': {
  53. data: [
  54. [1699907700, [{count: 7810.2}]],
  55. [1699908000, [{count: 1216.8}]],
  56. ],
  57. },
  58. },
  59. });
  60. const {result, waitForNextUpdate} = reactHooks.renderHook(
  61. ({filters, yAxis}) => useSpanMetricsSeries({filters, yAxis}),
  62. {
  63. wrapper: Wrapper,
  64. initialProps: {
  65. filters: {
  66. 'span.group': '221aa7ebd216',
  67. transaction: '/api/details',
  68. release: '0.0.1',
  69. 'resource.render_blocking_status': 'blocking' as const,
  70. },
  71. yAxis: ['spm()'] as MetricsProperty[],
  72. },
  73. }
  74. );
  75. expect(result.current.isLoading).toEqual(true);
  76. expect(eventsRequest).toHaveBeenCalledWith(
  77. '/organizations/org-slug/events-stats/',
  78. expect.objectContaining({
  79. method: 'GET',
  80. query: expect.objectContaining({
  81. query: `span.group:221aa7ebd216 transaction:/api/details release:0.0.1 resource.render_blocking_status:blocking`,
  82. dataset: 'spansMetrics',
  83. statsPeriod: '10d',
  84. referrer: 'span-metrics-series',
  85. interval: '30m',
  86. yAxis: 'spm()',
  87. }),
  88. })
  89. );
  90. await waitForNextUpdate();
  91. expect(result.current.isLoading).toEqual(false);
  92. expect(result.current.data).toEqual({
  93. 'spm()': {
  94. data: [
  95. {name: '2023-11-13T20:35:00+00:00', value: 7810.2},
  96. {name: '2023-11-13T20:40:00+00:00', value: 1216.8},
  97. ],
  98. seriesName: 'spm()',
  99. },
  100. });
  101. });
  102. it('adjusts interval based on the yAxis', async () => {
  103. const eventsRequest = MockApiClient.addMockResponse({
  104. url: `/organizations/${organization.slug}/events-stats/`,
  105. method: 'GET',
  106. body: {},
  107. });
  108. const {rerender, waitForNextUpdate} = reactHooks.renderHook(
  109. ({yAxis}) => useSpanMetricsSeries({yAxis}),
  110. {
  111. wrapper: Wrapper,
  112. initialProps: {
  113. yAxis: ['avg(span.self_time)', 'spm()'] as MetricsProperty[],
  114. },
  115. }
  116. );
  117. expect(eventsRequest).toHaveBeenLastCalledWith(
  118. '/organizations/org-slug/events-stats/',
  119. expect.objectContaining({
  120. method: 'GET',
  121. query: expect.objectContaining({
  122. interval: '30m',
  123. yAxis: ['avg(span.self_time)', 'spm()'] as MetricsProperty[],
  124. }),
  125. })
  126. );
  127. rerender({
  128. yAxis: ['p95(span.self_time)', 'spm()'] as MetricsProperty[],
  129. });
  130. expect(eventsRequest).toHaveBeenLastCalledWith(
  131. '/organizations/org-slug/events-stats/',
  132. expect.objectContaining({
  133. method: 'GET',
  134. query: expect.objectContaining({
  135. interval: '1h',
  136. yAxis: ['p95(span.self_time)', 'spm()'] as MetricsProperty[],
  137. }),
  138. })
  139. );
  140. await waitForNextUpdate();
  141. });
  142. });