useSpanMetrics.spec.tsx 3.8 KB

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