projectPerformance.spec.tsx 12 KB

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