index.spec.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen} from 'sentry-test/reactTestingLibrary';
  3. import ProjectsStore from 'sentry/stores/projectsStore';
  4. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  5. import MetricAlertDetails from 'sentry/views/alerts/rules/metric/details';
  6. jest.mock('sentry/utils/analytics/trackAdvancedAnalyticsEvent');
  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, router} = 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. route={{}}
  52. router={router}
  53. routes={router.routes}
  54. routeParams={router.params}
  55. location={router.location}
  56. params={{orgId: organization.slug, ruleId: rule.id}}
  57. />,
  58. {context: routerContext, organization}
  59. );
  60. expect(await screen.findAllByText(rule.name)).toHaveLength(2);
  61. expect(screen.getByText('Change alert status to Resolved')).toBeInTheDocument();
  62. expect(screen.getByText(incident.identifier)).toBeInTheDocument();
  63. // Related issues
  64. expect(screen.getByTestId('group')).toBeInTheDocument();
  65. expect(trackAdvancedAnalyticsEvent).toHaveBeenCalledWith(
  66. 'alert_rule_details.viewed',
  67. expect.objectContaining({
  68. rule_id: Number(rule.id),
  69. alert: '',
  70. })
  71. );
  72. });
  73. it('renders selected incident', async () => {
  74. const {routerContext, organization, router} = initializeOrg();
  75. const rule = TestStubs.MetricRule({projects: [project.slug]});
  76. const incident = TestStubs.Incident();
  77. MockApiClient.addMockResponse({
  78. url: `/organizations/org-slug/alert-rules/${rule.id}/`,
  79. body: rule,
  80. });
  81. const incidentMock = MockApiClient.addMockResponse({
  82. url: `/organizations/org-slug/incidents/${incident.id}/`,
  83. body: incident,
  84. });
  85. MockApiClient.addMockResponse({
  86. url: `/organizations/org-slug/incidents/`,
  87. body: [incident],
  88. });
  89. // Related issues to the selected incident
  90. const issuesRequest = MockApiClient.addMockResponse({
  91. 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',
  92. body: [TestStubs.Group()],
  93. });
  94. render(
  95. <MetricAlertDetails
  96. organization={organization}
  97. route={{}}
  98. router={router}
  99. routes={router.routes}
  100. routeParams={router.params}
  101. location={{...router.location, query: {alert: incident.id}}}
  102. params={{orgId: organization.slug, ruleId: rule.id}}
  103. />,
  104. {context: routerContext, organization}
  105. );
  106. expect(await screen.findAllByText(rule.name)).toHaveLength(2);
  107. // Related issues
  108. expect(screen.getByTestId('group')).toBeInTheDocument();
  109. expect(trackAdvancedAnalyticsEvent).toHaveBeenCalledWith(
  110. 'alert_rule_details.viewed',
  111. expect.objectContaining({
  112. rule_id: Number(rule.id),
  113. alert: '321',
  114. })
  115. );
  116. expect(incidentMock).toHaveBeenCalled();
  117. expect(issuesRequest).toHaveBeenCalled();
  118. });
  119. });