ruleDetails.spec.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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('renders the mute button and can mute/unmute alerts', async () => {
  164. const postRequest = MockApiClient.addMockResponse({
  165. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/snooze/`,
  166. method: 'POST',
  167. });
  168. const deleteRequest = MockApiClient.addMockResponse({
  169. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/snooze/`,
  170. method: 'DELETE',
  171. });
  172. createWrapper();
  173. expect(await screen.findByText('Mute for everyone')).toBeInTheDocument();
  174. await userEvent.click(screen.getByRole('button', {name: 'Mute for everyone'}));
  175. expect(postRequest).toHaveBeenCalledWith(
  176. expect.anything(),
  177. expect.objectContaining({data: {target: 'everyone'}})
  178. );
  179. expect(await screen.findByText('Unmute')).toBeInTheDocument();
  180. await userEvent.click(screen.getByRole('button', {name: 'Unmute'}));
  181. expect(deleteRequest).toHaveBeenCalledTimes(1);
  182. });
  183. it('mutes alert if query parameter is set', async () => {
  184. const request = MockApiClient.addMockResponse({
  185. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/snooze/`,
  186. method: 'POST',
  187. });
  188. const contextWithQueryParam = initializeOrg({
  189. router: {
  190. location: {query: {mute: '1'}},
  191. },
  192. });
  193. createWrapper({}, contextWithQueryParam);
  194. expect(await screen.findByText('Unmute')).toBeInTheDocument();
  195. expect(request).toHaveBeenCalledWith(
  196. expect.anything(),
  197. expect.objectContaining({
  198. data: {target: 'everyone'},
  199. })
  200. );
  201. });
  202. it('mute button is disabled if no alerts:write permission', async () => {
  203. const orgWithoutAccess = {
  204. access: [],
  205. };
  206. const contextWithoutAccess = initializeOrg({
  207. organization: orgWithoutAccess,
  208. });
  209. createWrapper({}, contextWithoutAccess, orgWithoutAccess);
  210. expect(await screen.findByRole('button', {name: 'Mute for everyone'})).toBeDisabled();
  211. });
  212. it('inserts user email into rule notify action', async () => {
  213. // Alert rule with "send a notification to member" action
  214. const sendNotificationRule = TestStubs.ProjectAlertRule({
  215. actions: [
  216. {
  217. id: 'sentry.mail.actions.NotifyEmailAction',
  218. name: 'Send a notification to Member and if none can be found then send a notification to ActiveMembers',
  219. targetIdentifier: member.id,
  220. targetType: 'Member',
  221. },
  222. ],
  223. });
  224. MockApiClient.addMockResponse({
  225. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  226. body: sendNotificationRule,
  227. });
  228. createWrapper();
  229. expect(
  230. await screen.findByText(`Send a notification to ${member.email}`)
  231. ).toBeInTheDocument();
  232. });
  233. });