useTraceTree.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. transactiontoSpanChildrenCount: {
  74. '1': 1,
  75. },
  76. }),
  77. traceSlug: 'test-trace',
  78. replay: null,
  79. })
  80. );
  81. await waitFor(() => {
  82. expect(result.current.type).toBe('empty');
  83. });
  84. });
  85. it('returns tree for non-empty success case', async () => {
  86. const mockedTrace = {
  87. transactions: [
  88. makeTransaction({
  89. start_timestamp: 0,
  90. timestamp: 1,
  91. transaction: 'transaction1',
  92. }),
  93. makeTransaction({
  94. start_timestamp: 1,
  95. timestamp: 2,
  96. transaction: 'transaction2',
  97. }),
  98. makeTransaction({
  99. start_timestamp: 0,
  100. timestamp: 1,
  101. transaction: 'transaction1',
  102. }),
  103. makeTransaction({
  104. start_timestamp: 1,
  105. timestamp: 2,
  106. transaction: 'transaction2',
  107. }),
  108. ],
  109. orphan_errors: [
  110. makeTraceError({
  111. title: 'error1',
  112. level: 'error',
  113. }),
  114. makeTraceError({
  115. title: 'error2',
  116. level: 'error',
  117. }),
  118. ],
  119. };
  120. const mockedMeta = {
  121. errors: 1,
  122. performance_issues: 2,
  123. projects: 1,
  124. transactions: 1,
  125. transactiontoSpanChildrenCount: {
  126. '1': 1,
  127. },
  128. };
  129. const {result} = renderHook(() =>
  130. useTraceTree({
  131. trace: getMockedTraceResults('success', mockedTrace),
  132. meta: getMockedMetaResults('success', mockedMeta),
  133. traceSlug: 'test-trace',
  134. replay: null,
  135. })
  136. );
  137. await waitFor(() => {
  138. expect(result.current.type).toBe('trace');
  139. expect(result.current.list).toHaveLength(7);
  140. });
  141. });
  142. });