slowestFunctionsWidget.spec.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import {ProjectFixture} from 'sentry-fixture/project';
  2. import {getAllByRole, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import ProjectsStore from 'sentry/stores/projectsStore';
  4. import {SlowestFunctionsWidget} from 'sentry/views/profiling/landing/slowestFunctionsWidget';
  5. describe('SlowestFunctionsWidget', function () {
  6. beforeEach(function () {
  7. const project = ProjectFixture({
  8. id: '1',
  9. slug: 'proj-slug',
  10. });
  11. ProjectsStore.loadInitialData([project]);
  12. });
  13. afterEach(function () {
  14. MockApiClient.clearMockResponses();
  15. });
  16. it('renders errors', async function () {
  17. // return 400 for all queries
  18. MockApiClient.addMockResponse({
  19. url: '/organizations/org-slug/events/',
  20. statusCode: 400,
  21. });
  22. render(<SlowestFunctionsWidget widgetHeight="100px" breakdownFunction="p75()" />);
  23. // starts by rendering loading
  24. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  25. // switches to errors once the api responds with an error
  26. expect(await screen.findByTestId('error-indicator')).toBeInTheDocument();
  27. });
  28. it('renders no functions', async function () {
  29. // for the slowest functions query
  30. MockApiClient.addMockResponse({
  31. url: '/organizations/org-slug/events/',
  32. body: {
  33. data: [],
  34. },
  35. match: [
  36. MockApiClient.matchQuery({
  37. dataset: 'profileFunctions',
  38. field: ['project.id', 'fingerprint', 'package', 'function', 'count()', 'sum()'],
  39. }),
  40. ],
  41. });
  42. render(<SlowestFunctionsWidget widgetHeight="100px" breakdownFunction="p75()" />);
  43. // starts by rendering loading
  44. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  45. // switches to the no functions view
  46. expect(await screen.findByText('No functions found')).toBeInTheDocument();
  47. });
  48. it('renders examples and chart', async function () {
  49. // for the slowest functions query
  50. MockApiClient.addMockResponse({
  51. url: '/organizations/org-slug/events/',
  52. body: {
  53. data: [
  54. {
  55. 'project.id': 1,
  56. fingerprint: 123,
  57. package: 'foo',
  58. function: 'bar',
  59. 'sum()': 150,
  60. },
  61. {
  62. 'project.id': 1,
  63. fingerprint: 456,
  64. package: 'baz',
  65. function: 'qux',
  66. 'sum()': 100,
  67. },
  68. ],
  69. },
  70. match: [
  71. MockApiClient.matchQuery({
  72. dataset: 'profileFunctions',
  73. field: ['project.id', 'fingerprint', 'package', 'function', 'count()', 'sum()'],
  74. }),
  75. ],
  76. });
  77. // for the totals query
  78. MockApiClient.addMockResponse({
  79. url: '/organizations/org-slug/events/',
  80. body: {data: [{'project.id': 1, 'sum()': 2500000}]},
  81. match: [
  82. MockApiClient.matchQuery({
  83. dataset: 'profileFunctions',
  84. field: ['project.id', 'sum()'],
  85. project: [1],
  86. }),
  87. ],
  88. });
  89. // for the chart + examples
  90. MockApiClient.addMockResponse({
  91. url: '/organizations/org-slug/events-stats/',
  92. body: {
  93. 123: {
  94. 'all_examples()': {
  95. order: 0,
  96. start: 0,
  97. end: 1000,
  98. meta: {
  99. fields: {
  100. time: 'date',
  101. fingerprint: 'integer',
  102. p75: 'duration',
  103. all_examples: 'string',
  104. },
  105. units: {
  106. time: null,
  107. fingerprint: null,
  108. p75: 'nanosecond',
  109. all_examples: null,
  110. },
  111. },
  112. data: [
  113. [0, [{count: 0}]],
  114. [500, [{count: [{profile_id: '1'.repeat(32)}]}]],
  115. [
  116. 1000,
  117. [
  118. {
  119. count: [
  120. {profiler_id: '2'.repeat(32), thread_id: '0', start: 0, end: 1000},
  121. ],
  122. },
  123. ],
  124. ],
  125. ],
  126. },
  127. 'p75()': {
  128. order: 1,
  129. start: 0,
  130. end: 1000,
  131. meta: {
  132. fields: {
  133. time: 'date',
  134. fingerprint: 'integer',
  135. p75: 'duration',
  136. all_examples: 'string',
  137. },
  138. units: {
  139. time: null,
  140. fingerprint: null,
  141. p75: 'nanosecond',
  142. all_examples: null,
  143. },
  144. },
  145. data: [
  146. [0, [{count: 1}]],
  147. [500, [{count: 2}]],
  148. [1000, [{count: 3}]],
  149. ],
  150. },
  151. },
  152. 456: {
  153. 'all_examples()': {
  154. order: 0,
  155. start: 0,
  156. end: 1000,
  157. meta: {
  158. fields: {
  159. time: 'date',
  160. fingerprint: 'integer',
  161. p75: 'duration',
  162. all_examples: 'string',
  163. },
  164. units: {
  165. time: null,
  166. fingerprint: null,
  167. p75: 'nanosecond',
  168. all_examples: null,
  169. },
  170. },
  171. data: [
  172. [0, [{count: 0}]],
  173. [500, [{count: [{profile_id: '3'.repeat(32)}]}]],
  174. [
  175. 1000,
  176. [
  177. {
  178. count: [
  179. {profiler_id: '4'.repeat(32), thread_id: '0', start: 0, end: 1000},
  180. ],
  181. },
  182. ],
  183. ],
  184. ],
  185. },
  186. 'p75()': {
  187. order: 1,
  188. start: 0,
  189. end: 1000,
  190. meta: {
  191. fields: {
  192. time: 'date',
  193. fingerprint: 'integer',
  194. p75: 'duration',
  195. all_examples: 'string',
  196. },
  197. units: {
  198. time: null,
  199. fingerprint: null,
  200. p75: 'nanosecond',
  201. all_examples: null,
  202. },
  203. },
  204. data: [
  205. [0, [{count: 1}]],
  206. [500, [{count: 2}]],
  207. [1000, [{count: 3}]],
  208. ],
  209. },
  210. },
  211. },
  212. match: [
  213. MockApiClient.matchQuery({
  214. dataset: 'profileFunctions',
  215. field: ['fingerprint', 'all_examples()', 'p75()'],
  216. yAxis: ['all_examples()', 'p75()'],
  217. project: [1],
  218. }),
  219. ],
  220. });
  221. render(<SlowestFunctionsWidget widgetHeight="100px" breakdownFunction="p75()" />);
  222. // starts by rendering loading
  223. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  224. // switches to the functions-chart once the api responds with data
  225. expect(await screen.findByTestId('function-chart')).toBeInTheDocument();
  226. const items = screen.getAllByRole('listitem', {});
  227. expect(items.length).toEqual(2);
  228. const buttons = getAllByRole(items[0], 'button', {});
  229. expect(buttons.length).toEqual(2);
  230. await userEvent.click(buttons[1]);
  231. expect(screen.getByText('1'.repeat(8))).toBeInTheDocument();
  232. expect(screen.getByText('2'.repeat(8))).toBeInTheDocument();
  233. });
  234. });