useTraceMeta.spec.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import {QueryClientProvider} from '@tanstack/react-query';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {makeTestQueryClient} from 'sentry-test/queryClient';
  4. import {renderHook, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import * as useOrganization from 'sentry/utils/useOrganization';
  6. import type {ReplayTrace} from 'sentry/views/replays/detail/trace/useReplayTraces';
  7. import {useTraceMeta} from './useTraceMeta';
  8. const organization = OrganizationFixture();
  9. const queryClient = makeTestQueryClient();
  10. const mockedReplayTraces: ReplayTrace[] = [
  11. {
  12. traceSlug: 'slug1',
  13. timestamp: 1,
  14. },
  15. {
  16. traceSlug: 'slug2',
  17. timestamp: 2,
  18. },
  19. {
  20. traceSlug: 'slug3',
  21. timestamp: 3,
  22. },
  23. ];
  24. describe('useTraceMeta', () => {
  25. beforeEach(function () {
  26. queryClient.clear();
  27. jest.clearAllMocks();
  28. jest.spyOn(useOrganization, 'default').mockReturnValue(organization);
  29. });
  30. it('Returns merged metaResults', async () => {
  31. // Mock the API calls
  32. MockApiClient.addMockResponse({
  33. method: 'GET',
  34. url: '/organizations/org-slug/events-trace-meta/slug1/',
  35. body: {
  36. errors: 1,
  37. performance_issues: 1,
  38. projects: 1,
  39. transactions: 1,
  40. transaction_child_count_map: [{'transaction.id': '1', count: 1}],
  41. },
  42. });
  43. MockApiClient.addMockResponse({
  44. method: 'GET',
  45. url: '/organizations/org-slug/events-trace-meta/slug2/',
  46. body: {
  47. errors: 1,
  48. performance_issues: 1,
  49. projects: 1,
  50. transactions: 1,
  51. transaction_child_count_map: [{'transaction.id': '2', count: 2}],
  52. },
  53. });
  54. MockApiClient.addMockResponse({
  55. method: 'GET',
  56. url: '/organizations/org-slug/events-trace-meta/slug3/',
  57. body: {
  58. errors: 1,
  59. performance_issues: 1,
  60. projects: 1,
  61. transactions: 1,
  62. transaction_child_count_map: [],
  63. },
  64. });
  65. const wrapper = ({children}: {children: React.ReactNode}) => (
  66. <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  67. );
  68. const {result} = renderHook(() => useTraceMeta(mockedReplayTraces), {wrapper});
  69. expect(result.current).toEqual({
  70. data: undefined,
  71. errors: [],
  72. isLoading: true,
  73. status: 'loading',
  74. });
  75. await waitFor(() => expect(result.current.isLoading).toBe(false));
  76. expect(result.current).toEqual({
  77. data: {
  78. errors: 3,
  79. performance_issues: 3,
  80. projects: 1,
  81. transactions: 3,
  82. transactiontoSpanChildrenCount: {
  83. '1': 1,
  84. '2': 2,
  85. },
  86. },
  87. errors: [],
  88. isLoading: false,
  89. status: 'success',
  90. });
  91. });
  92. it('Collects errors from rejected api calls', async () => {
  93. // Mock the API calls
  94. const mockRequest1 = MockApiClient.addMockResponse({
  95. method: 'GET',
  96. url: '/organizations/org-slug/events-trace-meta/slug1/',
  97. statusCode: 400,
  98. });
  99. const mockRequest2 = MockApiClient.addMockResponse({
  100. method: 'GET',
  101. url: '/organizations/org-slug/events-trace-meta/slug2/',
  102. statusCode: 400,
  103. });
  104. const mockRequest3 = MockApiClient.addMockResponse({
  105. method: 'GET',
  106. url: '/organizations/org-slug/events-trace-meta/slug3/',
  107. statusCode: 400,
  108. });
  109. const wrapper = ({children}: {children: React.ReactNode}) => (
  110. <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  111. );
  112. const {result} = renderHook(() => useTraceMeta(mockedReplayTraces), {wrapper});
  113. expect(result.current).toEqual({
  114. data: undefined,
  115. errors: [],
  116. isLoading: true,
  117. status: 'loading',
  118. });
  119. await waitFor(() => expect(result.current.isLoading).toBe(false));
  120. expect(result.current).toEqual({
  121. data: {
  122. errors: 0,
  123. performance_issues: 0,
  124. projects: 0,
  125. transactions: 0,
  126. transactiontoSpanChildrenCount: {},
  127. },
  128. errors: [expect.any(Error), expect.any(Error), expect.any(Error)],
  129. isLoading: false,
  130. status: 'success',
  131. });
  132. expect(mockRequest1).toHaveBeenCalledTimes(1);
  133. expect(mockRequest2).toHaveBeenCalledTimes(1);
  134. expect(mockRequest3).toHaveBeenCalledTimes(1);
  135. });
  136. it('Accumulates metaResults and collects errors from rejected api calls', async () => {
  137. // Mock the API calls
  138. const mockRequest1 = MockApiClient.addMockResponse({
  139. method: 'GET',
  140. url: '/organizations/org-slug/events-trace-meta/slug1/',
  141. statusCode: 400,
  142. });
  143. const mockRequest2 = MockApiClient.addMockResponse({
  144. method: 'GET',
  145. url: '/organizations/org-slug/events-trace-meta/slug2/',
  146. body: {
  147. errors: 1,
  148. performance_issues: 1,
  149. projects: 1,
  150. transactions: 1,
  151. transaction_child_count_map: [],
  152. },
  153. });
  154. const mockRequest3 = MockApiClient.addMockResponse({
  155. method: 'GET',
  156. url: '/organizations/org-slug/events-trace-meta/slug3/',
  157. body: {
  158. errors: 1,
  159. performance_issues: 1,
  160. projects: 1,
  161. transactions: 1,
  162. transaction_child_count_map: [],
  163. },
  164. });
  165. const wrapper = ({children}: {children: React.ReactNode}) => (
  166. <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  167. );
  168. const {result} = renderHook(() => useTraceMeta(mockedReplayTraces), {wrapper});
  169. expect(result.current).toEqual({
  170. data: undefined,
  171. errors: [],
  172. isLoading: true,
  173. status: 'loading',
  174. });
  175. await waitFor(() => expect(result.current.isLoading).toBe(false));
  176. expect(result.current).toEqual({
  177. data: {
  178. errors: 2,
  179. performance_issues: 2,
  180. projects: 1,
  181. transactions: 2,
  182. transactiontoSpanChildrenCount: {},
  183. },
  184. errors: [expect.any(Error)],
  185. isLoading: false,
  186. status: 'success',
  187. });
  188. expect(mockRequest1).toHaveBeenCalledTimes(1);
  189. expect(mockRequest2).toHaveBeenCalledTimes(1);
  190. expect(mockRequest3).toHaveBeenCalledTimes(1);
  191. });
  192. });