details.spec.jsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import {Fragment} from 'react';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import GlobalModal from 'sentry/components/globalModal';
  5. import {metric} from 'sentry/utils/analytics';
  6. import MetricRulesEdit from 'sentry/views/alerts/rules/metric/edit';
  7. import {AlertRuleTriggerType} from 'sentry/views/alerts/rules/metric/types';
  8. jest.mock('sentry/utils/analytics', () => ({
  9. metric: {
  10. startTransaction: jest.fn(() => ({
  11. setTag: jest.fn(),
  12. setData: jest.fn(),
  13. })),
  14. endTransaction: jest.fn(),
  15. },
  16. }));
  17. describe('Incident Rules Details', function () {
  18. beforeAll(function () {
  19. MockApiClient.addMockResponse({
  20. url: '/organizations/org-slug/users/',
  21. body: [],
  22. });
  23. MockApiClient.addMockResponse({
  24. url: '/organizations/org-slug/tags/',
  25. body: [],
  26. });
  27. MockApiClient.addMockResponse({
  28. url: '/projects/org-slug/project-slug/environments/',
  29. body: [],
  30. });
  31. MockApiClient.addMockResponse({
  32. url: '/organizations/org-slug/events-stats/',
  33. body: null,
  34. });
  35. MockApiClient.addMockResponse({
  36. url: '/organizations/org-slug/events-meta/',
  37. body: {count: 5},
  38. });
  39. MockApiClient.addMockResponse({
  40. url: '/organizations/org-slug/alert-rules/available-actions/',
  41. body: [
  42. {
  43. allowedTargetTypes: ['user', 'team'],
  44. integrationName: null,
  45. type: 'email',
  46. integrationId: null,
  47. },
  48. ],
  49. });
  50. });
  51. it('renders and edits trigger', function () {
  52. const {organization, project} = initializeOrg();
  53. const rule = TestStubs.MetricRule();
  54. const onChangeTitleMock = jest.fn();
  55. const req = MockApiClient.addMockResponse({
  56. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  57. body: rule,
  58. });
  59. MockApiClient.addMockResponse({
  60. url: '/organizations/org-slug/members/',
  61. body: [TestStubs.Member()],
  62. });
  63. const editRule = MockApiClient.addMockResponse({
  64. url: `/projects/${organization.slug}/project-slug/alert-rules/${rule.id}/`,
  65. method: 'PUT',
  66. body: rule,
  67. });
  68. render(
  69. <Fragment>
  70. <GlobalModal />
  71. <MetricRulesEdit
  72. params={{
  73. orgId: organization.slug,
  74. projectId: project.slug,
  75. ruleId: rule.id,
  76. }}
  77. organization={organization}
  78. onChangeTitle={onChangeTitleMock}
  79. project={project}
  80. />
  81. </Fragment>
  82. );
  83. // has existing trigger
  84. expect(screen.getByTestId('critical-threshold')).toHaveValue('70');
  85. expect(screen.getByTestId('resolve-threshold')).toHaveValue('36');
  86. expect(req).toHaveBeenCalled();
  87. // Check correct rule name is called
  88. expect(onChangeTitleMock).toHaveBeenCalledWith(rule.name);
  89. userEvent.clear(screen.getByTestId('warning-threshold'));
  90. userEvent.type(screen.getByTestId('warning-threshold'), '13');
  91. userEvent.clear(screen.getByTestId('resolve-threshold'));
  92. userEvent.type(screen.getByTestId('resolve-threshold'), '12');
  93. // Create a new action
  94. userEvent.click(screen.getByLabelText('Add Action'));
  95. // Save Trigger
  96. userEvent.click(screen.getByLabelText('Save Rule'));
  97. expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
  98. expect(editRule).toHaveBeenCalledWith(
  99. expect.anything(),
  100. expect.objectContaining({
  101. data: expect.objectContaining({
  102. aggregation: 0,
  103. dataset: 'events',
  104. id: '4',
  105. name: 'My Incident Rule',
  106. projects: ['project-slug'],
  107. query: '',
  108. status: 0,
  109. timeWindow: 60,
  110. thresholdType: 0,
  111. resolveThreshold: 12,
  112. triggers: [
  113. expect.objectContaining({
  114. actions: [
  115. expect.objectContaining({
  116. integrationId: null,
  117. targetIdentifier: '',
  118. targetType: 'user',
  119. type: 'email',
  120. options: null,
  121. }),
  122. ],
  123. alertRuleId: '4',
  124. alertThreshold: 70,
  125. id: '1',
  126. }),
  127. expect.objectContaining({
  128. actions: [],
  129. alertThreshold: 13,
  130. }),
  131. ],
  132. }),
  133. method: 'PUT',
  134. })
  135. );
  136. // New Trigger should be in list
  137. // Has correct values
  138. expect(screen.getByTestId('critical-threshold')).toHaveValue('70');
  139. expect(screen.getByTestId('warning-threshold')).toHaveValue('13');
  140. expect(screen.getByTestId('resolve-threshold')).toHaveValue('12');
  141. });
  142. it('clears trigger', async function () {
  143. const {organization, project} = initializeOrg();
  144. const rule = TestStubs.MetricRule();
  145. rule.triggers.push({
  146. label: AlertRuleTriggerType.WARNING,
  147. alertThreshold: 13,
  148. actions: [],
  149. });
  150. rule.resolveThreshold = 12;
  151. const onChangeTitleMock = jest.fn();
  152. const req = MockApiClient.addMockResponse({
  153. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  154. body: rule,
  155. });
  156. MockApiClient.addMockResponse({
  157. url: '/organizations/org-slug/members/',
  158. body: [TestStubs.Member()],
  159. });
  160. const editRule = MockApiClient.addMockResponse({
  161. url: `/projects/${organization.slug}/project-slug/alert-rules/${rule.id}/`,
  162. method: 'PUT',
  163. body: rule,
  164. });
  165. render(
  166. <Fragment>
  167. <GlobalModal />
  168. <MetricRulesEdit
  169. params={{
  170. orgId: organization.slug,
  171. projectId: project.slug,
  172. ruleId: rule.id,
  173. }}
  174. organization={organization}
  175. onChangeTitle={onChangeTitleMock}
  176. project={project}
  177. />
  178. </Fragment>
  179. );
  180. // has existing trigger
  181. expect(screen.getByTestId('critical-threshold')).toHaveValue('70');
  182. expect(screen.getByTestId('warning-threshold')).toHaveValue('13');
  183. expect(screen.getByTestId('resolve-threshold')).toHaveValue('12');
  184. expect(req).toHaveBeenCalled();
  185. // Check correct rule name is called
  186. expect(onChangeTitleMock).toHaveBeenCalledWith(rule.name);
  187. userEvent.click(screen.getByLabelText('Add Action'));
  188. // Clear warning Trigger
  189. userEvent.clear(screen.getByTestId('warning-threshold'));
  190. await waitFor(() => expect(screen.getByLabelText('Save Rule')).toBeEnabled());
  191. // Save Trigger
  192. userEvent.click(screen.getByLabelText('Save Rule'));
  193. expect(editRule).toHaveBeenCalledWith(
  194. expect.anything(),
  195. expect.objectContaining({
  196. data: expect.objectContaining({
  197. aggregation: 0,
  198. dataset: 'events',
  199. id: '4',
  200. name: 'My Incident Rule',
  201. projects: ['project-slug'],
  202. query: '',
  203. status: 0,
  204. timeWindow: 60,
  205. resolveThreshold: 12,
  206. thresholdType: 0,
  207. triggers: [
  208. expect.objectContaining({
  209. actions: [
  210. expect.objectContaining({
  211. integrationId: null,
  212. targetIdentifier: '',
  213. targetType: 'user',
  214. type: 'email',
  215. options: null,
  216. }),
  217. ],
  218. alertRuleId: '4',
  219. alertThreshold: 70,
  220. id: '1',
  221. }),
  222. ],
  223. }),
  224. method: 'PUT',
  225. })
  226. );
  227. });
  228. it('renders 404', function () {
  229. const {organization, project} = initializeOrg();
  230. MockApiClient.addMockResponse({
  231. url: `/organizations/${organization.slug}/alert-rules/1234/`,
  232. statusCode: 404,
  233. body: {},
  234. });
  235. render(
  236. <MetricRulesEdit
  237. params={{
  238. orgId: organization.slug,
  239. projectId: project.slug,
  240. ruleId: '1234',
  241. }}
  242. organization={organization}
  243. project={project}
  244. />
  245. );
  246. expect(screen.getByText('This alert rule could not be found.')).toBeInTheDocument();
  247. });
  248. });