projectPerformance.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import {
  2. render,
  3. renderGlobalModal,
  4. screen,
  5. userEvent,
  6. } from 'sentry-test/reactTestingLibrary';
  7. import * as utils from 'sentry/utils/isActiveSuperuser';
  8. import ProjectPerformance, {
  9. allowedDurationValues,
  10. allowedPercentageValues,
  11. allowedSizeValues,
  12. DetectorConfigCustomer,
  13. } from 'sentry/views/settings/projectPerformance/projectPerformance';
  14. describe('projectPerformance', function () {
  15. const org = TestStubs.Organization({
  16. features: [
  17. 'performance-view',
  18. 'performance-issues-dev',
  19. 'project-performance-settings-admin',
  20. ],
  21. });
  22. const project = TestStubs.ProjectDetails();
  23. const configUrl = '/projects/org-slug/project-slug/transaction-threshold/configure/';
  24. let getMock, postMock, deleteMock, performanceIssuesMock;
  25. const router = TestStubs.router();
  26. const routerProps = {
  27. router,
  28. location: TestStubs.location(),
  29. routes: router.routes,
  30. route: router.routes[0],
  31. routeParams: router.params,
  32. };
  33. beforeEach(function () {
  34. MockApiClient.clearMockResponses();
  35. getMock = MockApiClient.addMockResponse({
  36. url: configUrl,
  37. method: 'GET',
  38. body: {
  39. id: project.id,
  40. threshold: '300',
  41. metric: 'duration',
  42. },
  43. statusCode: 200,
  44. });
  45. postMock = MockApiClient.addMockResponse({
  46. url: configUrl,
  47. method: 'POST',
  48. body: {
  49. id: project.id,
  50. threshold: '400',
  51. metric: 'lcp',
  52. },
  53. statusCode: 200,
  54. });
  55. deleteMock = MockApiClient.addMockResponse({
  56. url: configUrl,
  57. method: 'DELETE',
  58. statusCode: 200,
  59. });
  60. MockApiClient.addMockResponse({
  61. url: '/projects/org-slug/project-slug/',
  62. method: 'GET',
  63. body: {},
  64. statusCode: 200,
  65. });
  66. performanceIssuesMock = MockApiClient.addMockResponse({
  67. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  68. method: 'GET',
  69. body: {},
  70. statusCode: 200,
  71. });
  72. });
  73. it('renders the fields', function () {
  74. render(
  75. <ProjectPerformance
  76. params={{projectId: project.slug}}
  77. organization={org}
  78. project={project}
  79. {...routerProps}
  80. />
  81. );
  82. expect(
  83. screen.getByRole('textbox', {name: 'Response Time Threshold (ms)'})
  84. ).toHaveValue('300');
  85. expect(getMock).toHaveBeenCalledTimes(1);
  86. });
  87. it('updates the field', async function () {
  88. render(
  89. <ProjectPerformance
  90. params={{projectId: project.slug}}
  91. organization={org}
  92. project={project}
  93. {...routerProps}
  94. />
  95. );
  96. const input = screen.getByRole('textbox', {name: 'Response Time Threshold (ms)'});
  97. await userEvent.clear(input);
  98. await userEvent.type(input, '400');
  99. await userEvent.tab();
  100. expect(postMock).toHaveBeenCalledWith(
  101. configUrl,
  102. expect.objectContaining({
  103. data: {threshold: '400'},
  104. })
  105. );
  106. expect(input).toHaveValue('400');
  107. });
  108. it('clears the data', async function () {
  109. render(
  110. <ProjectPerformance
  111. params={{projectId: project.slug}}
  112. organization={org}
  113. project={project}
  114. {...routerProps}
  115. />
  116. );
  117. await userEvent.click(screen.getByRole('button', {name: 'Reset All'}));
  118. expect(deleteMock).toHaveBeenCalled();
  119. });
  120. it('does not get performance issues settings without the feature flag', function () {
  121. const orgWithoutPerfIssues = TestStubs.Organization({
  122. features: ['performance-view', 'performance-issues-dev'],
  123. });
  124. render(
  125. <ProjectPerformance
  126. params={{projectId: project.slug}}
  127. organization={orgWithoutPerfIssues}
  128. project={project}
  129. {...routerProps}
  130. />
  131. );
  132. expect(performanceIssuesMock).not.toHaveBeenCalled();
  133. });
  134. it('renders detector threshold configuration - admin ui', async function () {
  135. jest.spyOn(utils, 'isActiveSuperuser').mockReturnValue(true);
  136. MockApiClient.addMockResponse({
  137. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  138. method: 'GET',
  139. body: {n_plus_one_db_queries_detection_enabled: false},
  140. statusCode: 200,
  141. });
  142. const performanceIssuesPutMock = MockApiClient.addMockResponse({
  143. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  144. method: 'PUT',
  145. });
  146. render(
  147. <ProjectPerformance
  148. params={{projectId: project.slug}}
  149. organization={org}
  150. project={project}
  151. {...routerProps}
  152. />,
  153. {organization: org}
  154. );
  155. expect(
  156. await screen.findByText('N+1 DB Queries Detection Enabled')
  157. ).toBeInTheDocument();
  158. expect(screen.getByText('Slow DB Queries Detection Enabled')).toBeInTheDocument();
  159. const toggle = screen.getByRole('checkbox', {
  160. name: 'N+1 DB Queries Detection Enabled',
  161. });
  162. await userEvent.click(toggle);
  163. expect(performanceIssuesPutMock).toHaveBeenCalledWith(
  164. '/projects/org-slug/project-slug/performance-issues/configure/',
  165. expect.objectContaining({
  166. data: {n_plus_one_db_queries_detection_enabled: true},
  167. })
  168. );
  169. });
  170. it.each([
  171. {
  172. title: 'N+1 DB Queries',
  173. threshold: DetectorConfigCustomer.N_PLUS_DB_DURATION,
  174. allowedValues: allowedDurationValues,
  175. defaultValue: 100,
  176. newValue: 500,
  177. sliderIndex: 1,
  178. },
  179. {
  180. title: 'Slow DB Queries',
  181. threshold: DetectorConfigCustomer.SLOW_DB_DURATION,
  182. allowedValues: allowedDurationValues.slice(5),
  183. defaultValue: 1000,
  184. newValue: 3000,
  185. sliderIndex: 2,
  186. },
  187. {
  188. title: 'N+1 API Calls',
  189. threshold: DetectorConfigCustomer.N_PLUS_API_CALLS_DURATION,
  190. allowedValues: allowedDurationValues.slice(5),
  191. defaultValue: 300,
  192. newValue: 500,
  193. sliderIndex: 3,
  194. },
  195. {
  196. title: 'Large Render Blocking Asset',
  197. threshold: DetectorConfigCustomer.RENDER_BLOCKING_ASSET_RATIO,
  198. allowedValues: allowedPercentageValues,
  199. defaultValue: 0.33,
  200. newValue: 0.5,
  201. sliderIndex: 4,
  202. },
  203. {
  204. title: 'Large HTTP Payload',
  205. threshold: DetectorConfigCustomer.LARGE_HTT_PAYLOAD_SIZE,
  206. allowedValues: allowedSizeValues.slice(1),
  207. defaultValue: 1000000,
  208. newValue: 5000000,
  209. sliderIndex: 5,
  210. },
  211. {
  212. title: 'DB on Main Thread',
  213. threshold: DetectorConfigCustomer.DB_ON_MAIN_THREAD_DURATION,
  214. allowedValues: [10, 16, 33, 50],
  215. defaultValue: 16,
  216. newValue: 33,
  217. sliderIndex: 6,
  218. },
  219. {
  220. title: 'File I/O on Main Thread',
  221. threshold: DetectorConfigCustomer.FILE_IO_MAIN_THREAD_DURATION,
  222. allowedValues: [10, 16, 33, 50],
  223. defaultValue: 16,
  224. newValue: 50,
  225. sliderIndex: 7,
  226. },
  227. {
  228. title: 'Consecutive DB Queries',
  229. threshold: DetectorConfigCustomer.CONSECUTIVE_DB_MIN_TIME_SAVED,
  230. allowedValues: allowedDurationValues.slice(0, 23),
  231. defaultValue: 100,
  232. newValue: 5000,
  233. sliderIndex: 8,
  234. },
  235. {
  236. title: 'Uncompressed Asset',
  237. threshold: DetectorConfigCustomer.UNCOMPRESSED_ASSET_SIZE,
  238. allowedValues: allowedSizeValues.slice(1),
  239. defaultValue: 512000,
  240. newValue: 700000,
  241. sliderIndex: 9,
  242. },
  243. {
  244. title: 'Uncompressed Asset',
  245. threshold: DetectorConfigCustomer.UNCOMPRESSED_ASSET_DURATION,
  246. allowedValues: allowedDurationValues.slice(5),
  247. defaultValue: 500,
  248. newValue: 400,
  249. sliderIndex: 10,
  250. },
  251. {
  252. title: 'Consecutive HTTP',
  253. threshold: DetectorConfigCustomer.CONSECUTIVE_HTTP_MIN_TIME_SAVED,
  254. allowedValues: allowedDurationValues.slice(14),
  255. defaultValue: 2000,
  256. newValue: 4000,
  257. sliderIndex: 11,
  258. },
  259. ])(
  260. 'renders detector thresholds settings for $title issue',
  261. async ({title, threshold, allowedValues, defaultValue, newValue, sliderIndex}) => {
  262. // Mock endpoints
  263. const mockGETBody = {
  264. [threshold]: defaultValue,
  265. n_plus_one_db_queries_detection_enabled: true,
  266. slow_db_queries_detection_enabled: true,
  267. db_on_main_thread_detection_enabled: true,
  268. file_io_on_main_thread_detection_enabled: true,
  269. consecutive_db_queries_detection_enabled: true,
  270. large_render_blocking_asset_detection_enabled: true,
  271. uncompressed_assets_detection_enabled: true,
  272. large_http_payload_detection_enabled: true,
  273. n_plus_one_api_calls_detection_enabled: true,
  274. consecutive_http_spans_detection_enabled: true,
  275. };
  276. const performanceIssuesGetMock = MockApiClient.addMockResponse({
  277. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  278. method: 'GET',
  279. body: mockGETBody,
  280. statusCode: 200,
  281. });
  282. const performanceIssuesPutMock = MockApiClient.addMockResponse({
  283. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  284. method: 'PUT',
  285. });
  286. render(
  287. <ProjectPerformance
  288. params={{projectId: project.slug}}
  289. organization={org}
  290. project={project}
  291. {...routerProps}
  292. />,
  293. {organization: org}
  294. );
  295. expect(
  296. await screen.findByText('Performance Issues - Detector Threshold Settings')
  297. ).toBeInTheDocument();
  298. expect(screen.getByText(title)).toBeInTheDocument();
  299. // Open collapsed panels
  300. const chevrons = screen.getAllByTestId('form-panel-collapse-chevron');
  301. for (const chevron of chevrons) {
  302. await userEvent.click(chevron);
  303. }
  304. const slider = screen.getAllByRole('slider')[sliderIndex];
  305. const indexOfValue = allowedValues.indexOf(defaultValue);
  306. const newValueIndex = allowedValues.indexOf(newValue);
  307. // The value of the slider should be equal to the index
  308. // of the value returned from the GET method,
  309. // passed to it in the allowedValues array.
  310. expect(performanceIssuesGetMock).toHaveBeenCalled();
  311. expect(slider).toHaveValue(indexOfValue.toString());
  312. // Slide value on range slider.
  313. slider.focus();
  314. const indexDelta = newValueIndex - indexOfValue;
  315. await userEvent.keyboard(
  316. indexDelta > 0 ? `{ArrowRight>${indexDelta}}` : `{ArrowLeft>${-indexDelta}}`
  317. );
  318. await userEvent.tab();
  319. expect(slider).toHaveValue(newValueIndex.toString());
  320. // Ensure that PUT request is fired to update
  321. // project settings
  322. const expectedPUTPayload = {};
  323. expectedPUTPayload[threshold] = newValue;
  324. expect(performanceIssuesPutMock).toHaveBeenCalledWith(
  325. '/projects/org-slug/project-slug/performance-issues/configure/',
  326. expect.objectContaining({
  327. data: expectedPUTPayload,
  328. })
  329. );
  330. }
  331. );
  332. it('test reset all detector thresholds', async function () {
  333. MockApiClient.addMockResponse({
  334. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  335. method: 'GET',
  336. body: {
  337. n_plus_one_db_queries_detection_enabled: true,
  338. slow_db_queries_detection_enabled: false,
  339. },
  340. statusCode: 200,
  341. });
  342. const delete_request_mock = MockApiClient.addMockResponse({
  343. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  344. method: 'DELETE',
  345. });
  346. render(
  347. <ProjectPerformance
  348. params={{projectId: project.slug}}
  349. organization={org}
  350. project={project}
  351. {...routerProps}
  352. />,
  353. {organization: org}
  354. );
  355. const button = await screen.findByText('Reset All Thresholds');
  356. expect(button).toBeInTheDocument();
  357. renderGlobalModal();
  358. await userEvent.click(button);
  359. // Ensure that confirm modal renders
  360. const confirmButton = screen.getByText('Confirm');
  361. expect(confirmButton).toBeInTheDocument();
  362. await userEvent.click(confirmButton);
  363. expect(delete_request_mock).toHaveBeenCalled();
  364. });
  365. });