useTraceMeta.spec.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 {useTraceMeta} from './useTraceMeta';
  7. const organization = OrganizationFixture();
  8. const queryClient = makeTestQueryClient();
  9. describe('useTraceMeta', () => {
  10. beforeEach(function () {
  11. queryClient.clear();
  12. jest.clearAllMocks();
  13. jest.spyOn(useOrganization, 'default').mockReturnValue(organization);
  14. });
  15. it('Returns merged metaResults', async () => {
  16. const traceSlugs = ['slug1', 'slug2', 'slug3'];
  17. // Mock the API calls
  18. MockApiClient.addMockResponse({
  19. method: 'GET',
  20. url: '/organizations/org-slug/events-trace-meta/slug1/',
  21. body: {
  22. errors: 1,
  23. performance_issues: 1,
  24. projects: 1,
  25. transactions: 1,
  26. },
  27. });
  28. MockApiClient.addMockResponse({
  29. method: 'GET',
  30. url: '/organizations/org-slug/events-trace-meta/slug2/',
  31. body: {
  32. errors: 1,
  33. performance_issues: 1,
  34. projects: 1,
  35. transactions: 1,
  36. },
  37. });
  38. MockApiClient.addMockResponse({
  39. method: 'GET',
  40. url: '/organizations/org-slug/events-trace-meta/slug3/',
  41. body: {
  42. errors: 1,
  43. performance_issues: 1,
  44. projects: 1,
  45. transactions: 1,
  46. },
  47. });
  48. const wrapper = ({children}: {children: React.ReactNode}) => (
  49. <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  50. );
  51. const {result} = renderHook(() => useTraceMeta(traceSlugs), {wrapper});
  52. expect(result.current).toEqual({
  53. data: undefined,
  54. errors: [],
  55. isLoading: true,
  56. });
  57. await waitFor(() => expect(result.current.isLoading).toBe(false));
  58. expect(result.current).toEqual({
  59. data: {
  60. errors: 3,
  61. performance_issues: 3,
  62. projects: 1,
  63. transactions: 3,
  64. },
  65. errors: [],
  66. isLoading: false,
  67. });
  68. });
  69. it('Collects errors from rejected api calls', async () => {
  70. const traceSlugs = ['slug1', 'slug2', 'slug3'];
  71. // Mock the API calls
  72. const mockRequest1 = MockApiClient.addMockResponse({
  73. method: 'GET',
  74. url: '/organizations/org-slug/events-trace-meta/slug1/',
  75. statusCode: 400,
  76. });
  77. const mockRequest2 = MockApiClient.addMockResponse({
  78. method: 'GET',
  79. url: '/organizations/org-slug/events-trace-meta/slug2/',
  80. statusCode: 400,
  81. });
  82. const mockRequest3 = MockApiClient.addMockResponse({
  83. method: 'GET',
  84. url: '/organizations/org-slug/events-trace-meta/slug3/',
  85. statusCode: 400,
  86. });
  87. const wrapper = ({children}: {children: React.ReactNode}) => (
  88. <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  89. );
  90. const {result} = renderHook(() => useTraceMeta(traceSlugs), {wrapper});
  91. expect(result.current).toEqual({
  92. data: undefined,
  93. errors: [],
  94. isLoading: true,
  95. });
  96. await waitFor(() => expect(result.current.isLoading).toBe(false));
  97. expect(result.current).toEqual({
  98. data: {
  99. errors: 0,
  100. performance_issues: 0,
  101. projects: 0,
  102. transactions: 0,
  103. },
  104. errors: [expect.any(Error), expect.any(Error), expect.any(Error)],
  105. isLoading: false,
  106. });
  107. expect(mockRequest1).toHaveBeenCalledTimes(1);
  108. expect(mockRequest2).toHaveBeenCalledTimes(1);
  109. expect(mockRequest3).toHaveBeenCalledTimes(1);
  110. });
  111. it('Accumulates metaResults and collects errors from rejected api calls', async () => {
  112. const traceSlugs = ['slug1', 'slug2', 'slug3'];
  113. // Mock the API calls
  114. const mockRequest1 = MockApiClient.addMockResponse({
  115. method: 'GET',
  116. url: '/organizations/org-slug/events-trace-meta/slug1/',
  117. statusCode: 400,
  118. });
  119. const mockRequest2 = MockApiClient.addMockResponse({
  120. method: 'GET',
  121. url: '/organizations/org-slug/events-trace-meta/slug2/',
  122. body: {
  123. errors: 1,
  124. performance_issues: 1,
  125. projects: 1,
  126. transactions: 1,
  127. },
  128. });
  129. const mockRequest3 = MockApiClient.addMockResponse({
  130. method: 'GET',
  131. url: '/organizations/org-slug/events-trace-meta/slug3/',
  132. body: {
  133. errors: 1,
  134. performance_issues: 1,
  135. projects: 1,
  136. transactions: 1,
  137. },
  138. });
  139. const wrapper = ({children}: {children: React.ReactNode}) => (
  140. <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  141. );
  142. const {result} = renderHook(() => useTraceMeta(traceSlugs), {wrapper});
  143. expect(result.current).toEqual({
  144. data: undefined,
  145. errors: [],
  146. isLoading: true,
  147. });
  148. await waitFor(() => expect(result.current.isLoading).toBe(false));
  149. expect(result.current).toEqual({
  150. data: {
  151. errors: 2,
  152. performance_issues: 2,
  153. projects: 1,
  154. transactions: 2,
  155. },
  156. errors: [expect.any(Error)],
  157. isLoading: false,
  158. });
  159. expect(mockRequest1).toHaveBeenCalledTimes(1);
  160. expect(mockRequest2).toHaveBeenCalledTimes(1);
  161. expect(mockRequest3).toHaveBeenCalledTimes(1);
  162. });
  163. });