useDiscover.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import type {ReactNode} from 'react';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {makeTestQueryClient} from 'sentry-test/queryClient';
  5. import {renderHook, waitFor} from 'sentry-test/reactTestingLibrary';
  6. import {QueryClientProvider} from 'sentry/utils/queryClient';
  7. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  8. import {useLocation} from 'sentry/utils/useLocation';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. import {useSpanMetrics} from 'sentry/views/starfish/queries/useDiscover';
  12. import type {SpanMetricsProperty} from 'sentry/views/starfish/types';
  13. jest.mock('sentry/utils/useLocation');
  14. jest.mock('sentry/utils/usePageFilters');
  15. jest.mock('sentry/utils/useOrganization');
  16. function Wrapper({children}: {children?: ReactNode}) {
  17. return (
  18. <QueryClientProvider client={makeTestQueryClient()}>{children}</QueryClientProvider>
  19. );
  20. }
  21. describe('useSpanMetrics', () => {
  22. const organization = OrganizationFixture();
  23. jest.mocked(usePageFilters).mockReturnValue({
  24. isReady: true,
  25. desyncedFilters: new Set(),
  26. pinnedFilters: new Set(),
  27. shouldPersist: true,
  28. selection: {
  29. datetime: {
  30. period: '10d',
  31. start: null,
  32. end: null,
  33. utc: false,
  34. },
  35. environments: [],
  36. projects: [],
  37. },
  38. });
  39. jest.mocked(useLocation).mockReturnValue(
  40. LocationFixture({
  41. query: {statsPeriod: '10d'},
  42. })
  43. );
  44. jest.mocked(useOrganization).mockReturnValue(organization);
  45. it('respects the `enabled` prop', () => {
  46. const eventsRequest = MockApiClient.addMockResponse({
  47. url: `/organizations/${organization.slug}/events/`,
  48. method: 'GET',
  49. body: {data: []},
  50. });
  51. const {result} = renderHook(
  52. ({fields, enabled}) => useSpanMetrics({fields, enabled}, 'span-metrics-series'),
  53. {
  54. wrapper: Wrapper,
  55. initialProps: {
  56. fields: ['spm()'] as SpanMetricsProperty[],
  57. enabled: false,
  58. },
  59. }
  60. );
  61. expect(result.current.isFetching).toEqual(false);
  62. expect(eventsRequest).not.toHaveBeenCalled();
  63. });
  64. it('queries for current selection', async () => {
  65. const eventsRequest = MockApiClient.addMockResponse({
  66. url: `/organizations/${organization.slug}/events/`,
  67. method: 'GET',
  68. body: {
  69. data: [
  70. {
  71. 'span.op': 'db',
  72. 'spm()': 1486.3201388888888,
  73. 'count()': 2140301,
  74. },
  75. ],
  76. },
  77. });
  78. const {result} = renderHook(
  79. ({filters, fields, sorts, limit, cursor, referrer}) =>
  80. useSpanMetrics(
  81. {
  82. search: MutableSearch.fromQueryObject(filters),
  83. fields,
  84. sorts,
  85. limit,
  86. cursor,
  87. },
  88. referrer
  89. ),
  90. {
  91. wrapper: Wrapper,
  92. initialProps: {
  93. filters: {
  94. 'span.group': '221aa7ebd216',
  95. transaction: '/api/details',
  96. release: '0.0.1',
  97. environment: undefined,
  98. },
  99. fields: ['spm()'] as SpanMetricsProperty[],
  100. sorts: [{field: 'spm()', kind: 'desc' as const}],
  101. limit: 10,
  102. referrer: 'api-spec',
  103. cursor: undefined,
  104. },
  105. }
  106. );
  107. expect(result.current.isLoading).toEqual(true);
  108. expect(eventsRequest).toHaveBeenCalledWith(
  109. '/organizations/org-slug/events/',
  110. expect.objectContaining({
  111. method: 'GET',
  112. query: {
  113. dataset: 'spansMetrics',
  114. environment: [],
  115. field: ['spm()'],
  116. per_page: 10,
  117. project: [],
  118. sort: '-spm()',
  119. query: `span.group:221aa7ebd216 transaction:/api/details release:0.0.1`,
  120. referrer: 'api-spec',
  121. statsPeriod: '10d',
  122. },
  123. })
  124. );
  125. await waitFor(() => expect(result.current.isLoading).toEqual(false));
  126. expect(result.current.data).toEqual([
  127. {
  128. 'span.op': 'db',
  129. 'spm()': 1486.3201388888888,
  130. 'count()': 2140301,
  131. },
  132. ]);
  133. });
  134. });