create.spec.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. import selectEvent from 'react-select-event';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import TeamStore from 'sentry/stores/teamStore';
  6. import {metric} from 'sentry/utils/analytics';
  7. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  8. import AlertsContainer from 'sentry/views/alerts';
  9. import AlertBuilderProjectProvider from 'sentry/views/alerts/builder/projectProvider';
  10. import ProjectAlertsCreate from 'sentry/views/alerts/create';
  11. jest.unmock('sentry/utils/recreateRoute');
  12. jest.mock('sentry/actionCreators/members');
  13. jest.mock('react-router');
  14. jest.mock('sentry/utils/analytics', () => ({
  15. metric: {
  16. startTransaction: jest.fn(() => ({
  17. setTag: jest.fn(),
  18. setData: jest.fn(),
  19. })),
  20. endTransaction: jest.fn(),
  21. mark: jest.fn(),
  22. measure: jest.fn(),
  23. },
  24. trackAdvancedAnalyticsEvent: jest.fn(),
  25. }));
  26. jest.mock('sentry/utils/analytics/trackAdvancedAnalyticsEvent');
  27. describe('ProjectAlertsCreate', function () {
  28. beforeEach(function () {
  29. TeamStore.init();
  30. TeamStore.loadInitialData([], false, null);
  31. MockApiClient.addMockResponse({
  32. url: '/projects/org-slug/project-slug/rules/configuration/',
  33. body: TestStubs.ProjectAlertRuleConfiguration(),
  34. });
  35. MockApiClient.addMockResponse({
  36. url: '/projects/org-slug/project-slug/rules/1/',
  37. body: TestStubs.ProjectAlertRule(),
  38. });
  39. MockApiClient.addMockResponse({
  40. url: '/projects/org-slug/project-slug/environments/',
  41. body: TestStubs.Environments(),
  42. });
  43. MockApiClient.addMockResponse({
  44. url: `/projects/org-slug/project-slug/?expand=hasAlertIntegration`,
  45. body: {},
  46. });
  47. MockApiClient.addMockResponse({
  48. url: `/projects/org-slug/project-slug/ownership/`,
  49. method: 'GET',
  50. body: {
  51. fallthrough: false,
  52. autoAssignment: false,
  53. },
  54. });
  55. });
  56. afterEach(function () {
  57. MockApiClient.clearMockResponses();
  58. jest.clearAllMocks();
  59. TeamStore.teardown();
  60. });
  61. const createWrapper = (props = {}, location = {}) => {
  62. const {organization, project, router} = initializeOrg(props);
  63. ProjectsStore.loadInitialData([project]);
  64. const params = {orgId: organization.slug, projectId: project.slug};
  65. const wrapper = render(
  66. <AlertsContainer>
  67. <AlertBuilderProjectProvider params={params}>
  68. <ProjectAlertsCreate
  69. params={params}
  70. location={{
  71. pathname: `/organizations/org-slug/alerts/rules/${project.slug}/new/`,
  72. query: {createFromWizard: true},
  73. ...location,
  74. }}
  75. router={router}
  76. />
  77. </AlertBuilderProjectProvider>
  78. </AlertsContainer>,
  79. {organization}
  80. );
  81. return {
  82. wrapper,
  83. organization,
  84. project,
  85. router,
  86. };
  87. };
  88. it('adds default parameters if wizard was skipped', function () {
  89. const location = {query: {}};
  90. const wrapper = createWrapper(undefined, location);
  91. expect(wrapper.router.replace).toHaveBeenCalledWith({
  92. pathname: '/organizations/org-slug/alerts/new/metric',
  93. query: {
  94. aggregate: 'count()',
  95. dataset: 'events',
  96. eventTypes: 'error',
  97. project: 'project-slug',
  98. },
  99. });
  100. });
  101. describe('Issue Alert', function () {
  102. it('loads default values', function () {
  103. createWrapper();
  104. expect(screen.getByText('All Environments')).toBeInTheDocument();
  105. expect(screen.getAllByDisplayValue('all')).toHaveLength(2);
  106. expect(screen.getByText('30 minutes')).toBeInTheDocument();
  107. });
  108. it('can remove filters', async function () {
  109. createWrapper();
  110. const mock = MockApiClient.addMockResponse({
  111. url: '/projects/org-slug/project-slug/rules/',
  112. method: 'POST',
  113. body: TestStubs.ProjectAlertRule(),
  114. });
  115. // Change name of alert rule
  116. userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
  117. // Add a filter and remove it
  118. await selectEvent.select(screen.getByText('Add optional filter...'), [
  119. 'The issue is older or newer than...',
  120. ]);
  121. userEvent.click(screen.getByLabelText('Delete Node'));
  122. userEvent.click(screen.getByText('Save Rule'));
  123. expect(mock).toHaveBeenCalledWith(
  124. expect.any(String),
  125. expect.objectContaining({
  126. data: {
  127. actionMatch: 'all',
  128. actions: [],
  129. conditions: [],
  130. filterMatch: 'all',
  131. filters: [],
  132. frequency: 30,
  133. name: 'My Rule Name',
  134. owner: null,
  135. },
  136. })
  137. );
  138. });
  139. it('can remove triggers', async function () {
  140. const {organization} = createWrapper();
  141. const mock = MockApiClient.addMockResponse({
  142. url: '/projects/org-slug/project-slug/rules/',
  143. method: 'POST',
  144. body: TestStubs.ProjectAlertRule(),
  145. });
  146. // Change name of alert rule
  147. userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
  148. // Add a trigger and remove it
  149. await selectEvent.select(screen.getByText('Add optional trigger...'), [
  150. 'A new issue is created',
  151. ]);
  152. userEvent.click(screen.getByLabelText('Delete Node'));
  153. expect(trackAdvancedAnalyticsEvent).toHaveBeenCalledWith(
  154. 'edit_alert_rule.add_row',
  155. {
  156. name: 'sentry.rules.conditions.first_seen_event.FirstSeenEventCondition',
  157. organization,
  158. project_id: '2',
  159. type: 'conditions',
  160. }
  161. );
  162. userEvent.click(screen.getByText('Save Rule'));
  163. expect(mock).toHaveBeenCalledWith(
  164. expect.any(String),
  165. expect.objectContaining({
  166. data: {
  167. actionMatch: 'all',
  168. actions: [],
  169. conditions: [],
  170. filterMatch: 'all',
  171. filters: [],
  172. frequency: 30,
  173. name: 'My Rule Name',
  174. owner: null,
  175. },
  176. })
  177. );
  178. });
  179. it('can remove actions', async function () {
  180. createWrapper();
  181. const mock = MockApiClient.addMockResponse({
  182. url: '/projects/org-slug/project-slug/rules/',
  183. method: 'POST',
  184. body: TestStubs.ProjectAlertRule(),
  185. });
  186. // Change name of alert rule
  187. userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
  188. // Add an action and remove it
  189. await selectEvent.select(screen.getByText('Add action...'), [
  190. 'Send a notification to all legacy integrations',
  191. ]);
  192. userEvent.click(screen.getByLabelText('Delete Node'));
  193. userEvent.click(screen.getByText('Save Rule'));
  194. expect(mock).toHaveBeenCalledWith(
  195. expect.any(String),
  196. expect.objectContaining({
  197. data: {
  198. actionMatch: 'all',
  199. actions: [],
  200. conditions: [],
  201. filterMatch: 'all',
  202. filters: [],
  203. frequency: 30,
  204. name: 'My Rule Name',
  205. owner: null,
  206. },
  207. })
  208. );
  209. });
  210. describe('updates and saves', function () {
  211. let mock;
  212. beforeEach(function () {
  213. mock = MockApiClient.addMockResponse({
  214. url: '/projects/org-slug/project-slug/rules/',
  215. method: 'POST',
  216. body: TestStubs.ProjectAlertRule(),
  217. });
  218. });
  219. afterEach(function () {
  220. jest.clearAllMocks();
  221. });
  222. it('environment, action and filter match', async function () {
  223. const wrapper = createWrapper();
  224. // Change target environment
  225. await selectEvent.select(screen.getByText('All Environments'), ['production']);
  226. // Change actionMatch and filterMatch dropdown
  227. const allDropdowns = screen.getAllByText('all');
  228. expect(allDropdowns).toHaveLength(2);
  229. await selectEvent.select(allDropdowns[0], ['any']);
  230. await selectEvent.select(allDropdowns[1], ['any']);
  231. // Change name of alert rule
  232. userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
  233. userEvent.click(screen.getByText('Save Rule'));
  234. expect(mock).toHaveBeenCalledWith(
  235. expect.any(String),
  236. expect.objectContaining({
  237. data: {
  238. actionMatch: 'any',
  239. filterMatch: 'any',
  240. conditions: [],
  241. actions: [],
  242. filters: [],
  243. environment: 'production',
  244. frequency: 30,
  245. name: 'My Rule Name',
  246. owner: null,
  247. },
  248. })
  249. );
  250. expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
  251. await waitFor(() => {
  252. expect(wrapper.router.push).toHaveBeenCalledWith({
  253. pathname: '/organizations/org-slug/alerts/rules/project-slug/1/details/',
  254. });
  255. });
  256. });
  257. it('new condition', async function () {
  258. const wrapper = createWrapper();
  259. // Change name of alert rule
  260. userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
  261. // Add another condition
  262. await selectEvent.select(screen.getByText('Add optional filter...'), [
  263. "The event's tags match {key} {match} {value}",
  264. ]);
  265. // Edit new Condition
  266. userEvent.paste(screen.getByPlaceholderText('key'), 'conditionKey');
  267. userEvent.paste(screen.getByPlaceholderText('value'), 'conditionValue');
  268. await selectEvent.select(screen.getByText('contains'), ['does not equal']);
  269. userEvent.click(screen.getByText('Save Rule'));
  270. expect(mock).toHaveBeenCalledWith(
  271. expect.any(String),
  272. expect.objectContaining({
  273. data: {
  274. actionMatch: 'all',
  275. actions: [],
  276. conditions: [],
  277. filterMatch: 'all',
  278. filters: [
  279. {
  280. id: 'sentry.rules.filters.tagged_event.TaggedEventFilter',
  281. key: 'conditionKey',
  282. match: 'ne',
  283. value: 'conditionValue',
  284. },
  285. ],
  286. frequency: 30,
  287. name: 'My Rule Name',
  288. owner: null,
  289. },
  290. })
  291. );
  292. expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
  293. await waitFor(() => {
  294. expect(wrapper.router.push).toHaveBeenCalledWith({
  295. pathname: '/organizations/org-slug/alerts/rules/project-slug/1/details/',
  296. });
  297. });
  298. });
  299. it('new filter', async function () {
  300. const wrapper = createWrapper();
  301. // Change name of alert rule
  302. userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
  303. // Add a new filter
  304. await selectEvent.select(screen.getByText('Add optional filter...'), [
  305. 'The issue is older or newer than...',
  306. ]);
  307. userEvent.paste(screen.getByPlaceholderText('10'), '12');
  308. userEvent.click(screen.getByText('Save Rule'));
  309. expect(mock).toHaveBeenCalledWith(
  310. expect.any(String),
  311. expect.objectContaining({
  312. data: {
  313. actionMatch: 'all',
  314. filterMatch: 'all',
  315. filters: [
  316. {
  317. id: 'sentry.rules.filters.age_comparison.AgeComparisonFilter',
  318. comparison_type: 'older',
  319. time: 'minute',
  320. value: '12',
  321. },
  322. ],
  323. actions: [],
  324. conditions: [],
  325. frequency: 30,
  326. name: 'My Rule Name',
  327. owner: null,
  328. },
  329. })
  330. );
  331. expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
  332. await waitFor(() => {
  333. expect(wrapper.router.push).toHaveBeenCalledWith({
  334. pathname: '/organizations/org-slug/alerts/rules/project-slug/1/details/',
  335. });
  336. });
  337. });
  338. it('new action', async function () {
  339. const wrapper = createWrapper();
  340. // Change name of alert rule
  341. userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
  342. // Add a new action
  343. await selectEvent.select(screen.getByText('Add action...'), [
  344. 'Issue Owners, Team, or Member',
  345. ]);
  346. // Update action interval
  347. await selectEvent.select(screen.getByText('30 minutes'), ['60 minutes']);
  348. userEvent.click(screen.getByText('Save Rule'));
  349. expect(mock).toHaveBeenCalledWith(
  350. expect.any(String),
  351. expect.objectContaining({
  352. data: {
  353. actionMatch: 'all',
  354. actions: [
  355. {id: 'sentry.mail.actions.NotifyEmailAction', targetType: 'IssueOwners'},
  356. ],
  357. conditions: [],
  358. filterMatch: 'all',
  359. filters: [],
  360. frequency: '60',
  361. name: 'My Rule Name',
  362. owner: null,
  363. },
  364. })
  365. );
  366. expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
  367. await waitFor(() => {
  368. expect(wrapper.router.push).toHaveBeenCalledWith({
  369. pathname: '/organizations/org-slug/alerts/rules/project-slug/1/details/',
  370. });
  371. });
  372. });
  373. });
  374. });
  375. });