create.spec.jsx 13 KB

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