index.spec.tsx 5.5 KB

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