useIndexedSpans.spec.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 {useIndexedSpans} from 'sentry/views/starfish/queries/useIndexedSpans';
  12. import type {IndexedProperty} 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('useIndexedSpans', () => {
  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. beforeEach(() => {
  46. jest.clearAllMocks();
  47. });
  48. it('respects the `enabled` prop', () => {
  49. const eventsRequest = MockApiClient.addMockResponse({
  50. url: `/organizations/${organization.slug}/events/`,
  51. method: 'GET',
  52. body: {data: []},
  53. });
  54. const {result} = renderHook(
  55. ({fields, enabled}) => useIndexedSpans({fields, enabled}),
  56. {
  57. wrapper: Wrapper,
  58. initialProps: {
  59. fields: ['span.description'] as IndexedProperty[],
  60. enabled: false,
  61. },
  62. }
  63. );
  64. expect(result.current.isFetching).toEqual(false);
  65. expect(eventsRequest).not.toHaveBeenCalled();
  66. });
  67. it('queries for current selection', async () => {
  68. const eventsRequest = MockApiClient.addMockResponse({
  69. url: `/organizations/${organization.slug}/events/`,
  70. method: 'GET',
  71. body: {
  72. data: [
  73. {
  74. 'span.group': '221aa7ebd216',
  75. 'span.op': 'db',
  76. 'span.description': 'SELECT * FROM users;',
  77. },
  78. ],
  79. meta: {
  80. fields: {
  81. 'span.description': 'string',
  82. 'span.op': 'string',
  83. 'span.group': 'string',
  84. },
  85. },
  86. },
  87. });
  88. const {result} = renderHook(
  89. ({filters, fields, sorts, limit, cursor, referrer}) =>
  90. useIndexedSpans({
  91. search: MutableSearch.fromQueryObject(filters),
  92. fields,
  93. sorts,
  94. limit,
  95. cursor,
  96. referrer,
  97. }),
  98. {
  99. wrapper: Wrapper,
  100. initialProps: {
  101. filters: {
  102. 'span.group': '221aa7ebd216',
  103. 'measurements.inp': ['<50', '>0'],
  104. transaction: '/api/details',
  105. release: '0.0.1',
  106. },
  107. fields: ['span.op', 'span.group', 'span.description'] as IndexedProperty[],
  108. sorts: [{field: 'span.group', kind: 'desc' as const}],
  109. limit: 10,
  110. referrer: 'api-spec',
  111. cursor: undefined,
  112. },
  113. }
  114. );
  115. expect(result.current.isLoading).toEqual(true);
  116. expect(eventsRequest).toHaveBeenCalledWith(
  117. '/organizations/org-slug/events/',
  118. expect.objectContaining({
  119. method: 'GET',
  120. query: {
  121. dataset: 'spansIndexed',
  122. environment: [],
  123. field: ['span.op', 'span.group', 'span.description'],
  124. per_page: 10,
  125. project: [],
  126. sort: '-span.group',
  127. query: `span.group:221aa7ebd216 measurements.inp:<50 measurements.inp:>0 transaction:/api/details release:0.0.1`,
  128. referrer: 'api-spec',
  129. statsPeriod: '10d',
  130. },
  131. })
  132. );
  133. await waitFor(() => expect(result.current.isLoading).toEqual(false));
  134. expect(result.current.data).toEqual([
  135. {
  136. 'span.group': '221aa7ebd216',
  137. 'span.op': 'db',
  138. 'span.description': 'SELECT * FROM users;',
  139. },
  140. ]);
  141. });
  142. });