useProfileEventsStats.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {ReactNode} from 'react';
  2. import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  5. import {useProfileEventsStats} from 'sentry/utils/profiling/hooks/useProfileEventsStats';
  6. import {OrganizationContext} from 'sentry/views/organizationContext';
  7. const {organization} = initializeOrg();
  8. const client = new QueryClient();
  9. function TestContext({children}: {children?: ReactNode}) {
  10. return (
  11. <QueryClientProvider client={client}>
  12. <OrganizationContext.Provider value={organization}>
  13. {children}
  14. </OrganizationContext.Provider>
  15. </QueryClientProvider>
  16. );
  17. }
  18. describe('useProfileEvents', function () {
  19. afterEach(function () {
  20. MockApiClient.clearMockResponses();
  21. });
  22. it('handles no axis', async function () {
  23. const yAxes = [];
  24. MockApiClient.addMockResponse({
  25. url: `/organizations/${organization.slug}/events-stats/`,
  26. body: {},
  27. match: [MockApiClient.matchQuery({dataset: 'profiles'})],
  28. });
  29. const {result, waitFor} = reactHooks.renderHook(useProfileEventsStats, {
  30. wrapper: TestContext,
  31. initialProps: {
  32. yAxes,
  33. referrer: '',
  34. },
  35. });
  36. await waitFor(() => result.current.isSuccess);
  37. expect(result.current.data).toEqual([
  38. {
  39. data: [],
  40. meta: {
  41. dataset: 'profiles',
  42. end: 0,
  43. start: 0,
  44. },
  45. timestamps: [],
  46. },
  47. expect.anything(),
  48. expect.objectContaining({
  49. getResponseHeader: expect.anything(),
  50. }),
  51. ]);
  52. });
  53. it('handles 1 axis', async function () {
  54. const yAxes = ['count()'];
  55. MockApiClient.addMockResponse({
  56. url: `/organizations/${organization.slug}/events-stats/`,
  57. body: {
  58. data: [
  59. [0, [{count: 1}]],
  60. [5, [{count: 2}]],
  61. ],
  62. start: 0,
  63. end: 10,
  64. meta: {
  65. fields: {count: 'integer'},
  66. units: {count: null},
  67. },
  68. },
  69. match: [MockApiClient.matchQuery({dataset: 'profiles'})],
  70. });
  71. const {result, waitFor} = reactHooks.renderHook(useProfileEventsStats, {
  72. wrapper: TestContext,
  73. initialProps: {
  74. yAxes,
  75. referrer: '',
  76. },
  77. });
  78. await waitFor(() => result.current.isSuccess);
  79. expect(result.current.data).toEqual([
  80. {
  81. data: [{axis: 'count()', values: [1, 2]}],
  82. meta: {
  83. dataset: 'profiles',
  84. start: 0,
  85. end: 10,
  86. },
  87. timestamps: [0, 5],
  88. },
  89. expect.anything(),
  90. expect.objectContaining({
  91. getResponseHeader: expect.anything(),
  92. }),
  93. ]);
  94. });
  95. it('handles n axes', async function () {
  96. const yAxes = ['count()', 'p99()'];
  97. MockApiClient.addMockResponse({
  98. url: `/organizations/${organization.slug}/events-stats/`,
  99. body: {
  100. 'count()': {
  101. data: [
  102. [0, [{count: 1}]],
  103. [5, [{count: 2}]],
  104. ],
  105. start: 0,
  106. end: 10,
  107. meta: {
  108. fields: {count: 'integer', p99: 'duration'},
  109. units: {count: null, p99: 'nanosecond'},
  110. },
  111. },
  112. 'p99()': {
  113. data: [
  114. [0, [{count: 3}]],
  115. [5, [{count: 4}]],
  116. ],
  117. start: 0,
  118. end: 10,
  119. meta: {
  120. fields: {count: 'integer', p99: 'duration'},
  121. units: {count: null, p99: 'nanosecond'},
  122. },
  123. },
  124. },
  125. match: [MockApiClient.matchQuery({dataset: 'profiles'})],
  126. });
  127. const {result, waitFor} = reactHooks.renderHook(useProfileEventsStats, {
  128. wrapper: TestContext,
  129. initialProps: {
  130. yAxes,
  131. referrer: '',
  132. },
  133. });
  134. await waitFor(() => result.current.isSuccess);
  135. expect(result.current.data).toEqual([
  136. {
  137. data: [
  138. {axis: 'count()', values: [1, 2]},
  139. {axis: 'p99()', values: [3, 4]},
  140. ],
  141. meta: {
  142. dataset: 'profiles',
  143. start: 0,
  144. end: 10,
  145. },
  146. timestamps: [0, 5],
  147. },
  148. expect.anything(),
  149. expect.objectContaining({
  150. getResponseHeader: expect.anything(),
  151. }),
  152. ]);
  153. });
  154. });