duplicate.spec.tsx 4.4 KB

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