useTraces.spec.tsx 3.5 KB

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