duplicate.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. });
  40. MockApiClient.addMockResponse({
  41. url: '/organizations/org-slug/members/',
  42. body: [TestStubs.Member()],
  43. });
  44. });
  45. it('renders new alert form with values copied over', function () {
  46. const rule = TestStubs.MetricRule();
  47. rule.triggers.push({
  48. label: AlertRuleTriggerType.WARNING,
  49. alertThreshold: 60,
  50. actions: [],
  51. });
  52. rule.resolveThreshold = 50;
  53. const {organization, project, router} = initializeOrg({
  54. organization: {
  55. access: ['alerts:write'],
  56. },
  57. router: {
  58. // we need this to be set to make sure org in context is same as
  59. // current org in URL
  60. params: {orgId: 'org-slug'},
  61. location: {
  62. query: {
  63. createFromDuplicate: true,
  64. duplicateRuleId: `${rule.id}`,
  65. },
  66. },
  67. },
  68. project: rule.projects[0],
  69. projects: rule.projects,
  70. });
  71. const req = MockApiClient.addMockResponse({
  72. url: `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
  73. body: rule,
  74. });
  75. render(
  76. <Fragment>
  77. <GlobalModal />
  78. <MetricRulesDuplicate
  79. params={{orgId: organization.slug}}
  80. route={{}}
  81. routeParams={router.params}
  82. router={router}
  83. routes={router.routes}
  84. location={router.location}
  85. organization={organization}
  86. project={project}
  87. userTeamIds={[]}
  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. });