projectPerformance.spec.tsx 11 KB

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