duplicate.spec.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {Fragment} from 'react';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen} from 'sentry-test/reactTestingLibrary';
  4. import GlobalModal from 'sentry/components/globalModal';
  5. import MetricRulesDuplicate from 'sentry/views/alerts/rules/metric/duplicate';
  6. import {AlertRuleTriggerType} from 'sentry/views/alerts/rules/metric/types';
  7. describe('Incident Rules Duplicate', function () {
  8. beforeEach(function () {
  9. MockApiClient.clearMockResponses();
  10. MockApiClient.addMockResponse({
  11. url: '/organizations/org-slug/users/',
  12. body: [],
  13. });
  14. MockApiClient.addMockResponse({
  15. url: '/organizations/org-slug/tags/',
  16. body: [],
  17. });
  18. MockApiClient.addMockResponse({
  19. url: '/projects/org-slug/project-slug/environments/',
  20. body: [],
  21. });
  22. MockApiClient.addMockResponse({
  23. url: '/organizations/org-slug/events-stats/',
  24. body: null,
  25. });
  26. MockApiClient.addMockResponse({
  27. url: '/organizations/org-slug/events-meta/',
  28. body: {count: 5},
  29. });
  30. MockApiClient.addMockResponse({
  31. url: '/organizations/org-slug/alert-rules/available-actions/',
  32. body: [
  33. {
  34. allowedTargetTypes: ['user', 'team'],
  35. integrationName: null,
  36. type: 'email',
  37. integrationId: null,
  38. },
  39. {
  40. allowedTargetTypes: ['specific'],
  41. integrationName: null,
  42. type: 'slack',
  43. integrationId: 1,
  44. },
  45. ],
  46. });
  47. MockApiClient.addMockResponse({
  48. url: '/organizations/org-slug/members/',
  49. body: [TestStubs.Member()],
  50. });
  51. });
  52. it('renders new alert form with values copied over', function () {
  53. const rule = TestStubs.MetricRule();
  54. rule.triggers.push({
  55. label: AlertRuleTriggerType.WARNING,
  56. alertThreshold: 60,
  57. actions: [],
  58. });
  59. rule.resolveThreshold = 50;
  60. const {organization, project, routerProps} = initializeOrg({
  61. organization: {
  62. access: ['alerts:write'],
  63. },
  64. router: {
  65. params: {},
  66. location: {
  67. query: {
  68. createFromDuplicate: 'true',
  69. duplicateRuleId: `${rule.id}`,
  70. },
  71. },
  72. },
  73. project: rule.projects[0],
  74. projects: rule.projects,
  75. });
  76. const req = MockApiClient.addMockResponse({
  77. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  78. body: rule,
  79. });
  80. render(
  81. <Fragment>
  82. <GlobalModal />
  83. <MetricRulesDuplicate
  84. organization={organization}
  85. project={project}
  86. userTeamIds={[]}
  87. {...routerProps}
  88. />
  89. </Fragment>
  90. );
  91. // Duplicated alert has been called
  92. expect(req).toHaveBeenCalled();
  93. // Has correct values copied from the duplicated alert
  94. expect(screen.getByTestId('critical-threshold')).toHaveValue('70');
  95. expect(screen.getByTestId('warning-threshold')).toHaveValue('60');
  96. expect(screen.getByTestId('resolve-threshold')).toHaveValue('50');
  97. // Has the updated alert rule name
  98. expect(screen.getByTestId('alert-name')).toHaveValue(`${rule.name} copy`);
  99. });
  100. it('duplicates slack actions', function () {
  101. const rule = TestStubs.MetricRule();
  102. rule.triggers[0].actions.push({
  103. id: '13',
  104. alertRuleTriggerId: '12',
  105. type: 'slack',
  106. targetType: 'specific',
  107. targetIdentifier: '#feed-ecosystem',
  108. inputChannelId: 'ABC123',
  109. integrationId: 1,
  110. sentryAppId: null,
  111. desc: 'Send a Slack notification to #feed-ecosystem',
  112. });
  113. const {organization, project, routerProps} = initializeOrg({
  114. organization: {
  115. access: ['alerts:write'],
  116. },
  117. router: {
  118. params: {},
  119. location: {
  120. query: {
  121. createFromDuplicate: 'true',
  122. duplicateRuleId: `${rule.id}`,
  123. },
  124. },
  125. },
  126. project: rule.projects[0],
  127. projects: rule.projects,
  128. });
  129. const req = MockApiClient.addMockResponse({
  130. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  131. body: rule,
  132. });
  133. render(
  134. <MetricRulesDuplicate
  135. organization={organization}
  136. project={project}
  137. userTeamIds={[]}
  138. {...routerProps}
  139. />
  140. );
  141. // Duplicated alert has been called
  142. expect(req).toHaveBeenCalled();
  143. // Still has a selected slack action
  144. expect(screen.getByText('Slack')).toBeInTheDocument();
  145. expect(screen.getByPlaceholderText('optional: channel ID or user ID')).toHaveValue(
  146. 'ABC123'
  147. );
  148. expect(screen.getByText(/Enter a channel or user ID/)).toBeInTheDocument();
  149. });
  150. });