useTraceSpans.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {makeTestQueryClient} from 'sentry-test/queryClient';
  5. import {act, renderHook, waitFor} from 'sentry-test/reactTestingLibrary';
  6. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  7. import ProjectsStore from 'sentry/stores/projectsStore';
  8. import type {Organization} from 'sentry/types/organization';
  9. import {QueryClientProvider} from 'sentry/utils/queryClient';
  10. import {OrganizationContext} from 'sentry/views/organizationContext';
  11. import type {TraceResult} from 'sentry/views/traces/hooks/useTraces';
  12. import type {SpanResults} from 'sentry/views/traces/hooks/useTraceSpans';
  13. import {useTraceSpans} from 'sentry/views/traces/hooks/useTraceSpans';
  14. function createTraceResult(trace?: Partial<TraceResult>): TraceResult {
  15. return {
  16. breakdowns: [],
  17. duration: 333,
  18. end: 456,
  19. matchingSpans: 1,
  20. name: 'name',
  21. numErrors: 1,
  22. numOccurrences: 1,
  23. numSpans: 2,
  24. project: 'project',
  25. slices: 10,
  26. start: 123,
  27. trace: '00000000000000000000000000000000',
  28. ...trace,
  29. };
  30. }
  31. function createWrapper(organization: Organization) {
  32. return function ({children}: {children?: React.ReactNode}) {
  33. return (
  34. <QueryClientProvider client={makeTestQueryClient()}>
  35. <OrganizationContext.Provider value={organization}>
  36. {children}
  37. </OrganizationContext.Provider>
  38. </QueryClientProvider>
  39. );
  40. };
  41. }
  42. describe('useTraceSpans', function () {
  43. const project = ProjectFixture();
  44. const organization = OrganizationFixture();
  45. const context = initializeOrg({
  46. organization,
  47. projects: [project],
  48. router: {
  49. location: {
  50. pathname: '/organizations/org-slug/issues/',
  51. query: {project: project.id},
  52. },
  53. params: {},
  54. },
  55. });
  56. beforeEach(function () {
  57. MockApiClient.clearMockResponses();
  58. act(() => {
  59. ProjectsStore.loadInitialData([project]);
  60. PageFiltersStore.init();
  61. PageFiltersStore.onInitializeUrlState(
  62. {
  63. projects: [project].map(p => parseInt(p.id, 10)),
  64. environments: [],
  65. datetime: {
  66. period: '3d',
  67. start: null,
  68. end: null,
  69. utc: null,
  70. },
  71. },
  72. new Set()
  73. );
  74. });
  75. });
  76. it('handles querying the api', async function () {
  77. const trace = createTraceResult();
  78. const body: SpanResults<'id'> = {
  79. data: [{id: '0000000000000000'}],
  80. meta: {},
  81. };
  82. MockApiClient.addMockResponse({
  83. url: `/organizations/${organization.slug}/trace/${trace.trace}/spans/`,
  84. body,
  85. match: [
  86. MockApiClient.matchQuery({
  87. project: [parseInt(project.id, 10)],
  88. field: ['id'],
  89. maxSpansPerTrace: 10,
  90. query: 'foo:bar',
  91. statsPeriod: '3d',
  92. mri: 'd:transactions/duration@millisecond',
  93. metricsMax: '456',
  94. metricsMin: '123',
  95. metricsOp: 'sum',
  96. metricsQuery: 'foo:bar',
  97. }),
  98. ],
  99. });
  100. const {result} = renderHook(useTraceSpans, {
  101. ...context,
  102. wrapper: createWrapper(organization),
  103. initialProps: {
  104. fields: ['id'],
  105. trace,
  106. datetime: {
  107. end: null,
  108. period: '3d',
  109. start: null,
  110. utc: null,
  111. },
  112. query: 'foo:bar',
  113. mri: 'd:transactions/duration@millisecond',
  114. metricsMax: '456',
  115. metricsMin: '123',
  116. metricsOp: 'sum',
  117. metricsQuery: 'foo:bar',
  118. },
  119. });
  120. await waitFor(() => result.current.isSuccess);
  121. expect(result.current.data).toEqual(body);
  122. });
  123. });