ruleDetails.spec.tsx 8.7 KB

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