ruleDetails.spec.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import {browserHistory} from 'react-router';
  2. import moment from 'moment';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import AlertRuleDetails from './ruleDetails';
  7. describe('AlertRuleDetails', () => {
  8. const context = initializeOrg();
  9. const organization = context.organization;
  10. const project = TestStubs.Project();
  11. const rule = TestStubs.ProjectAlertRule({
  12. lastTriggered: moment().subtract(2, 'day').format(),
  13. });
  14. const member = TestStubs.Member();
  15. const createWrapper = (props: any = {}, newContext?: any, org = organization) => {
  16. const router = newContext ? newContext.router : context.router;
  17. const routerContext = newContext ? newContext.routerContext : context.routerContext;
  18. return render(
  19. <AlertRuleDetails
  20. params={{
  21. orgId: org.slug,
  22. projectId: project.slug,
  23. ruleId: rule.id,
  24. }}
  25. location={{...router.location, query: {}}}
  26. router={router}
  27. {...props}
  28. />,
  29. {context: routerContext, organization: org}
  30. );
  31. };
  32. beforeEach(() => {
  33. browserHistory.push = jest.fn();
  34. MockApiClient.addMockResponse({
  35. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  36. body: rule,
  37. match: [MockApiClient.matchQuery({expand: 'lastTriggered'})],
  38. });
  39. MockApiClient.addMockResponse({
  40. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/stats/`,
  41. body: [],
  42. });
  43. MockApiClient.addMockResponse({
  44. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/group-history/`,
  45. body: [
  46. {
  47. count: 1,
  48. group: TestStubs.Group(),
  49. lastTriggered: moment('Apr 11, 2019 1:08:59 AM UTC').format(),
  50. eventId: 'eventId',
  51. },
  52. ],
  53. headers: {
  54. Link:
  55. '<https://sentry.io/api/0/projects/org-slug/project-slug/rules/1/group-history/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1", ' +
  56. '<https://sentry.io/api/0/projects/org-slug/project-slug/rules/1/group-history/?cursor=0:100:0>; rel="next"; results="true"; cursor="0:100:0"',
  57. },
  58. });
  59. MockApiClient.addMockResponse({
  60. url: `/organizations/${organization.slug}/projects/`,
  61. body: [project],
  62. });
  63. MockApiClient.addMockResponse({
  64. url: `/organizations/${organization.slug}/users/`,
  65. body: [member],
  66. });
  67. ProjectsStore.init();
  68. ProjectsStore.loadInitialData([project]);
  69. });
  70. afterEach(() => {
  71. ProjectsStore.reset();
  72. MockApiClient.clearMockResponses();
  73. });
  74. it('displays alert rule with list of issues', async () => {
  75. createWrapper();
  76. expect(await screen.findAllByText('My alert rule')).toHaveLength(2);
  77. expect(screen.getByText('RequestError:')).toBeInTheDocument();
  78. expect(screen.getByText('Apr 11, 2019 1:08:59 AM UTC')).toBeInTheDocument();
  79. expect(screen.getByText('RequestError:')).toHaveAttribute(
  80. 'href',
  81. expect.stringMatching(
  82. RegExp(
  83. `/organizations/${organization.slug}/issues/${
  84. TestStubs.Group().id
  85. }/events/eventId.*`
  86. )
  87. )
  88. );
  89. });
  90. it('should allow paginating results', async () => {
  91. createWrapper();
  92. expect(await screen.findByLabelText('Next')).toBeEnabled();
  93. await userEvent.click(screen.getByLabelText('Next'));
  94. expect(browserHistory.push).toHaveBeenCalledWith({
  95. pathname: '/mock-pathname/',
  96. query: {
  97. cursor: '0:100:0',
  98. },
  99. });
  100. });
  101. it('should reset pagination cursor on date change', async () => {
  102. createWrapper();
  103. expect(await screen.findByText('Last 7 days')).toBeInTheDocument();
  104. await userEvent.click(screen.getByText('Last 7 days'));
  105. await userEvent.click(screen.getByText('Last 24 hours'));
  106. expect(context.router.push).toHaveBeenCalledWith(
  107. expect.objectContaining({
  108. query: {
  109. pageStatsPeriod: '24h',
  110. cursor: undefined,
  111. pageEnd: undefined,
  112. pageStart: undefined,
  113. pageUtc: undefined,
  114. },
  115. })
  116. );
  117. });
  118. it('should show the time since last triggered in sidebar', async () => {
  119. createWrapper();
  120. expect(await screen.findAllByText('Last Triggered')).toHaveLength(2);
  121. expect(screen.getByText('2 days ago')).toBeInTheDocument();
  122. });
  123. it('renders not found on 404', async () => {
  124. MockApiClient.addMockResponse({
  125. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  126. statusCode: 404,
  127. body: {},
  128. match: [MockApiClient.matchQuery({expand: 'lastTriggered'})],
  129. });
  130. createWrapper();
  131. expect(
  132. await screen.findByText('The alert rule you were looking for was not found.')
  133. ).toBeInTheDocument();
  134. });
  135. it('renders incompatible rule filter', async () => {
  136. const incompatibleRule = TestStubs.ProjectAlertRule({
  137. conditions: [
  138. {id: 'sentry.rules.conditions.first_seen_event.FirstSeenEventCondition'},
  139. {id: 'sentry.rules.conditions.regression_event.RegressionEventCondition'},
  140. ],
  141. });
  142. MockApiClient.addMockResponse({
  143. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  144. body: incompatibleRule,
  145. match: [MockApiClient.matchQuery({expand: 'lastTriggered'})],
  146. });
  147. createWrapper();
  148. expect(
  149. await screen.findByText(
  150. 'The conditions in this alert rule conflict and might not be working properly.'
  151. )
  152. ).toBeInTheDocument();
  153. });
  154. it('incompatible rule banner hidden for good rule', async () => {
  155. createWrapper();
  156. expect(await screen.findAllByText('My alert rule')).toHaveLength(2);
  157. expect(
  158. screen.queryByText(
  159. 'The conditions in this alert rule conflict and might not be working properly.'
  160. )
  161. ).not.toBeInTheDocument();
  162. });
  163. it('rule disabled banner because of missing actions and hides some actions', async () => {
  164. MockApiClient.addMockResponse({
  165. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  166. body: TestStubs.ProjectAlertRule({
  167. actions: [],
  168. status: 'disabled',
  169. }),
  170. match: [MockApiClient.matchQuery({expand: 'lastTriggered'})],
  171. });
  172. createWrapper();
  173. expect(
  174. await screen.findByText(
  175. 'This alert is disabled due to missing actions. Please edit the alert rule to enable this alert.'
  176. )
  177. ).toBeInTheDocument();
  178. expect(screen.getByRole('button', {name: 'Edit to enable'})).toBeInTheDocument();
  179. expect(screen.getByRole('button', {name: 'Duplicate'})).toBeDisabled();
  180. expect(screen.getByRole('button', {name: 'Mute for me'})).toBeDisabled();
  181. });
  182. it('rule disabled banner generic', async () => {
  183. MockApiClient.addMockResponse({
  184. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  185. body: TestStubs.ProjectAlertRule({
  186. status: 'disabled',
  187. }),
  188. match: [MockApiClient.matchQuery({expand: 'lastTriggered'})],
  189. });
  190. createWrapper();
  191. expect(
  192. await screen.findByText(
  193. 'This alert is disabled due to its configuration and needs to be edited to be enabled.'
  194. )
  195. ).toBeInTheDocument();
  196. });
  197. it('renders the mute button and can mute/unmute alerts', async () => {
  198. const postRequest = MockApiClient.addMockResponse({
  199. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/snooze/`,
  200. method: 'POST',
  201. });
  202. const deleteRequest = MockApiClient.addMockResponse({
  203. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/snooze/`,
  204. method: 'DELETE',
  205. });
  206. createWrapper();
  207. expect(await screen.findByText('Mute for everyone')).toBeInTheDocument();
  208. await userEvent.click(screen.getByRole('button', {name: 'Mute for everyone'}));
  209. expect(postRequest).toHaveBeenCalledWith(
  210. expect.anything(),
  211. expect.objectContaining({data: {target: 'everyone'}})
  212. );
  213. expect(await screen.findByText('Unmute')).toBeInTheDocument();
  214. await userEvent.click(screen.getByRole('button', {name: 'Unmute'}));
  215. expect(deleteRequest).toHaveBeenCalledTimes(1);
  216. });
  217. it('mutes alert if query parameter is set', async () => {
  218. const request = MockApiClient.addMockResponse({
  219. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/snooze/`,
  220. method: 'POST',
  221. });
  222. const contextWithQueryParam = initializeOrg({
  223. router: {
  224. location: {query: {mute: '1'}},
  225. },
  226. });
  227. createWrapper({}, contextWithQueryParam);
  228. expect(await screen.findByText('Unmute')).toBeInTheDocument();
  229. expect(request).toHaveBeenCalledWith(
  230. expect.anything(),
  231. expect.objectContaining({
  232. data: {target: 'everyone'},
  233. })
  234. );
  235. });
  236. it('mute button is disabled if no alerts:write permission', async () => {
  237. const orgWithoutAccess = {
  238. access: [],
  239. };
  240. const contextWithoutAccess = initializeOrg({
  241. organization: orgWithoutAccess,
  242. });
  243. createWrapper({}, contextWithoutAccess, orgWithoutAccess);
  244. expect(await screen.findByRole('button', {name: 'Mute for everyone'})).toBeDisabled();
  245. });
  246. it('inserts user email into rule notify action', async () => {
  247. // Alert rule with "send a notification to member" action
  248. const sendNotificationRule = TestStubs.ProjectAlertRule({
  249. actions: [
  250. {
  251. id: 'sentry.mail.actions.NotifyEmailAction',
  252. name: 'Send a notification to Member and if none can be found then send a notification to ActiveMembers',
  253. targetIdentifier: member.id,
  254. targetType: 'Member',
  255. },
  256. ],
  257. });
  258. MockApiClient.addMockResponse({
  259. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  260. body: sendNotificationRule,
  261. });
  262. createWrapper();
  263. expect(
  264. await screen.findByText(`Send a notification to ${member.email}`)
  265. ).toBeInTheDocument();
  266. });
  267. });