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