useSpanSamples.spec.tsx 3.9 KB

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