duplicate.spec.tsx 4.5 KB

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