useSpanMetrics.spec.tsx 4.0 KB

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