projectPerformance.spec.tsx 11 KB

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