projectPerformance.spec.tsx 12 KB

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