duplicate.spec.tsx 4.5 KB

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