useTraceSpans.spec.tsx 3.3 KB

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