projectPerformance.spec.tsx 12 KB

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