projectPerformance.spec.jsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import {
  2. fireEvent,
  3. render,
  4. renderGlobalModal,
  5. screen,
  6. userEvent,
  7. } from 'sentry-test/reactTestingLibrary';
  8. import * as utils from 'sentry/utils/isActiveSuperuser';
  9. import ProjectPerformance, {
  10. allowedDurationValues,
  11. } from 'sentry/views/settings/projectPerformance/projectPerformance';
  12. describe('projectPerformance', function () {
  13. const org = TestStubs.Organization({
  14. features: [
  15. 'performance-view',
  16. 'performance-issues-dev',
  17. 'project-performance-settings-admin',
  18. ],
  19. });
  20. const project = TestStubs.ProjectDetails();
  21. const configUrl = '/projects/org-slug/project-slug/transaction-threshold/configure/';
  22. let getMock, postMock, deleteMock, performanceIssuesMock;
  23. beforeEach(function () {
  24. MockApiClient.clearMockResponses();
  25. getMock = MockApiClient.addMockResponse({
  26. url: configUrl,
  27. method: 'GET',
  28. body: {
  29. id: project.id,
  30. threshold: '300',
  31. metric: 'duration',
  32. },
  33. statusCode: 200,
  34. });
  35. postMock = MockApiClient.addMockResponse({
  36. url: configUrl,
  37. method: 'POST',
  38. body: {
  39. id: project.id,
  40. threshold: '400',
  41. metric: 'lcp',
  42. },
  43. statusCode: 200,
  44. });
  45. deleteMock = MockApiClient.addMockResponse({
  46. url: configUrl,
  47. method: 'DELETE',
  48. statusCode: 200,
  49. });
  50. MockApiClient.addMockResponse({
  51. url: '/projects/org-slug/project-slug/',
  52. method: 'GET',
  53. body: {},
  54. statusCode: 200,
  55. });
  56. performanceIssuesMock = MockApiClient.addMockResponse({
  57. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  58. method: 'GET',
  59. body: {},
  60. statusCode: 200,
  61. });
  62. });
  63. it('renders the fields', function () {
  64. render(
  65. <ProjectPerformance
  66. params={{orgId: org.slug, projectId: project.slug}}
  67. organization={org}
  68. project={project}
  69. />
  70. );
  71. expect(
  72. screen.getByRole('textbox', {name: 'Response Time Threshold (ms)'})
  73. ).toHaveValue('300');
  74. expect(getMock).toHaveBeenCalledTimes(1);
  75. });
  76. it('updates the field', async function () {
  77. render(
  78. <ProjectPerformance
  79. params={{orgId: org.slug, projectId: project.slug}}
  80. organization={org}
  81. project={project}
  82. />
  83. );
  84. const input = screen.getByRole('textbox', {name: 'Response Time Threshold (ms)'});
  85. await userEvent.clear(input);
  86. await userEvent.type(input, '400');
  87. await userEvent.tab();
  88. expect(postMock).toHaveBeenCalledWith(
  89. configUrl,
  90. expect.objectContaining({
  91. data: {threshold: '400'},
  92. })
  93. );
  94. expect(input).toHaveValue('400');
  95. });
  96. it('clears the data', async function () {
  97. render(
  98. <ProjectPerformance
  99. params={{orgId: org.slug, projectId: project.slug}}
  100. organization={org}
  101. project={project}
  102. />
  103. );
  104. await userEvent.click(screen.getByRole('button', {name: 'Reset All'}));
  105. expect(deleteMock).toHaveBeenCalled();
  106. });
  107. it('does not get performance issues settings without the feature flag', function () {
  108. const orgWithoutPerfIssues = TestStubs.Organization({
  109. features: ['performance-view', 'performance-issues-dev'],
  110. });
  111. render(
  112. <ProjectPerformance
  113. params={{orgId: org.slug, projectId: project.slug}}
  114. organization={orgWithoutPerfIssues}
  115. project={project}
  116. />
  117. );
  118. expect(performanceIssuesMock).not.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={{orgId: org.slug, projectId: project.slug}}
  135. organization={org}
  136. project={project}
  137. />,
  138. {organization: org}
  139. );
  140. expect(
  141. await screen.findByText('N+1 DB Queries Detection Enabled')
  142. ).toBeInTheDocument();
  143. expect(screen.getByText('Slow DB Queries Detection Enabled')).toBeInTheDocument();
  144. const toggle = screen.getByRole('checkbox', {
  145. name: 'N+1 DB Queries Detection Enabled',
  146. });
  147. await userEvent.click(toggle);
  148. expect(performanceIssuesPutMock).toHaveBeenCalledWith(
  149. '/projects/org-slug/project-slug/performance-issues/configure/',
  150. expect.objectContaining({
  151. data: {n_plus_one_db_queries_detection_enabled: true},
  152. })
  153. );
  154. });
  155. it.each([
  156. {
  157. title: 'N+1 DB Queries',
  158. threshold: 'n_plus_one_db_duration_threshold',
  159. allowedValues: allowedDurationValues,
  160. defaultValue: 100,
  161. newValue: 500,
  162. newValueIndex: 5,
  163. sliderIndex: 1,
  164. },
  165. {
  166. title: 'Slow DB Queries',
  167. threshold: 'slow_db_query_duration_threshold',
  168. allowedValues: allowedDurationValues.slice(1),
  169. defaultValue: 1000,
  170. newValue: 3000,
  171. newValueIndex: 7,
  172. sliderIndex: 2,
  173. },
  174. ])(
  175. 'renders detector thresholds settings for $title issue',
  176. async ({
  177. title,
  178. threshold,
  179. allowedValues,
  180. defaultValue,
  181. newValue,
  182. newValueIndex,
  183. sliderIndex,
  184. }) => {
  185. // Mock endpoints
  186. const mockGETBody = {};
  187. mockGETBody[threshold] = defaultValue;
  188. const performanceIssuesGetMock = MockApiClient.addMockResponse({
  189. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  190. method: 'GET',
  191. body: mockGETBody,
  192. statusCode: 200,
  193. });
  194. const performanceIssuesPutMock = MockApiClient.addMockResponse({
  195. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  196. method: 'PUT',
  197. });
  198. render(
  199. <ProjectPerformance
  200. params={{orgId: org.slug, projectId: project.slug}}
  201. organization={org}
  202. project={project}
  203. />,
  204. {organization: org}
  205. );
  206. expect(
  207. await screen.findByText('Performance Issues - Detector Threshold Settings')
  208. ).toBeInTheDocument();
  209. expect(screen.getByText(title)).toBeInTheDocument();
  210. const slider = screen.getAllByRole('slider')[sliderIndex];
  211. const indexOfValue = allowedValues.indexOf(defaultValue);
  212. // The value of the slider should be equal to the index
  213. // of the value returned from the GET method,
  214. // passed to it in the allowedValues array.
  215. expect(performanceIssuesGetMock).toHaveBeenCalled();
  216. expect(slider).toHaveValue(indexOfValue.toString());
  217. // Slide value on range slider.
  218. fireEvent.change(slider, {target: {value: newValueIndex}});
  219. expect(slider).toHaveValue(newValueIndex.toString());
  220. fireEvent.keyUp(slider);
  221. // Ensure that PUT request is fired to update
  222. // project settings
  223. const expectedPUTPayload = {};
  224. expectedPUTPayload[threshold] = newValue;
  225. expect(performanceIssuesPutMock).toHaveBeenCalledWith(
  226. '/projects/org-slug/project-slug/performance-issues/configure/',
  227. expect.objectContaining({
  228. data: expectedPUTPayload,
  229. })
  230. );
  231. }
  232. );
  233. it('test reset all detector thresholds', async function () {
  234. MockApiClient.addMockResponse({
  235. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  236. method: 'GET',
  237. body: {
  238. n_plus_one_db_queries_detection_enabled: true,
  239. slow_db_queries_detection_enabled: false,
  240. },
  241. statusCode: 200,
  242. });
  243. const delete_request_mock = MockApiClient.addMockResponse({
  244. url: '/projects/org-slug/project-slug/performance-issues/configure/',
  245. method: 'DELETE',
  246. });
  247. render(
  248. <ProjectPerformance
  249. params={{orgId: org.slug, projectId: project.slug}}
  250. organization={org}
  251. project={project}
  252. />,
  253. {organization: org}
  254. );
  255. const button = await screen.findByText('Reset All Thresholds');
  256. expect(button).toBeInTheDocument();
  257. renderGlobalModal();
  258. await userEvent.click(button);
  259. // Ensure that confirm modal renders
  260. const confirmButton = screen.getByText('Confirm');
  261. expect(confirmButton).toBeInTheDocument();
  262. await userEvent.click(confirmButton);
  263. expect(delete_request_mock).toHaveBeenCalled();
  264. });
  265. });