useSpanMetrics.spec.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {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 {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('queries for current selection', async () => {
  45. const eventsRequest = MockApiClient.addMockResponse({
  46. url: `/organizations/${organization.slug}/events/`,
  47. method: 'GET',
  48. body: {
  49. data: [
  50. {
  51. 'span.op': 'db',
  52. 'spm()': 1486.3201388888888,
  53. 'count()': 2140301,
  54. },
  55. ],
  56. },
  57. });
  58. const {result, waitForNextUpdate} = reactHooks.renderHook(
  59. ({filters, fields, sorts, limit, cursor, referrer}) =>
  60. useSpanMetrics({filters, fields, sorts, limit, cursor, referrer}),
  61. {
  62. wrapper: Wrapper,
  63. initialProps: {
  64. filters: {
  65. 'span.group': '221aa7ebd216',
  66. transaction: '/api/details',
  67. release: '0.0.1',
  68. },
  69. fields: ['spm()'] as MetricsProperty[],
  70. sorts: [{field: 'spm()', kind: 'desc' as const}],
  71. limit: 10,
  72. referrer: 'api-spec',
  73. cursor: undefined,
  74. },
  75. }
  76. );
  77. expect(result.current.isLoading).toEqual(true);
  78. expect(eventsRequest).toHaveBeenCalledWith(
  79. '/organizations/org-slug/events/',
  80. expect.objectContaining({
  81. method: 'GET',
  82. query: {
  83. dataset: 'spansMetrics',
  84. environment: [],
  85. field: ['spm()'],
  86. per_page: 10,
  87. project: [],
  88. sort: '-spm()',
  89. query: `span.group:221aa7ebd216 transaction:/api/details release:0.0.1`,
  90. referrer: 'api-spec',
  91. statsPeriod: '10d',
  92. },
  93. })
  94. );
  95. await waitForNextUpdate();
  96. expect(result.current.isLoading).toEqual(false);
  97. expect(result.current.data).toEqual([
  98. {
  99. 'span.op': 'db',
  100. 'spm()': 1486.3201388888888,
  101. 'count()': 2140301,
  102. },
  103. ]);
  104. });
  105. });