useIndexedSpans.spec.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 usePageFilters from 'sentry/utils/usePageFilters';
  10. import {OrganizationContext} from 'sentry/views/organizationContext';
  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. function Wrapper({children}: {children?: ReactNode}) {
  16. return (
  17. <QueryClientProvider client={makeTestQueryClient()}>
  18. <OrganizationContext.Provider value={OrganizationFixture()}>
  19. {children}
  20. </OrganizationContext.Provider>
  21. </QueryClientProvider>
  22. );
  23. }
  24. describe('useIndexedSpans', () => {
  25. const organization = OrganizationFixture();
  26. jest.mocked(usePageFilters).mockReturnValue({
  27. isReady: true,
  28. desyncedFilters: new Set(),
  29. pinnedFilters: new Set(),
  30. shouldPersist: true,
  31. selection: {
  32. datetime: {
  33. period: '10d',
  34. start: null,
  35. end: null,
  36. utc: false,
  37. },
  38. environments: [],
  39. projects: [],
  40. },
  41. });
  42. jest.mocked(useLocation).mockReturnValue(
  43. LocationFixture({
  44. query: {statsPeriod: '10d'},
  45. })
  46. );
  47. beforeEach(() => {
  48. jest.clearAllMocks();
  49. });
  50. it('respects the `enabled` prop', () => {
  51. const eventsRequest = MockApiClient.addMockResponse({
  52. url: `/organizations/${organization.slug}/events/`,
  53. method: 'GET',
  54. body: {data: []},
  55. });
  56. const {result} = renderHook(
  57. ({fields, enabled}) => useIndexedSpans({fields, enabled}),
  58. {
  59. wrapper: Wrapper,
  60. initialProps: {
  61. fields: ['span.description'] as IndexedProperty[],
  62. enabled: false,
  63. },
  64. }
  65. );
  66. expect(result.current.isFetching).toEqual(false);
  67. expect(eventsRequest).not.toHaveBeenCalled();
  68. });
  69. it('queries for current selection', async () => {
  70. const eventsRequest = MockApiClient.addMockResponse({
  71. url: `/organizations/${organization.slug}/events/`,
  72. method: 'GET',
  73. body: {
  74. data: [
  75. {
  76. 'span.group': '221aa7ebd216',
  77. 'span.op': 'db',
  78. 'span.description': 'SELECT * FROM users;',
  79. },
  80. ],
  81. meta: {
  82. fields: {
  83. 'span.description': 'string',
  84. 'span.op': 'string',
  85. 'span.group': 'string',
  86. },
  87. },
  88. },
  89. });
  90. const {result} = renderHook(
  91. ({filters, fields, sorts, limit, cursor, referrer}) =>
  92. useIndexedSpans({
  93. search: MutableSearch.fromQueryObject(filters),
  94. fields,
  95. sorts,
  96. limit,
  97. cursor,
  98. referrer,
  99. }),
  100. {
  101. wrapper: Wrapper,
  102. initialProps: {
  103. filters: {
  104. 'span.group': '221aa7ebd216',
  105. 'measurements.inp': ['<50', '>0'],
  106. transaction: '/api/details',
  107. release: '0.0.1',
  108. },
  109. fields: ['span.op', 'span.group', 'span.description'] as IndexedProperty[],
  110. sorts: [{field: 'span.group', kind: 'desc' as const}],
  111. limit: 10,
  112. referrer: 'api-spec',
  113. cursor: undefined,
  114. },
  115. }
  116. );
  117. expect(result.current.isLoading).toEqual(true);
  118. expect(eventsRequest).toHaveBeenCalledWith(
  119. '/organizations/org-slug/events/',
  120. expect.objectContaining({
  121. method: 'GET',
  122. query: {
  123. dataset: 'spansIndexed',
  124. environment: [],
  125. field: ['span.op', 'span.group', 'span.description'],
  126. per_page: 10,
  127. project: [],
  128. sort: '-span.group',
  129. query: `span.group:221aa7ebd216 measurements.inp:<50 measurements.inp:>0 transaction:/api/details release:0.0.1`,
  130. referrer: 'api-spec',
  131. statsPeriod: '10d',
  132. },
  133. })
  134. );
  135. await waitFor(() => expect(result.current.isLoading).toEqual(false));
  136. expect(result.current.data).toEqual([
  137. {
  138. 'span.group': '221aa7ebd216',
  139. 'span.op': 'db',
  140. 'span.description': 'SELECT * FROM users;',
  141. },
  142. ]);
  143. });
  144. });