index.spec.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import {EventsStats} from 'sentry-fixture/events';
  2. import {Incident} from 'sentry-fixture/incident';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import {trackAnalytics} from 'sentry/utils/analytics';
  7. import MetricAlertDetails from 'sentry/views/alerts/rules/metric/details';
  8. jest.mock('sentry/utils/analytics');
  9. describe('MetricAlertDetails', () => {
  10. const project = TestStubs.Project({slug: 'earth', platform: 'javascript'});
  11. beforeEach(() => {
  12. act(() => ProjectsStore.loadInitialData([project]));
  13. MockApiClient.addMockResponse({
  14. url: '/organizations/org-slug/projects/',
  15. body: [project],
  16. });
  17. MockApiClient.addMockResponse({
  18. url: '/organizations/org-slug/users/',
  19. body: [],
  20. });
  21. MockApiClient.addMockResponse({
  22. url: '/organizations/org-slug/events-stats/',
  23. body: EventsStats(),
  24. });
  25. MockApiClient.addMockResponse({
  26. url: '/organizations/org-slug/issues/?end=2017-10-17T02%3A41%3A20&groupStatsPeriod=auto&limit=5&project=2&query=event.type%3Aerror&sort=freq&start=2017-10-10T02%3A41%3A20',
  27. body: [TestStubs.Group()],
  28. });
  29. });
  30. afterEach(() => {
  31. act(() => ProjectsStore.reset());
  32. jest.resetAllMocks();
  33. MockApiClient.clearMockResponses();
  34. });
  35. it('renders', async () => {
  36. const {routerContext, organization, routerProps} = initializeOrg();
  37. const incident = Incident();
  38. const rule = TestStubs.MetricRule({
  39. projects: [project.slug],
  40. latestIncident: incident,
  41. });
  42. MockApiClient.addMockResponse({
  43. url: `/organizations/org-slug/alert-rules/${rule.id}/`,
  44. body: rule,
  45. });
  46. MockApiClient.addMockResponse({
  47. url: `/organizations/org-slug/incidents/`,
  48. body: [incident],
  49. });
  50. render(
  51. <MetricAlertDetails
  52. organization={organization}
  53. {...routerProps}
  54. params={{ruleId: rule.id}}
  55. />,
  56. {context: routerContext, organization}
  57. );
  58. expect(await screen.findAllByText(rule.name)).toHaveLength(2);
  59. expect(screen.getByText('Change alert status to Resolved')).toBeInTheDocument();
  60. expect(screen.getByText(`#${incident.identifier}`)).toBeInTheDocument();
  61. // Related issues
  62. expect(screen.getByTestId('group')).toBeInTheDocument();
  63. expect(trackAnalytics).toHaveBeenCalledWith(
  64. 'alert_rule_details.viewed',
  65. expect.objectContaining({
  66. rule_id: Number(rule.id),
  67. alert: '',
  68. })
  69. );
  70. });
  71. it('renders selected incident', async () => {
  72. const {routerContext, organization, router, routerProps} = initializeOrg();
  73. const rule = TestStubs.MetricRule({projects: [project.slug]});
  74. const incident = Incident();
  75. MockApiClient.addMockResponse({
  76. url: `/organizations/org-slug/alert-rules/${rule.id}/`,
  77. body: rule,
  78. });
  79. const incidentMock = MockApiClient.addMockResponse({
  80. url: `/organizations/org-slug/incidents/${incident.id}/`,
  81. body: incident,
  82. });
  83. MockApiClient.addMockResponse({
  84. url: `/organizations/org-slug/incidents/`,
  85. body: [incident],
  86. });
  87. // Related issues to the selected incident
  88. const issuesRequest = MockApiClient.addMockResponse({
  89. url: '/organizations/org-slug/issues/?end=2016-04-26T19%3A44%3A05&groupStatsPeriod=auto&limit=5&project=2&query=event.type%3Aerror&sort=freq&start=2016-03-29T19%3A44%3A05',
  90. body: [TestStubs.Group()],
  91. });
  92. render(
  93. <MetricAlertDetails
  94. organization={organization}
  95. {...routerProps}
  96. location={{...router.location, query: {alert: incident.id}}}
  97. params={{ruleId: rule.id}}
  98. />,
  99. {context: routerContext, organization}
  100. );
  101. expect(await screen.findAllByText(rule.name)).toHaveLength(2);
  102. // Related issues
  103. expect(screen.getByTestId('group')).toBeInTheDocument();
  104. expect(trackAnalytics).toHaveBeenCalledWith(
  105. 'alert_rule_details.viewed',
  106. expect.objectContaining({
  107. rule_id: Number(rule.id),
  108. alert: '321',
  109. })
  110. );
  111. expect(incidentMock).toHaveBeenCalled();
  112. expect(issuesRequest).toHaveBeenCalled();
  113. });
  114. it('renders mute button for metric alert', async () => {
  115. const {routerContext, organization, routerProps} = initializeOrg();
  116. const incident = Incident();
  117. const rule = TestStubs.MetricRule({
  118. projects: [project.slug],
  119. latestIncident: incident,
  120. });
  121. MockApiClient.addMockResponse({
  122. url: `/organizations/org-slug/alert-rules/${rule.id}/`,
  123. body: rule,
  124. });
  125. MockApiClient.addMockResponse({
  126. url: `/organizations/org-slug/incidents/`,
  127. body: [incident],
  128. });
  129. const postRequest = MockApiClient.addMockResponse({
  130. url: `/projects/${organization.slug}/${project.slug}/alert-rules/${rule.id}/snooze/`,
  131. method: 'POST',
  132. });
  133. const deleteRequest = MockApiClient.addMockResponse({
  134. url: `/projects/${organization.slug}/${project.slug}/alert-rules/${rule.id}/snooze/`,
  135. method: 'DELETE',
  136. });
  137. render(
  138. <MetricAlertDetails
  139. {...routerProps}
  140. organization={organization}
  141. params={{ruleId: rule.id}}
  142. />,
  143. {context: routerContext, organization}
  144. );
  145. expect(await screen.findByText('Mute for me')).toBeInTheDocument();
  146. await userEvent.click(screen.getByRole('button', {name: 'Mute for me'}));
  147. expect(postRequest).toHaveBeenCalledTimes(1);
  148. expect(await screen.findByText('Unmute')).toBeInTheDocument();
  149. await userEvent.click(screen.getByRole('button', {name: 'Unmute'}));
  150. expect(deleteRequest).toHaveBeenCalledTimes(1);
  151. });
  152. });