projectPerformance.spec.tsx 12 KB

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