useSpanMetricsTopNSeries.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import type {ReactNode} from 'react';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {makeTestQueryClient} from 'sentry-test/queryClient';
  4. import {renderHook, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import {QueryClientProvider} from 'sentry/utils/queryClient';
  6. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import usePageFilters from 'sentry/utils/usePageFilters';
  10. import {useSpanMetricsTopNSeries} from 'sentry/views/starfish/queries/useSpanMetricsTopNSeries';
  11. import type {SpanMetricsProperty} from 'sentry/views/starfish/types';
  12. jest.mock('sentry/utils/useLocation');
  13. jest.mock('sentry/utils/usePageFilters');
  14. jest.mock('sentry/utils/useOrganization');
  15. function Wrapper({children}: {children?: ReactNode}) {
  16. return (
  17. <QueryClientProvider client={makeTestQueryClient()}>{children}</QueryClientProvider>
  18. );
  19. }
  20. describe('useSpanMetricsTopNSeries', () => {
  21. const organization = OrganizationFixture();
  22. jest.mocked(usePageFilters).mockReturnValue({
  23. isReady: true,
  24. desyncedFilters: new Set(),
  25. pinnedFilters: new Set(),
  26. shouldPersist: true,
  27. selection: {
  28. datetime: {
  29. period: '10d',
  30. start: null,
  31. end: null,
  32. utc: false,
  33. },
  34. environments: [],
  35. projects: [],
  36. },
  37. });
  38. jest.mocked(useLocation).mockReturnValue({
  39. pathname: '',
  40. search: '',
  41. query: {},
  42. hash: '',
  43. state: undefined,
  44. action: 'PUSH',
  45. key: '',
  46. });
  47. jest.mocked(useOrganization).mockReturnValue(organization);
  48. it('rolls multi-axis top-n responses up into multiple series', async () => {
  49. const eventsRequest = MockApiClient.addMockResponse({
  50. url: `/organizations/${organization.slug}/events-stats/`,
  51. method: 'GET',
  52. body: {
  53. '200': {
  54. data: [
  55. [1699907700, [{count: 117}]],
  56. [1699908000, [{count: 199}]],
  57. ],
  58. },
  59. '304': {
  60. data: [
  61. [1699907700, [{count: 12}]],
  62. [1699908000, [{count: 13}]],
  63. ],
  64. },
  65. },
  66. });
  67. const {result} = renderHook(
  68. ({filters, fields, topEvents, yAxis}) =>
  69. useSpanMetricsTopNSeries({
  70. search: MutableSearch.fromQueryObject(filters),
  71. fields,
  72. topEvents,
  73. yAxis,
  74. }),
  75. {
  76. wrapper: Wrapper,
  77. initialProps: {
  78. filters: {
  79. 'span.group': '221aa7ebd216',
  80. },
  81. fields: ['span.status_code' as const, 'count()' as const],
  82. topEvents: 5,
  83. yAxis: ['count()'] as SpanMetricsProperty[],
  84. },
  85. }
  86. );
  87. expect(eventsRequest).toHaveBeenCalledWith(
  88. '/organizations/org-slug/events-stats/',
  89. expect.objectContaining({
  90. method: 'GET',
  91. query: expect.objectContaining({
  92. query: `span.group:221aa7ebd216`,
  93. dataset: 'spansMetrics',
  94. statsPeriod: '10d',
  95. referrer: 'span-metrics-top-n-series',
  96. interval: '30m',
  97. topEvents: '5',
  98. field: ['span.status_code', 'count()'],
  99. yAxis: 'count()',
  100. }),
  101. })
  102. );
  103. await waitFor(() => expect(result.current.isLoading).toEqual(false));
  104. expect(result.current.data).toEqual({
  105. '200': {
  106. data: [
  107. {name: '2023-11-13T20:35:00+00:00', value: 117},
  108. {name: '2023-11-13T20:40:00+00:00', value: 199},
  109. ],
  110. seriesName: '200',
  111. },
  112. '304': {
  113. data: [
  114. {name: '2023-11-13T20:35:00+00:00', value: 12},
  115. {name: '2023-11-13T20:40:00+00:00', value: 13},
  116. ],
  117. seriesName: '304',
  118. },
  119. });
  120. });
  121. });