edit.spec.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import {metric} from 'sentry/utils/analytics';
  4. import MetricRulesEdit from 'sentry/views/alerts/rules/metric/edit';
  5. import {AlertRuleTriggerType} from 'sentry/views/alerts/rules/metric/types';
  6. jest.mock('sentry/utils/analytics', () => ({
  7. metric: {
  8. startTransaction: jest.fn(() => ({
  9. setTag: jest.fn(),
  10. setData: jest.fn(),
  11. })),
  12. mark: jest.fn(),
  13. measure: jest.fn(),
  14. endTransaction: jest.fn(),
  15. },
  16. }));
  17. describe('MetricRulesEdit', function () {
  18. beforeEach(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. MockApiClient.addMockResponse({
  51. url: '/organizations/org-slug/members/',
  52. body: [TestStubs.Member()],
  53. });
  54. });
  55. afterEach(() => {
  56. MockApiClient.clearMockResponses();
  57. jest.clearAllMocks();
  58. });
  59. it('renders and edits trigger', async function () {
  60. const {organization, project} = initializeOrg();
  61. const rule = TestStubs.MetricRule();
  62. const onChangeTitleMock = jest.fn();
  63. const req = MockApiClient.addMockResponse({
  64. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  65. body: rule,
  66. });
  67. const editRule = MockApiClient.addMockResponse({
  68. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  69. method: 'PUT',
  70. body: rule,
  71. });
  72. render(
  73. <MetricRulesEdit
  74. {...TestStubs.routeComponentProps()}
  75. params={{
  76. projectId: project.slug,
  77. ruleId: rule.id,
  78. }}
  79. userTeamIds={[]}
  80. organization={organization}
  81. onChangeTitle={onChangeTitleMock}
  82. project={project}
  83. />
  84. );
  85. // has existing trigger
  86. expect(screen.getByTestId('critical-threshold')).toHaveValue('70');
  87. expect(screen.getByTestId('resolve-threshold')).toHaveValue('36');
  88. expect(req).toHaveBeenCalled();
  89. // Check correct rule name is called
  90. expect(onChangeTitleMock).toHaveBeenCalledWith(rule.name);
  91. await userEvent.clear(screen.getByTestId('resolve-threshold'));
  92. await userEvent.type(screen.getByTestId('resolve-threshold'), '7');
  93. // Create a new action
  94. await userEvent.click(screen.getByLabelText('Add Action'));
  95. // Save Trigger
  96. await 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. dataset: 'events',
  103. id: '4',
  104. name: 'My Incident Rule',
  105. projects: ['project-slug'],
  106. query: '',
  107. status: 0,
  108. timeWindow: 60,
  109. thresholdType: 0,
  110. resolveThreshold: 7,
  111. triggers: [
  112. expect.objectContaining({
  113. actions: [
  114. expect.objectContaining({
  115. integrationId: null,
  116. targetIdentifier: '',
  117. targetType: 'user',
  118. type: 'email',
  119. options: null,
  120. }),
  121. ],
  122. alertRuleId: '4',
  123. alertThreshold: 70,
  124. id: '1',
  125. }),
  126. ],
  127. }),
  128. method: 'PUT',
  129. })
  130. );
  131. // New Trigger should be in list
  132. // Has correct values
  133. expect(screen.getByTestId('critical-threshold')).toHaveValue('70');
  134. expect(screen.getByTestId('resolve-threshold')).toHaveValue('7');
  135. });
  136. it('removes warning trigger', async function () {
  137. const {organization, project} = initializeOrg();
  138. const rule = TestStubs.MetricRule();
  139. rule.triggers.push({
  140. label: AlertRuleTriggerType.WARNING,
  141. alertThreshold: 13,
  142. actions: [],
  143. });
  144. rule.resolveThreshold = 12;
  145. MockApiClient.addMockResponse({
  146. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  147. body: rule,
  148. });
  149. const editRule = MockApiClient.addMockResponse({
  150. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  151. method: 'PUT',
  152. body: rule,
  153. });
  154. render(
  155. <MetricRulesEdit
  156. {...TestStubs.routeComponentProps()}
  157. params={{
  158. projectId: project.slug,
  159. ruleId: rule.id,
  160. }}
  161. userTeamIds={[]}
  162. organization={organization}
  163. onChangeTitle={() => {}}
  164. project={project}
  165. />
  166. );
  167. // has existing trigger
  168. expect(screen.getByTestId('critical-threshold')).toHaveValue('70');
  169. expect(screen.getByTestId('warning-threshold')).toHaveValue('13');
  170. expect(screen.getByTestId('resolve-threshold')).toHaveValue('12');
  171. // Clear warning Trigger
  172. await userEvent.clear(screen.getByTestId('warning-threshold'));
  173. await userEvent.click(screen.getByLabelText('Save Rule'));
  174. expect(editRule).toHaveBeenCalledWith(
  175. expect.anything(),
  176. expect.objectContaining({
  177. data: expect.objectContaining({
  178. dataset: 'events',
  179. id: '4',
  180. name: 'My Incident Rule',
  181. projects: ['project-slug'],
  182. query: '',
  183. status: 0,
  184. timeWindow: 60,
  185. resolveThreshold: 12,
  186. thresholdType: 0,
  187. triggers: [
  188. expect.objectContaining({
  189. actions: [],
  190. alertRuleId: '4',
  191. alertThreshold: 70,
  192. id: '1',
  193. }),
  194. ],
  195. }),
  196. method: 'PUT',
  197. })
  198. );
  199. });
  200. it('renders 404', function () {
  201. const {organization, project} = initializeOrg();
  202. MockApiClient.addMockResponse({
  203. url: `/organizations/${organization.slug}/alert-rules/1234/`,
  204. statusCode: 404,
  205. body: {},
  206. });
  207. render(
  208. <MetricRulesEdit
  209. {...TestStubs.routeComponentProps()}
  210. userTeamIds={[]}
  211. onChangeTitle={() => {}}
  212. params={{
  213. projectId: project.slug,
  214. ruleId: '1234',
  215. }}
  216. organization={organization}
  217. project={project}
  218. />
  219. );
  220. expect(screen.getByText('This alert rule could not be found.')).toBeInTheDocument();
  221. });
  222. });