projectPerformance.spec.tsx 12 KB

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