useSpanSamples.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 usePageFilters from 'sentry/utils/usePageFilters';
  8. import {useSpanSamples} from 'sentry/views/insights/http/queries/useSpanSamples';
  9. import {SpanIndexedField, type SpanIndexedProperty} from 'sentry/views/insights/types';
  10. import {OrganizationContext} from 'sentry/views/organizationContext';
  11. jest.mock('sentry/utils/usePageFilters');
  12. describe('useSpanSamples', () => {
  13. const organization = OrganizationFixture();
  14. function Wrapper({children}: {children?: ReactNode}) {
  15. return (
  16. <QueryClientProvider client={makeTestQueryClient()}>
  17. <OrganizationContext.Provider value={organization}>
  18. {children}
  19. </OrganizationContext.Provider>
  20. </QueryClientProvider>
  21. );
  22. }
  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. beforeEach(() => {
  40. jest.clearAllMocks();
  41. });
  42. it('respects the `enabled` prop', () => {
  43. const request = MockApiClient.addMockResponse({
  44. url: `/api/0/organizations/${organization.slug}/spans-samples/`,
  45. method: 'GET',
  46. body: {data: []},
  47. });
  48. const {result} = renderHook(
  49. ({fields, enabled}) => useSpanSamples({fields, enabled}),
  50. {
  51. wrapper: Wrapper,
  52. initialProps: {
  53. fields: [
  54. SpanIndexedField.TRANSACTION_ID,
  55. SpanIndexedField.ID,
  56. ] as SpanIndexedProperty[],
  57. enabled: false,
  58. },
  59. }
  60. );
  61. expect(result.current.isFetching).toEqual(false);
  62. expect(request).not.toHaveBeenCalled();
  63. });
  64. it('queries for current selection', async () => {
  65. const request = MockApiClient.addMockResponse({
  66. url: `/api/0/organizations/${organization.slug}/spans-samples/`,
  67. method: 'GET',
  68. body: {
  69. data: [
  70. {
  71. 'transaction.id': '7663aab8a',
  72. 'span.id': '3aab8a77fe231',
  73. },
  74. ],
  75. },
  76. });
  77. const {result} = renderHook(
  78. ({filters, fields, referrer}) =>
  79. useSpanSamples({
  80. search: MutableSearch.fromQueryObject(filters),
  81. fields,
  82. referrer,
  83. min: 100,
  84. max: 900,
  85. }),
  86. {
  87. wrapper: Wrapper,
  88. initialProps: {
  89. filters: {
  90. 'span.group': '221aa7ebd216',
  91. release: '0.0.1',
  92. environment: undefined,
  93. },
  94. fields: [
  95. SpanIndexedField.TRANSACTION_ID,
  96. SpanIndexedField.ID,
  97. ] as SpanIndexedProperty[],
  98. referrer: 'api-spec',
  99. },
  100. }
  101. );
  102. expect(result.current.isPending).toEqual(true);
  103. expect(request).toHaveBeenCalledWith(
  104. '/api/0/organizations/org-slug/spans-samples/',
  105. expect.objectContaining({
  106. method: 'GET',
  107. query: {
  108. additionalFields: ['transaction.id', 'span_id'],
  109. project: [],
  110. query: `span.group:221aa7ebd216 release:0.0.1`,
  111. referrer: 'api-spec',
  112. statsPeriod: '10d',
  113. lowerBound: 100,
  114. firstBound: 300,
  115. secondBound: 600,
  116. upperBound: 900,
  117. utc: false,
  118. },
  119. })
  120. );
  121. await waitFor(() => expect(result.current.isPending).toEqual(false));
  122. expect(result.current.data).toEqual([
  123. {
  124. 'transaction.id': '7663aab8a',
  125. 'span.id': '3aab8a77fe231',
  126. },
  127. ]);
  128. });
  129. });