useSpanMetricsTopNSeries.spec.tsx 3.7 KB

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