alertRulesList.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {
  3. act,
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. within,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import OrganizationStore from 'sentry/stores/organizationStore';
  11. import ProjectsStore from 'sentry/stores/projectsStore';
  12. import TeamStore from 'sentry/stores/teamStore';
  13. import {IncidentStatus} from 'sentry/views/alerts/types';
  14. import AlertRulesList from './alertRulesList';
  15. jest.mock('sentry/utils/analytics');
  16. describe('AlertRulesList', () => {
  17. const defaultOrg = TestStubs.Organization({
  18. access: ['alerts:write'],
  19. });
  20. TeamStore.loadInitialData([TestStubs.Team()], false, null);
  21. let rulesMock!: jest.Mock;
  22. let projectMock!: jest.Mock;
  23. const pageLinks =
  24. '<https://sentry.io/api/0/organizations/org-slug/combined-rules/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1", ' +
  25. '<https://sentry.io/api/0/organizations/org-slug/combined-rules/?cursor=0:100:0>; rel="next"; results="true"; cursor="0:100:0"';
  26. beforeEach(() => {
  27. rulesMock = MockApiClient.addMockResponse({
  28. url: '/organizations/org-slug/combined-rules/',
  29. headers: {Link: pageLinks},
  30. body: [
  31. TestStubs.ProjectAlertRule({
  32. id: '123',
  33. name: 'First Issue Alert',
  34. projects: ['earth'],
  35. createdBy: {name: 'Samwise', id: 1, email: ''},
  36. }),
  37. TestStubs.MetricRule({
  38. id: '345',
  39. projects: ['earth'],
  40. latestIncident: TestStubs.Incident({
  41. status: IncidentStatus.CRITICAL,
  42. }),
  43. }),
  44. TestStubs.MetricRule({
  45. id: '678',
  46. projects: ['earth'],
  47. latestIncident: null,
  48. }),
  49. ],
  50. });
  51. projectMock = MockApiClient.addMockResponse({
  52. url: '/organizations/org-slug/projects/',
  53. body: [
  54. TestStubs.Project({
  55. slug: 'earth',
  56. platform: 'javascript',
  57. teams: [TestStubs.Team()],
  58. }),
  59. ],
  60. });
  61. act(() => OrganizationStore.onUpdate(defaultOrg, {replace: true}));
  62. act(() => ProjectsStore.loadInitialData([]));
  63. });
  64. afterEach(() => {
  65. act(() => ProjectsStore.reset());
  66. MockApiClient.clearMockResponses();
  67. jest.clearAllMocks();
  68. });
  69. it('displays list', async () => {
  70. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  71. render(<AlertRulesList />, {context: routerContext, organization});
  72. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  73. expect(projectMock).toHaveBeenLastCalledWith(
  74. expect.anything(),
  75. expect.objectContaining({
  76. query: expect.objectContaining({query: 'slug:earth'}),
  77. })
  78. );
  79. expect(screen.getAllByTestId('badge-display-name')[0]).toHaveTextContent('earth');
  80. });
  81. it('displays empty state', async () => {
  82. MockApiClient.addMockResponse({
  83. url: '/organizations/org-slug/combined-rules/',
  84. body: [],
  85. });
  86. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  87. render(<AlertRulesList />, {context: routerContext, organization});
  88. expect(
  89. await screen.findByText('No alert rules found for the current query.')
  90. ).toBeInTheDocument();
  91. expect(rulesMock).toHaveBeenCalledTimes(0);
  92. });
  93. it('displays team dropdown context if unassigned', async () => {
  94. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  95. render(<AlertRulesList />, {context: routerContext, organization});
  96. const assignee = (await screen.findAllByTestId('alert-row-assignee'))[0];
  97. const btn = within(assignee).getAllByRole('button')[0];
  98. expect(assignee).toBeInTheDocument();
  99. expect(btn).toBeInTheDocument();
  100. await userEvent.click(btn, {skipHover: true});
  101. expect(screen.getByText('#team-slug')).toBeInTheDocument();
  102. expect(within(assignee).getByText('Unassigned')).toBeInTheDocument();
  103. });
  104. it('assigns rule to team from unassigned', async () => {
  105. const assignMock = MockApiClient.addMockResponse({
  106. method: 'PUT',
  107. url: '/projects/org-slug/earth/rules/123/',
  108. body: [],
  109. });
  110. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  111. render(<AlertRulesList />, {context: routerContext, organization});
  112. const assignee = (await screen.findAllByTestId('alert-row-assignee'))[0];
  113. const btn = within(assignee).getAllByRole('button')[0];
  114. expect(assignee).toBeInTheDocument();
  115. expect(btn).toBeInTheDocument();
  116. await userEvent.click(btn, {skipHover: true});
  117. await userEvent.click(screen.getByText('#team-slug'));
  118. expect(assignMock).toHaveBeenCalledWith(
  119. '/projects/org-slug/earth/rules/123/',
  120. expect.objectContaining({
  121. data: expect.objectContaining({owner: 'team:1'}),
  122. })
  123. );
  124. });
  125. it('displays dropdown context menu with actions', async () => {
  126. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  127. render(<AlertRulesList />, {context: routerContext, organization});
  128. const actions = (await screen.findAllByRole('button', {name: 'Actions'}))[0];
  129. expect(actions).toBeInTheDocument();
  130. await userEvent.click(actions);
  131. expect(screen.getByText('Edit')).toBeInTheDocument();
  132. expect(screen.getByText('Delete')).toBeInTheDocument();
  133. expect(screen.getByText('Duplicate')).toBeInTheDocument();
  134. });
  135. it('deletes a rule', async () => {
  136. const {routerContext, organization} = initializeOrg({
  137. organization: defaultOrg,
  138. });
  139. const deletedRuleName = 'First Issue Alert';
  140. MockApiClient.addMockResponse({
  141. url: '/organizations/org-slug/combined-rules/',
  142. headers: {Link: pageLinks},
  143. body: [
  144. TestStubs.ProjectAlertRule({
  145. id: '123',
  146. name: deletedRuleName,
  147. projects: ['earth'],
  148. createdBy: {name: 'Samwise', id: 1, email: ''},
  149. }),
  150. ],
  151. });
  152. const deleteMock = MockApiClient.addMockResponse({
  153. url: `/projects/${organization.slug}/earth/rules/123/`,
  154. method: 'DELETE',
  155. body: {},
  156. });
  157. render(<AlertRulesList />, {context: routerContext, organization});
  158. renderGlobalModal();
  159. const actions = (await screen.findAllByRole('button', {name: 'Actions'}))[0];
  160. // Add a new response to the mock with no rules
  161. const emptyListMock = MockApiClient.addMockResponse({
  162. url: '/organizations/org-slug/combined-rules/',
  163. headers: {Link: pageLinks},
  164. body: [],
  165. });
  166. expect(screen.queryByText(deletedRuleName)).toBeInTheDocument();
  167. await userEvent.click(actions);
  168. await userEvent.click(screen.getByText('Delete'));
  169. await userEvent.click(screen.getByRole('button', {name: 'Delete Rule'}));
  170. expect(deleteMock).toHaveBeenCalledTimes(1);
  171. expect(emptyListMock).toHaveBeenCalledTimes(1);
  172. expect(screen.queryByText(deletedRuleName)).not.toBeInTheDocument();
  173. });
  174. it('sends user to new alert page on duplicate action', async () => {
  175. const {routerContext, organization, router} = initializeOrg({
  176. organization: defaultOrg,
  177. });
  178. render(<AlertRulesList />, {context: routerContext, organization});
  179. const actions = (await screen.findAllByRole('button', {name: 'Actions'}))[0];
  180. expect(actions).toBeInTheDocument();
  181. await userEvent.click(actions);
  182. const duplicate = await screen.findByText('Duplicate');
  183. expect(duplicate).toBeInTheDocument();
  184. await userEvent.click(duplicate);
  185. expect(router.push).toHaveBeenCalledWith({
  186. pathname: '/organizations/org-slug/alerts/new/issue/',
  187. query: {
  188. createFromDuplicate: true,
  189. duplicateRuleId: '123',
  190. project: 'earth',
  191. referrer: 'alert_stream',
  192. },
  193. });
  194. });
  195. it('sorts by name', async () => {
  196. const {routerContext, organization} = initializeOrg({
  197. organization: defaultOrg,
  198. router: {
  199. location: TestStubs.location({
  200. query: {asc: '1', sort: 'name'},
  201. // Sort by the name column
  202. search: '?asc=1&sort=name`',
  203. }),
  204. },
  205. });
  206. render(<AlertRulesList />, {context: routerContext, organization});
  207. expect(await screen.findByText('Alert Rule')).toHaveAttribute(
  208. 'aria-sort',
  209. 'ascending'
  210. );
  211. expect(rulesMock).toHaveBeenCalledTimes(1);
  212. expect(rulesMock).toHaveBeenCalledWith(
  213. '/organizations/org-slug/combined-rules/',
  214. expect.objectContaining({
  215. query: expect.objectContaining({sort: 'name', asc: '1'}),
  216. })
  217. );
  218. });
  219. it('disables the new alert button for members', async () => {
  220. const noAccessOrg = {
  221. ...defaultOrg,
  222. access: [],
  223. };
  224. const {routerContext, organization} = initializeOrg({organization: noAccessOrg});
  225. render(<AlertRulesList />, {context: routerContext, organization});
  226. expect(await screen.findByLabelText('Create Alert')).toBeDisabled();
  227. });
  228. it('searches by name', async () => {
  229. const {routerContext, organization, router} = initializeOrg();
  230. render(<AlertRulesList />, {context: routerContext, organization});
  231. const search = await screen.findByPlaceholderText('Search by name');
  232. expect(search).toBeInTheDocument();
  233. const testQuery = 'test name';
  234. await userEvent.type(search, `${testQuery}{enter}`);
  235. expect(router.push).toHaveBeenCalledWith(
  236. expect.objectContaining({
  237. query: {
  238. name: testQuery,
  239. },
  240. })
  241. );
  242. });
  243. it('uses empty team query parameter when removing all teams', async () => {
  244. const {routerContext, organization, router} = initializeOrg({
  245. router: {
  246. location: TestStubs.location({
  247. query: {team: 'myteams'},
  248. search: '?team=myteams`',
  249. }),
  250. },
  251. });
  252. render(<AlertRulesList />, {context: routerContext, organization});
  253. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  254. await userEvent.click(await screen.findByRole('button', {name: 'My Teams'}));
  255. // Uncheck myteams
  256. const myTeams = await screen.findAllByText('My Teams');
  257. await userEvent.click(myTeams[1]);
  258. expect(router.push).toHaveBeenCalledWith(
  259. expect.objectContaining({
  260. query: {
  261. team: '',
  262. },
  263. })
  264. );
  265. });
  266. it('displays metric alert status', async () => {
  267. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  268. render(<AlertRulesList />, {context: routerContext, organization});
  269. const rules = await screen.findAllByText('My Incident Rule');
  270. expect(rules[0]).toBeInTheDocument();
  271. expect(screen.getByText('Triggered')).toBeInTheDocument();
  272. expect(screen.getByText('Above 70')).toBeInTheDocument();
  273. expect(screen.getByText('Below 36')).toBeInTheDocument();
  274. expect(screen.getAllByTestId('alert-badge')[0]).toBeInTheDocument();
  275. });
  276. it('displays issue alert disabled', async () => {
  277. MockApiClient.addMockResponse({
  278. url: '/organizations/org-slug/combined-rules/',
  279. headers: {Link: pageLinks},
  280. body: [
  281. TestStubs.ProjectAlertRule({
  282. name: 'First Issue Alert',
  283. projects: ['earth'],
  284. status: 'disabled',
  285. }),
  286. ],
  287. });
  288. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  289. render(<AlertRulesList />, {context: routerContext, organization});
  290. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  291. expect(screen.getByText('Disabled')).toBeInTheDocument();
  292. });
  293. it('displays issue alert disabled instead of muted', async () => {
  294. MockApiClient.addMockResponse({
  295. url: '/organizations/org-slug/combined-rules/',
  296. headers: {Link: pageLinks},
  297. body: [
  298. TestStubs.ProjectAlertRule({
  299. name: 'First Issue Alert',
  300. projects: ['earth'],
  301. // both disabled and muted
  302. status: 'disabled',
  303. snooze: true,
  304. }),
  305. ],
  306. });
  307. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  308. render(<AlertRulesList />, {context: routerContext, organization});
  309. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  310. expect(screen.getByText('Disabled')).toBeInTheDocument();
  311. expect(screen.queryByText('Muted')).not.toBeInTheDocument();
  312. });
  313. it('displays issue alert muted', async () => {
  314. MockApiClient.addMockResponse({
  315. url: '/organizations/org-slug/combined-rules/',
  316. headers: {Link: pageLinks},
  317. body: [
  318. TestStubs.ProjectAlertRule({
  319. name: 'First Issue Alert',
  320. projects: ['earth'],
  321. snooze: true,
  322. }),
  323. ],
  324. });
  325. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  326. render(<AlertRulesList />, {context: routerContext, organization});
  327. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  328. expect(screen.getByText('Muted')).toBeInTheDocument();
  329. });
  330. it('displays metric alert muted', async () => {
  331. MockApiClient.addMockResponse({
  332. url: '/organizations/org-slug/combined-rules/',
  333. headers: {Link: pageLinks},
  334. body: [
  335. TestStubs.MetricRule({
  336. projects: ['earth'],
  337. snooze: true,
  338. }),
  339. ],
  340. });
  341. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  342. render(<AlertRulesList />, {context: routerContext, organization});
  343. expect(await screen.findByText('My Incident Rule')).toBeInTheDocument();
  344. expect(screen.getByText('Muted')).toBeInTheDocument();
  345. });
  346. it('sorts by alert rule', async () => {
  347. const {routerContext, organization} = initializeOrg({organization: defaultOrg});
  348. render(<AlertRulesList />, {context: routerContext, organization});
  349. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  350. expect(rulesMock).toHaveBeenCalledWith(
  351. '/organizations/org-slug/combined-rules/',
  352. expect.objectContaining({
  353. query: {
  354. expand: ['latestIncident', 'lastTriggered'],
  355. sort: ['incident_status', 'date_triggered'],
  356. team: ['myteams', 'unassigned'],
  357. },
  358. })
  359. );
  360. });
  361. it('preserves empty team query parameter on pagination', async () => {
  362. const {routerContext, organization, router} = initializeOrg({
  363. organization: defaultOrg,
  364. });
  365. render(<AlertRulesList />, {context: routerContext, organization});
  366. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  367. await userEvent.click(screen.getByLabelText('Next'));
  368. expect(router.push).toHaveBeenCalledWith(
  369. expect.objectContaining({
  370. query: {
  371. team: '',
  372. cursor: '0:100:0',
  373. },
  374. })
  375. );
  376. });
  377. });