projectPerformance.spec.tsx 11 KB

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