useTraces.spec.tsx 3.2 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} 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. }),
  92. ],
  93. });
  94. const {result} = renderHook(useTraces, {
  95. ...context,
  96. wrapper: createWrapper(organization),
  97. initialProps: {
  98. datetime: {
  99. end: null,
  100. period: '3d',
  101. start: null,
  102. utc: null,
  103. },
  104. limit: 10,
  105. query: 'foo:bar',
  106. },
  107. });
  108. await waitFor(() => result.current.isSuccess);
  109. expect(result.current.data).toEqual(body);
  110. });
  111. });