useDiscover.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 {useSpanMetrics} from 'sentry/views/starfish/queries/useDiscover';
  12. import type {SpanMetricsProperty} from 'sentry/views/starfish/types';
  13. jest.mock('sentry/utils/useLocation');
  14. jest.mock('sentry/utils/usePageFilters');
  15. describe('useSpanMetrics', () => {
  16. const organization = OrganizationFixture();
  17. function Wrapper({children}: {children?: ReactNode}) {
  18. return (
  19. <QueryClientProvider client={makeTestQueryClient()}>
  20. <OrganizationContext.Provider value={organization}>
  21. {children}
  22. </OrganizationContext.Provider>
  23. </QueryClientProvider>
  24. );
  25. }
  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. it('respects the `enabled` prop', () => {
  48. const eventsRequest = MockApiClient.addMockResponse({
  49. url: `/organizations/${organization.slug}/events/`,
  50. method: 'GET',
  51. body: {data: []},
  52. });
  53. const {result} = renderHook(
  54. ({fields, enabled}) => useSpanMetrics({fields, enabled}, 'span-metrics-series'),
  55. {
  56. wrapper: Wrapper,
  57. initialProps: {
  58. fields: ['spm()'] as SpanMetricsProperty[],
  59. enabled: false,
  60. },
  61. }
  62. );
  63. expect(result.current.isFetching).toEqual(false);
  64. expect(eventsRequest).not.toHaveBeenCalled();
  65. });
  66. it('queries for current selection', async () => {
  67. const eventsRequest = MockApiClient.addMockResponse({
  68. url: `/organizations/${organization.slug}/events/`,
  69. method: 'GET',
  70. body: {
  71. data: [
  72. {
  73. 'span.op': 'db',
  74. 'spm()': 1486.3201388888888,
  75. 'count()': 2140301,
  76. },
  77. ],
  78. },
  79. });
  80. const {result} = renderHook(
  81. ({filters, fields, sorts, limit, cursor, referrer}) =>
  82. useSpanMetrics(
  83. {
  84. search: MutableSearch.fromQueryObject(filters),
  85. fields,
  86. sorts,
  87. limit,
  88. cursor,
  89. },
  90. referrer
  91. ),
  92. {
  93. wrapper: Wrapper,
  94. initialProps: {
  95. filters: {
  96. 'span.group': '221aa7ebd216',
  97. transaction: '/api/details',
  98. release: '0.0.1',
  99. environment: undefined,
  100. },
  101. fields: ['spm()'] as SpanMetricsProperty[],
  102. sorts: [{field: 'spm()', kind: 'desc' as const}],
  103. limit: 10,
  104. referrer: 'api-spec',
  105. cursor: undefined,
  106. },
  107. }
  108. );
  109. expect(result.current.isLoading).toEqual(true);
  110. expect(eventsRequest).toHaveBeenCalledWith(
  111. '/organizations/org-slug/events/',
  112. expect.objectContaining({
  113. method: 'GET',
  114. query: {
  115. dataset: 'spansMetrics',
  116. environment: [],
  117. field: ['spm()'],
  118. per_page: 10,
  119. project: [],
  120. sort: '-spm()',
  121. query: `span.group:221aa7ebd216 transaction:/api/details release:0.0.1`,
  122. referrer: 'api-spec',
  123. statsPeriod: '10d',
  124. },
  125. })
  126. );
  127. await waitFor(() => expect(result.current.isLoading).toEqual(false));
  128. expect(result.current.data).toEqual([
  129. {
  130. 'span.op': 'db',
  131. 'spm()': 1486.3201388888888,
  132. 'count()': 2140301,
  133. },
  134. ]);
  135. });
  136. });