useSpanList.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 {useSpanList} from 'sentry/views/starfish/queries/useSpanList';
  9. import {SpanMetricsQueryFilters} from 'sentry/views/starfish/types';
  10. jest.mock('sentry/utils/useLocation');
  11. jest.mock('sentry/utils/useOrganization');
  12. function wrapper({children}: {children?: ReactNode}) {
  13. return (
  14. <QueryClientProvider client={makeTestQueryClient()}>{children}</QueryClientProvider>
  15. );
  16. }
  17. describe('useSpanList', () => {
  18. const organization = Organization();
  19. jest.mocked(useOrganization).mockReturnValue(organization);
  20. it('queries for current selection', async () => {
  21. jest.mocked(useLocation).mockReturnValue({
  22. pathname: '',
  23. search: '',
  24. query: {
  25. statsPeriod: '10d',
  26. },
  27. hash: '',
  28. state: undefined,
  29. action: 'PUSH',
  30. key: '',
  31. });
  32. const eventsRequest = MockApiClient.addMockResponse({
  33. url: `/organizations/${organization.slug}/events/`,
  34. method: 'GET',
  35. body: {
  36. data: [
  37. {
  38. 'span.description': 'SELECT .. FROM sentry_apitoken;',
  39. 'spm()': 44,
  40. },
  41. ],
  42. },
  43. });
  44. const {result, waitForNextUpdate} = reactHooks.renderHook(
  45. ({filters, sorts, limit}) => useSpanList(filters, sorts, limit),
  46. {
  47. wrapper,
  48. initialProps: {
  49. filters: {
  50. 'span.module': 'db',
  51. 'span.action': 'SELECT',
  52. } as SpanMetricsQueryFilters,
  53. sorts: [],
  54. limit: 11,
  55. },
  56. }
  57. );
  58. expect(result.current.isLoading).toEqual(true);
  59. expect(eventsRequest).toHaveBeenCalledWith(
  60. '/organizations/org-slug/events/',
  61. expect.objectContaining({
  62. method: 'GET',
  63. query: {
  64. query: `span.module:db span.action:SELECT has:span.description`,
  65. dataset: 'spansMetrics',
  66. statsPeriod: '10d',
  67. environment: [],
  68. project: [],
  69. referrer: 'api.starfish.use-span-list',
  70. field: [
  71. 'project.id',
  72. 'span.op',
  73. 'span.group',
  74. 'span.description',
  75. 'span.domain',
  76. 'spm()',
  77. 'sum(span.self_time)',
  78. 'avg(span.self_time)',
  79. 'http_error_count()',
  80. 'time_spent_percentage()',
  81. ],
  82. per_page: 11,
  83. },
  84. })
  85. );
  86. await waitForNextUpdate();
  87. expect(result.current.isLoading).toEqual(false);
  88. expect(result.current.data).toEqual([
  89. {
  90. 'span.description': 'SELECT .. FROM sentry_apitoken;',
  91. 'spm()': 44,
  92. },
  93. ]);
  94. });
  95. });