useTraces.spec.tsx 3.1 KB

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