useSpanMetrics.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {ReactNode} from 'react';
  2. import {Organization} 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 {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import {useSpanMetrics} from 'sentry/views/starfish/queries/useSpanMetrics';
  10. import {MetricsProperty} from 'sentry/views/starfish/types';
  11. jest.mock('sentry/utils/useLocation');
  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('useSpanMetrics', () => {
  20. const organization = Organization();
  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(useLocation).mockReturnValue({
  38. pathname: '',
  39. search: '',
  40. query: {statsPeriod: '10d'},
  41. hash: '',
  42. state: undefined,
  43. action: 'PUSH',
  44. key: '',
  45. });
  46. jest.mocked(useOrganization).mockReturnValue(organization);
  47. it('queries for current selection', async () => {
  48. const eventsRequest = MockApiClient.addMockResponse({
  49. url: `/organizations/${organization.slug}/events/`,
  50. method: 'GET',
  51. body: {
  52. data: [
  53. {
  54. 'span.op': 'db',
  55. 'spm()': 1486.3201388888888,
  56. 'count()': 2140301,
  57. },
  58. ],
  59. },
  60. });
  61. const {result, waitForNextUpdate} = reactHooks.renderHook(
  62. ({filters, fields}) => useSpanMetrics(filters, fields),
  63. {
  64. wrapper: Wrapper,
  65. initialProps: {
  66. filters: {
  67. 'span.group': '221aa7ebd216',
  68. transaction: '/api/details',
  69. release: '0.0.1',
  70. },
  71. fields: ['spm()'] as MetricsProperty[],
  72. },
  73. }
  74. );
  75. expect(result.current.isLoading).toEqual(true);
  76. expect(eventsRequest).toHaveBeenCalledWith(
  77. '/organizations/org-slug/events/',
  78. expect.objectContaining({
  79. method: 'GET',
  80. query: expect.objectContaining({
  81. query: `span.group:221aa7ebd216 transaction:/api/details release:0.0.1`,
  82. dataset: 'spansMetrics',
  83. statsPeriod: '10d',
  84. field: ['spm()'],
  85. }),
  86. })
  87. );
  88. await waitForNextUpdate();
  89. expect(result.current.isLoading).toEqual(false);
  90. expect(result.current.data).toEqual({
  91. 'span.op': 'db',
  92. 'spm()': 1486.3201388888888,
  93. 'count()': 2140301,
  94. });
  95. });
  96. });