useSpanSamples.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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} 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: [SpanIndexedField.TRANSACTION_ID, SpanIndexedField.ID],
  54. enabled: false,
  55. },
  56. }
  57. );
  58. expect(result.current.isFetching).toEqual(false);
  59. expect(request).not.toHaveBeenCalled();
  60. });
  61. it('queries for current selection', async () => {
  62. const request = MockApiClient.addMockResponse({
  63. url: `/api/0/organizations/${organization.slug}/spans-samples/`,
  64. method: 'GET',
  65. body: {
  66. data: [
  67. {
  68. 'transaction.id': '7663aab8a',
  69. 'span.id': '3aab8a77fe231',
  70. },
  71. ],
  72. },
  73. });
  74. const {result} = renderHook(
  75. ({filters, fields, referrer}) =>
  76. useSpanSamples({
  77. search: MutableSearch.fromQueryObject(filters),
  78. fields,
  79. referrer,
  80. min: 100,
  81. max: 900,
  82. }),
  83. {
  84. wrapper: Wrapper,
  85. initialProps: {
  86. filters: {
  87. 'span.group': '221aa7ebd216',
  88. release: '0.0.1',
  89. environment: undefined,
  90. },
  91. fields: [SpanIndexedField.TRANSACTION_ID, SpanIndexedField.ID],
  92. referrer: 'api-spec',
  93. },
  94. }
  95. );
  96. expect(result.current.isLoading).toEqual(true);
  97. expect(request).toHaveBeenCalledWith(
  98. '/api/0/organizations/org-slug/spans-samples/',
  99. expect.objectContaining({
  100. method: 'GET',
  101. query: {
  102. additionalFields: ['transaction.id', 'span_id'],
  103. project: [],
  104. query: `span.group:221aa7ebd216 release:0.0.1`,
  105. referrer: 'api-spec',
  106. statsPeriod: '10d',
  107. lowerBound: 100,
  108. firstBound: 300,
  109. secondBound: 600,
  110. upperBound: 900,
  111. utc: false,
  112. },
  113. })
  114. );
  115. await waitFor(() => expect(result.current.isLoading).toEqual(false));
  116. expect(result.current.data).toEqual([
  117. {
  118. 'transaction.id': '7663aab8a',
  119. 'span.id': '3aab8a77fe231',
  120. },
  121. ]);
  122. });
  123. });