projectPerformance.spec.tsx 12 KB

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