index.spec.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import {browserHistory} from 'react-router';
  2. import selectEvent from 'react-select-event';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {
  5. render,
  6. renderGlobalModal,
  7. screen,
  8. userEvent,
  9. waitFor,
  10. } from 'sentry-test/reactTestingLibrary';
  11. import {
  12. addErrorMessage,
  13. addLoadingMessage,
  14. addSuccessMessage,
  15. } from 'sentry/actionCreators/indicator';
  16. import {updateOnboardingTask} from 'sentry/actionCreators/onboardingTasks';
  17. import ProjectsStore from 'sentry/stores/projectsStore';
  18. import {metric} from 'sentry/utils/analytics';
  19. import IssueRuleEditor from 'sentry/views/alerts/rules/issue';
  20. import ProjectAlerts from 'sentry/views/settings/projectAlerts';
  21. jest.unmock('sentry/utils/recreateRoute');
  22. jest.mock('sentry/actionCreators/onboardingTasks');
  23. jest.mock('sentry/actionCreators/indicator', () => ({
  24. addSuccessMessage: jest.fn(),
  25. addErrorMessage: jest.fn(),
  26. addLoadingMessage: jest.fn(),
  27. }));
  28. jest.mock('sentry/utils/analytics', () => ({
  29. metric: {
  30. startTransaction: jest.fn(() => ({
  31. setTag: jest.fn(),
  32. setData: jest.fn(),
  33. })),
  34. endTransaction: jest.fn(),
  35. mark: jest.fn(),
  36. measure: jest.fn(),
  37. },
  38. trackAnalytics: jest.fn(),
  39. }));
  40. const projectAlertRuleDetailsRoutes = [
  41. {
  42. path: '/',
  43. },
  44. {
  45. path: '/settings/',
  46. name: 'Settings',
  47. indexRoute: {},
  48. },
  49. {
  50. name: 'Organization',
  51. path: ':orgId/',
  52. },
  53. {
  54. name: 'Project',
  55. path: 'projects/:projectId/',
  56. },
  57. {},
  58. {
  59. indexRoute: {name: 'General'},
  60. },
  61. {
  62. name: 'Alert Rules',
  63. path: 'alerts/',
  64. indexRoute: {},
  65. },
  66. {
  67. path: 'rules/',
  68. name: 'Rules',
  69. component: null,
  70. indexRoute: {},
  71. childRoutes: [
  72. {path: 'new/', name: 'New'},
  73. {path: ':ruleId/', name: 'Edit'},
  74. ],
  75. },
  76. {path: ':ruleId/', name: 'Edit Alert Rule'},
  77. ];
  78. const createWrapper = (props = {}) => {
  79. const {organization, project, routerContext, router} = initializeOrg(props);
  80. const params = {
  81. projectId: project.slug,
  82. ruleId: router.location.query.createFromDuplicate ? undefined : '1',
  83. };
  84. const onChangeTitleMock = jest.fn();
  85. const wrapper = render(
  86. <ProjectAlerts organization={organization} project={project} params={params}>
  87. <IssueRuleEditor
  88. params={params}
  89. location={router.location}
  90. routes={projectAlertRuleDetailsRoutes}
  91. router={router}
  92. onChangeTitle={onChangeTitleMock}
  93. project={project}
  94. userTeamIds={[]}
  95. />
  96. </ProjectAlerts>,
  97. {context: routerContext}
  98. );
  99. return {
  100. wrapper,
  101. organization,
  102. project,
  103. onChangeTitleMock,
  104. };
  105. };
  106. describe('IssueRuleEditor', function () {
  107. beforeEach(function () {
  108. browserHistory.replace = jest.fn();
  109. MockApiClient.addMockResponse({
  110. url: '/projects/org-slug/project-slug/rules/configuration/',
  111. body: TestStubs.ProjectAlertRuleConfiguration(),
  112. });
  113. MockApiClient.addMockResponse({
  114. url: '/projects/org-slug/project-slug/rules/1/',
  115. body: TestStubs.ProjectAlertRule(),
  116. });
  117. MockApiClient.addMockResponse({
  118. url: '/projects/org-slug/project-slug/environments/',
  119. body: TestStubs.Environments(),
  120. });
  121. MockApiClient.addMockResponse({
  122. url: `/projects/org-slug/project-slug/?expand=hasAlertIntegration`,
  123. body: {},
  124. });
  125. MockApiClient.addMockResponse({
  126. url: `/projects/org-slug/project-slug/ownership/`,
  127. method: 'GET',
  128. body: {
  129. fallthrough: false,
  130. autoAssignment: false,
  131. },
  132. });
  133. ProjectsStore.loadInitialData([TestStubs.Project()]);
  134. });
  135. afterEach(function () {
  136. MockApiClient.clearMockResponses();
  137. jest.clearAllMocks();
  138. ProjectsStore.reset();
  139. });
  140. describe('Edit Rule', function () {
  141. let mock;
  142. const endpoint = '/projects/org-slug/project-slug/rules/1/';
  143. beforeEach(function () {
  144. mock = MockApiClient.addMockResponse({
  145. url: endpoint,
  146. method: 'PUT',
  147. body: TestStubs.ProjectAlertRule(),
  148. });
  149. });
  150. it('gets correct rule name', function () {
  151. const rule = TestStubs.ProjectAlertRule();
  152. mock = MockApiClient.addMockResponse({
  153. url: endpoint,
  154. method: 'GET',
  155. body: rule,
  156. });
  157. const {onChangeTitleMock} = createWrapper();
  158. expect(mock).toHaveBeenCalled();
  159. expect(onChangeTitleMock).toHaveBeenCalledWith(rule.name);
  160. });
  161. it('deletes rule', async function () {
  162. const deleteMock = MockApiClient.addMockResponse({
  163. url: endpoint,
  164. method: 'DELETE',
  165. body: {},
  166. });
  167. createWrapper();
  168. renderGlobalModal();
  169. await userEvent.click(screen.getByLabelText('Delete Rule'));
  170. expect(
  171. await screen.findByText('Are you sure you want to delete this rule?')
  172. ).toBeInTheDocument();
  173. await userEvent.click(screen.getByTestId('confirm-button'));
  174. await waitFor(() => expect(deleteMock).toHaveBeenCalled());
  175. expect(browserHistory.replace).toHaveBeenCalledWith(
  176. '/settings/org-slug/projects/project-slug/alerts/'
  177. );
  178. });
  179. it('sends correct environment value', async function () {
  180. createWrapper();
  181. await selectEvent.select(screen.getByText('staging'), 'production');
  182. await userEvent.click(screen.getByText('Save Rule'));
  183. await waitFor(() =>
  184. expect(mock).toHaveBeenCalledWith(
  185. endpoint,
  186. expect.objectContaining({
  187. data: expect.objectContaining({environment: 'production'}),
  188. })
  189. )
  190. );
  191. expect(metric.startTransaction).toHaveBeenCalledTimes(1);
  192. expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
  193. });
  194. it('strips environment value if "All environments" is selected', async function () {
  195. createWrapper();
  196. await selectEvent.select(screen.getByText('staging'), 'All Environments');
  197. await userEvent.click(screen.getByText('Save Rule'));
  198. await waitFor(() => expect(mock).toHaveBeenCalledTimes(1));
  199. expect(mock).not.toHaveBeenCalledWith(
  200. endpoint,
  201. expect.objectContaining({
  202. data: expect.objectContaining({environment: '__all_environments__'}),
  203. })
  204. );
  205. expect(metric.startTransaction).toHaveBeenCalledTimes(1);
  206. expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
  207. });
  208. it('updates the alert onboarding task', async function () {
  209. createWrapper();
  210. await userEvent.click(screen.getByText('Save Rule'));
  211. await waitFor(() => expect(updateOnboardingTask).toHaveBeenCalledTimes(1));
  212. expect(metric.startTransaction).toHaveBeenCalledTimes(1);
  213. expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
  214. });
  215. it('renders multiple sentry apps at the same time', async () => {
  216. const linearApp = {
  217. id: 'sentry.rules.actions.notify_event_sentry_app.NotifyEventSentryAppAction',
  218. enabled: true,
  219. actionType: 'sentryapp',
  220. service: 'linear',
  221. sentryAppInstallationUuid: 'linear-d864bc2a8755',
  222. prompt: 'Linear',
  223. label: 'Create a Linear issue with these ',
  224. formFields: {
  225. type: 'alert-rule-settings',
  226. uri: '/hooks/sentry/alert-rule-action',
  227. description:
  228. 'When the alert fires automatically create a Linear issue with the following properties.',
  229. required_fields: [
  230. {
  231. name: 'teamId',
  232. label: 'Team',
  233. type: 'select',
  234. uri: '/hooks/sentry/issues/teams',
  235. choices: [['test-6f0b2b4d402b', 'Sentry']],
  236. },
  237. ],
  238. optional_fields: [
  239. // Optional fields removed
  240. ],
  241. },
  242. };
  243. const threadsApp = {
  244. id: 'sentry.rules.actions.notify_event_sentry_app.NotifyEventSentryAppAction',
  245. enabled: true,
  246. actionType: 'sentryapp',
  247. service: 'threads',
  248. sentryAppInstallationUuid: 'threads-987c470e50cc',
  249. prompt: 'Threads',
  250. label: 'Post to a Threads channel with these ',
  251. formFields: {
  252. type: 'alert-rule-settings',
  253. uri: '/sentry/saveAlert',
  254. required_fields: [
  255. {
  256. type: 'select',
  257. label: 'Channel',
  258. name: 'channel',
  259. async: true,
  260. uri: '/sentry/channels',
  261. choices: [],
  262. },
  263. ],
  264. },
  265. };
  266. MockApiClient.addMockResponse({
  267. url: '/projects/org-slug/project-slug/rules/configuration/',
  268. body: {actions: [linearApp, threadsApp], conditions: [], filters: []},
  269. });
  270. createWrapper();
  271. await selectEvent.select(screen.getByText('Add action...'), 'Threads');
  272. await selectEvent.select(screen.getByText('Add action...'), 'Linear');
  273. expect(screen.getByText('Create a Linear issue with these')).toBeInTheDocument();
  274. expect(
  275. screen.getByText('Post to a Threads channel with these')
  276. ).toBeInTheDocument();
  277. });
  278. });
  279. describe('Edit Rule: Slack Channel Look Up', function () {
  280. const uuid = 'xxxx-xxxx-xxxx';
  281. beforeEach(function () {
  282. jest.useFakeTimers();
  283. });
  284. afterEach(function () {
  285. jest.clearAllTimers();
  286. MockApiClient.clearMockResponses();
  287. });
  288. it('success status updates the rule', async function () {
  289. const mockSuccess = MockApiClient.addMockResponse({
  290. url: `/projects/org-slug/project-slug/rule-task/${uuid}/`,
  291. body: {status: 'success', rule: TestStubs.ProjectAlertRule({name: 'Slack Rule'})},
  292. });
  293. MockApiClient.addMockResponse({
  294. url: '/projects/org-slug/project-slug/rules/1/',
  295. method: 'PUT',
  296. statusCode: 202,
  297. body: {uuid},
  298. });
  299. createWrapper();
  300. await userEvent.click(screen.getByText('Save Rule'), {delay: null});
  301. await waitFor(() => expect(addLoadingMessage).toHaveBeenCalledTimes(2));
  302. jest.advanceTimersByTime(1000);
  303. await waitFor(() => expect(mockSuccess).toHaveBeenCalledTimes(1));
  304. jest.advanceTimersByTime(1000);
  305. await waitFor(() => expect(addSuccessMessage).toHaveBeenCalledTimes(1));
  306. expect(screen.getByDisplayValue('Slack Rule')).toBeInTheDocument();
  307. });
  308. it('pending status keeps loading true', async function () {
  309. const pollingMock = MockApiClient.addMockResponse({
  310. url: `/projects/org-slug/project-slug/rule-task/${uuid}/`,
  311. body: {status: 'pending'},
  312. });
  313. MockApiClient.addMockResponse({
  314. url: '/projects/org-slug/project-slug/rules/1/',
  315. method: 'PUT',
  316. statusCode: 202,
  317. body: {uuid},
  318. });
  319. createWrapper();
  320. await userEvent.click(screen.getByText('Save Rule'), {delay: null});
  321. await waitFor(() => expect(addLoadingMessage).toHaveBeenCalledTimes(2));
  322. jest.advanceTimersByTime(1000);
  323. await waitFor(() => expect(pollingMock).toHaveBeenCalledTimes(1));
  324. expect(screen.getByTestId('loading-mask')).toBeInTheDocument();
  325. });
  326. it('failed status renders error message', async function () {
  327. const mockFailed = MockApiClient.addMockResponse({
  328. url: `/projects/org-slug/project-slug/rule-task/${uuid}/`,
  329. body: {status: 'failed'},
  330. });
  331. MockApiClient.addMockResponse({
  332. url: '/projects/org-slug/project-slug/rules/1/',
  333. method: 'PUT',
  334. statusCode: 202,
  335. body: {uuid},
  336. });
  337. createWrapper();
  338. await userEvent.click(screen.getByText('Save Rule'), {delay: null});
  339. await waitFor(() => expect(addLoadingMessage).toHaveBeenCalledTimes(2));
  340. jest.advanceTimersByTime(1000);
  341. await waitFor(() => expect(mockFailed).toHaveBeenCalledTimes(1));
  342. expect(screen.getByText('An error occurred')).toBeInTheDocument();
  343. expect(addErrorMessage).toHaveBeenCalledTimes(1);
  344. });
  345. });
  346. describe('Duplicate Rule', function () {
  347. let mock;
  348. const rule = TestStubs.ProjectAlertRule();
  349. const endpoint = `/projects/org-slug/project-slug/rules/${rule.id}/`;
  350. beforeEach(function () {
  351. mock = MockApiClient.addMockResponse({
  352. url: endpoint,
  353. method: 'GET',
  354. body: rule,
  355. });
  356. });
  357. it('gets correct rule to duplicate and renders fields correctly', function () {
  358. createWrapper({
  359. organization: {
  360. access: ['alerts:write'],
  361. },
  362. router: {
  363. location: {
  364. query: {
  365. createFromDuplicate: true,
  366. duplicateRuleId: `${rule.id}`,
  367. },
  368. },
  369. },
  370. });
  371. expect(mock).toHaveBeenCalled();
  372. expect(screen.getByTestId('alert-name')).toHaveValue(`${rule.name} copy`);
  373. });
  374. });
  375. });