index.spec.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  3. import OrganizationStore from 'sentry/stores/organizationStore';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import TeamStore from 'sentry/stores/teamStore';
  6. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  7. import AlertRulesList from 'sentry/views/alerts/list/rules';
  8. import {IncidentStatus} from 'sentry/views/alerts/types';
  9. import {OrganizationContext} from 'sentry/views/organizationContext';
  10. jest.mock('sentry/utils/analytics/trackAdvancedAnalyticsEvent');
  11. describe('AlertRulesList', () => {
  12. const {routerContext, organization, router} = initializeOrg({
  13. organization: {
  14. access: ['alerts:write'],
  15. features: ['duplicate-alert-rule'],
  16. },
  17. });
  18. TeamStore.loadInitialData([TestStubs.Team()], false, null);
  19. let rulesMock;
  20. let projectMock;
  21. const pageLinks =
  22. '<https://sentry.io/api/0/organizations/org-slug/combined-rules/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1", ' +
  23. '<https://sentry.io/api/0/organizations/org-slug/combined-rules/?cursor=0:100:0>; rel="next"; results="true"; cursor="0:100:0"';
  24. const getComponent = props => (
  25. <OrganizationContext.Provider value={organization}>
  26. <AlertRulesList
  27. organization={organization}
  28. params={{orgId: organization.slug}}
  29. location={{query: {}, search: ''}}
  30. router={router}
  31. {...props}
  32. />
  33. </OrganizationContext.Provider>
  34. );
  35. const createWrapper = props => render(getComponent(props), {context: routerContext});
  36. beforeEach(() => {
  37. rulesMock = MockApiClient.addMockResponse({
  38. url: '/organizations/org-slug/combined-rules/',
  39. headers: {Link: pageLinks},
  40. body: [
  41. TestStubs.ProjectAlertRule({
  42. id: '123',
  43. name: 'First Issue Alert',
  44. projects: ['earth'],
  45. createdBy: {name: 'Samwise', id: 1, email: ''},
  46. }),
  47. TestStubs.MetricRule({
  48. id: '345',
  49. projects: ['earth'],
  50. latestIncident: TestStubs.Incident({
  51. status: IncidentStatus.CRITICAL,
  52. }),
  53. }),
  54. TestStubs.MetricRule({
  55. id: '678',
  56. projects: ['earth'],
  57. latestIncident: null,
  58. }),
  59. ],
  60. });
  61. projectMock = MockApiClient.addMockResponse({
  62. url: '/organizations/org-slug/projects/',
  63. body: [
  64. TestStubs.Project({
  65. slug: 'earth',
  66. platform: 'javascript',
  67. teams: [TestStubs.Team()],
  68. }),
  69. ],
  70. });
  71. act(() => OrganizationStore.onUpdate(organization, {replace: true}));
  72. act(() => ProjectsStore.loadInitialData([]));
  73. });
  74. afterEach(() => {
  75. act(() => ProjectsStore.reset());
  76. MockApiClient.clearMockResponses();
  77. trackAdvancedAnalyticsEvent.mockClear();
  78. });
  79. it('displays list', async () => {
  80. createWrapper();
  81. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  82. expect(projectMock).toHaveBeenLastCalledWith(
  83. expect.anything(),
  84. expect.objectContaining({
  85. query: expect.objectContaining({query: 'slug:earth'}),
  86. })
  87. );
  88. expect(screen.getAllByTestId('badge-display-name')[0]).toHaveTextContent('earth');
  89. expect(trackAdvancedAnalyticsEvent).toHaveBeenCalledWith(
  90. 'alert_rules.viewed',
  91. expect.objectContaining({
  92. sort: 'incident_status,date_triggered',
  93. })
  94. );
  95. });
  96. it('displays empty state', async () => {
  97. MockApiClient.addMockResponse({
  98. url: '/organizations/org-slug/combined-rules/',
  99. body: [],
  100. });
  101. createWrapper();
  102. expect(
  103. await screen.findByText('No alert rules found for the current query.')
  104. ).toBeInTheDocument();
  105. expect(rulesMock).toHaveBeenCalledTimes(0);
  106. });
  107. it('displays team dropdown context if unassigned', async () => {
  108. createWrapper();
  109. const assignee = (await screen.findAllByTestId('alert-row-assignee'))[0];
  110. const btn = within(assignee).getAllByRole('button')[0];
  111. expect(assignee).toBeInTheDocument();
  112. expect(btn).toBeInTheDocument();
  113. userEvent.click(btn, {skipHover: true});
  114. expect(screen.getByText('#team-slug')).toBeInTheDocument();
  115. expect(within(assignee).getByText('Unassigned')).toBeInTheDocument();
  116. });
  117. it('assigns rule to team from unassigned', async () => {
  118. const assignMock = MockApiClient.addMockResponse({
  119. method: 'PUT',
  120. url: '/projects/org-slug/earth/rules/123/',
  121. body: [],
  122. });
  123. createWrapper();
  124. const assignee = (await screen.findAllByTestId('alert-row-assignee'))[0];
  125. const btn = within(assignee).getAllByRole('button')[0];
  126. expect(assignee).toBeInTheDocument();
  127. expect(btn).toBeInTheDocument();
  128. userEvent.click(btn, {skipHover: true});
  129. userEvent.click(screen.getByText('#team-slug'));
  130. expect(assignMock).toHaveBeenCalledWith(
  131. '/projects/org-slug/earth/rules/123/',
  132. expect.objectContaining({
  133. data: expect.objectContaining({owner: 'team:1'}),
  134. })
  135. );
  136. });
  137. it('displays dropdown context menu with actions', async () => {
  138. createWrapper();
  139. const actions = (await screen.findAllByTestId('alert-row-actions'))[0];
  140. expect(actions).toBeInTheDocument();
  141. userEvent.click(actions);
  142. expect(screen.getByText('Edit')).toBeInTheDocument();
  143. expect(screen.getByText('Delete')).toBeInTheDocument();
  144. expect(screen.getByText('Duplicate')).toBeInTheDocument();
  145. });
  146. it('sends user to new alert page on duplicate action', async () => {
  147. createWrapper();
  148. const actions = (await screen.findAllByTestId('alert-row-actions'))[0];
  149. expect(actions).toBeInTheDocument();
  150. userEvent.click(actions);
  151. const duplicate = await screen.findByText('Duplicate');
  152. expect(duplicate).toBeInTheDocument();
  153. userEvent.click(duplicate);
  154. expect(router.push).toHaveBeenCalledWith({
  155. pathname: '/organizations/org-slug/alerts/new/issue/',
  156. query: {
  157. createFromDuplicate: true,
  158. duplicateRuleId: '123',
  159. project: 'earth',
  160. referrer: 'alert_stream',
  161. },
  162. });
  163. });
  164. it('sorts by name', async () => {
  165. const {rerender} = createWrapper();
  166. // The name column is not used for sorting
  167. expect(await screen.findByText('Alert Rule')).toHaveAttribute('aria-sort', 'none');
  168. // Sort by the name column
  169. rerender(
  170. getComponent({
  171. location: {
  172. query: {asc: '1', sort: 'name'},
  173. search: '?asc=1&sort=name`',
  174. },
  175. })
  176. );
  177. expect(await screen.findByText('Alert Rule')).toHaveAttribute(
  178. 'aria-sort',
  179. 'ascending'
  180. );
  181. expect(rulesMock).toHaveBeenCalledTimes(2);
  182. expect(rulesMock).toHaveBeenCalledWith(
  183. '/organizations/org-slug/combined-rules/',
  184. expect.objectContaining({
  185. query: expect.objectContaining({sort: 'name', asc: '1'}),
  186. })
  187. );
  188. });
  189. it('disables the new alert button for members', async () => {
  190. const noAccessOrg = {
  191. ...organization,
  192. access: [],
  193. };
  194. const {rerender} = createWrapper({organization: noAccessOrg});
  195. expect(await screen.findByLabelText('Create Alert')).toBeDisabled();
  196. // Enabled with access
  197. rerender(getComponent());
  198. expect(await screen.findByLabelText('Create Alert')).toBeEnabled();
  199. });
  200. it('searches by name', async () => {
  201. createWrapper();
  202. const search = await screen.findByPlaceholderText('Search by name');
  203. expect(search).toBeInTheDocument();
  204. const testQuery = 'test name';
  205. userEvent.type(search, `${testQuery}{enter}`);
  206. expect(router.push).toHaveBeenCalledWith(
  207. expect.objectContaining({
  208. query: {
  209. name: testQuery,
  210. expand: ['latestIncident', 'lastTriggered'],
  211. sort: ['incident_status', 'date_triggered'],
  212. team: ['myteams', 'unassigned'],
  213. },
  214. })
  215. );
  216. });
  217. it('uses empty team query parameter when removing all teams', async () => {
  218. const {rerender} = createWrapper();
  219. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  220. rerender(
  221. getComponent({location: {query: {team: 'myteams'}, search: '?team=myteams`'}})
  222. );
  223. userEvent.click(await screen.findByRole('button', {name: 'My Teams'}));
  224. // Uncheck myteams
  225. const myTeams = await screen.findAllByText('My Teams');
  226. userEvent.click(myTeams[1]);
  227. expect(router.push).toHaveBeenCalledWith(
  228. expect.objectContaining({
  229. query: {
  230. expand: ['latestIncident', 'lastTriggered'],
  231. sort: ['incident_status', 'date_triggered'],
  232. team: '',
  233. },
  234. })
  235. );
  236. });
  237. it('displays alert status', async () => {
  238. createWrapper();
  239. const rules = await screen.findAllByText('My Incident Rule');
  240. expect(rules[0]).toBeInTheDocument();
  241. expect(screen.getByText('Triggered')).toBeInTheDocument();
  242. expect(screen.getByText('Above 70')).toBeInTheDocument();
  243. expect(screen.getByText('Below 36')).toBeInTheDocument();
  244. expect(screen.getAllByTestId('alert-badge')[0]).toBeInTheDocument();
  245. });
  246. it('sorts by alert rule', async () => {
  247. createWrapper({organization});
  248. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  249. expect(rulesMock).toHaveBeenCalledWith(
  250. '/organizations/org-slug/combined-rules/',
  251. expect.objectContaining({
  252. query: {
  253. expand: ['latestIncident', 'lastTriggered'],
  254. sort: ['incident_status', 'date_triggered'],
  255. team: ['myteams', 'unassigned'],
  256. },
  257. })
  258. );
  259. });
  260. it('preserves empty team query parameter on pagination', async () => {
  261. createWrapper({
  262. organization,
  263. location: {query: {team: ''}, search: '?team=`'},
  264. });
  265. expect(await screen.findByText('First Issue Alert')).toBeInTheDocument();
  266. userEvent.click(screen.getByLabelText('Next'));
  267. expect(router.push).toHaveBeenCalledWith(
  268. expect.objectContaining({
  269. query: {
  270. expand: ['latestIncident', 'lastTriggered'],
  271. sort: ['incident_status', 'date_triggered'],
  272. team: '',
  273. cursor: '0:100:0',
  274. },
  275. })
  276. );
  277. });
  278. });