projectPerformance.spec.tsx 11 KB

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