useTraceTree.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {renderHook, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import type {
  4. TraceMeta,
  5. TraceSplitResults,
  6. } from 'sentry/utils/performance/quickTrace/types';
  7. import type {UseApiQueryResult} from 'sentry/utils/queryClient';
  8. import * as useApi from 'sentry/utils/useApi';
  9. import * as useOrganization from 'sentry/utils/useOrganization';
  10. import type {TraceTree} from '../traceModels/traceTree';
  11. import {makeTraceError, makeTransaction} from '../traceModels/traceTreeTestUtils';
  12. import type {TraceMetaQueryResults} from './useTraceMeta';
  13. import {useTraceTree} from './useTraceTree';
  14. const getMockedTraceResults = (
  15. status: string,
  16. data: TraceSplitResults<TraceTree.Transaction> | undefined = undefined
  17. ) =>
  18. ({
  19. status,
  20. data,
  21. }) as UseApiQueryResult<TraceSplitResults<TraceTree.Transaction> | undefined, any>;
  22. const getMockedMetaResults = (status: string, data: TraceMeta | undefined = undefined) =>
  23. ({
  24. status,
  25. data,
  26. }) as TraceMetaQueryResults;
  27. const organization = OrganizationFixture();
  28. describe('useTraceTree', () => {
  29. beforeEach(function () {
  30. jest.restoreAllMocks();
  31. const api = new MockApiClient();
  32. jest.spyOn(useApi, 'default').mockReturnValue(api);
  33. jest.spyOn(useOrganization, 'default').mockReturnValue(organization);
  34. });
  35. it('returns tree for error case', async () => {
  36. const {result} = renderHook(() =>
  37. useTraceTree({
  38. trace: getMockedTraceResults('error'),
  39. meta: getMockedMetaResults('error'),
  40. traceSlug: 'test-trace',
  41. replay: null,
  42. })
  43. );
  44. await waitFor(() => {
  45. expect(result.current.type).toBe('error');
  46. });
  47. });
  48. it('returns tree for loading case', async () => {
  49. const {result} = renderHook(() =>
  50. useTraceTree({
  51. trace: getMockedTraceResults('pending'),
  52. meta: getMockedMetaResults('pending'),
  53. traceSlug: 'test-trace',
  54. replay: null,
  55. })
  56. );
  57. await waitFor(() => {
  58. expect(result.current.type).toBe('loading');
  59. });
  60. });
  61. it('returns tree for empty success case', async () => {
  62. const {result} = renderHook(() =>
  63. useTraceTree({
  64. trace: getMockedTraceResults('success', {
  65. transactions: [],
  66. orphan_errors: [],
  67. }),
  68. meta: getMockedMetaResults('success', {
  69. errors: 1,
  70. performance_issues: 2,
  71. projects: 1,
  72. transactions: 1,
  73. transaction_child_count_map: {
  74. '1': 1,
  75. },
  76. span_count: 0,
  77. span_count_map: {},
  78. }),
  79. traceSlug: 'test-trace',
  80. replay: null,
  81. })
  82. );
  83. await waitFor(() => {
  84. expect(result.current.type).toBe('empty');
  85. });
  86. });
  87. it('returns tree for non-empty success case', async () => {
  88. const mockedTrace = {
  89. transactions: [
  90. makeTransaction({
  91. start_timestamp: 0,
  92. timestamp: 1,
  93. transaction: 'transaction1',
  94. }),
  95. makeTransaction({
  96. start_timestamp: 1,
  97. timestamp: 2,
  98. transaction: 'transaction2',
  99. }),
  100. makeTransaction({
  101. start_timestamp: 0,
  102. timestamp: 1,
  103. transaction: 'transaction1',
  104. }),
  105. makeTransaction({
  106. start_timestamp: 1,
  107. timestamp: 2,
  108. transaction: 'transaction2',
  109. }),
  110. ],
  111. orphan_errors: [
  112. makeTraceError({
  113. title: 'error1',
  114. level: 'error',
  115. }),
  116. makeTraceError({
  117. title: 'error2',
  118. level: 'error',
  119. }),
  120. ],
  121. };
  122. const mockedMeta = {
  123. errors: 1,
  124. performance_issues: 2,
  125. projects: 1,
  126. transactions: 1,
  127. transaction_child_count_map: {
  128. '1': 1,
  129. },
  130. span_count: 0,
  131. span_count_map: {},
  132. };
  133. const {result} = renderHook(() =>
  134. useTraceTree({
  135. trace: getMockedTraceResults('success', mockedTrace),
  136. meta: getMockedMetaResults('success', mockedMeta),
  137. traceSlug: 'test-trace',
  138. replay: null,
  139. })
  140. );
  141. await waitFor(() => {
  142. expect(result.current.type).toBe('trace');
  143. });
  144. expect(result.current.list).toHaveLength(7);
  145. });
  146. });