edit.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {MemberFixture} from 'sentry-fixture/member';
  2. import {TeamFixture} from 'sentry-fixture/team';
  3. import {UptimeRuleFixture} from 'sentry-fixture/uptimeRule';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {
  6. render,
  7. renderGlobalModal,
  8. screen,
  9. userEvent,
  10. within,
  11. } from 'sentry-test/reactTestingLibrary';
  12. import selectEvent from 'sentry-test/selectEvent';
  13. import OrganizationStore from 'sentry/stores/organizationStore';
  14. import {UptimeRulesEdit} from './edit';
  15. describe('uptime/edit', function () {
  16. beforeEach(function () {
  17. MockApiClient.addMockResponse({
  18. url: '/organizations/org-slug/members/',
  19. body: [MemberFixture()],
  20. });
  21. MockApiClient.addMockResponse({
  22. url: '/organizations/org-slug/teams/',
  23. body: [TeamFixture()],
  24. });
  25. });
  26. it('displays the edit form', async function () {
  27. const {organization, project, routerProps} = initializeOrg();
  28. OrganizationStore.onUpdate(organization);
  29. const uptimeRule = UptimeRuleFixture();
  30. const handleChangeTitle = jest.fn();
  31. MockApiClient.addMockResponse({
  32. url: `/projects/${organization.slug}/${project.slug}/uptime/${uptimeRule.id}/`,
  33. method: 'GET',
  34. body: uptimeRule,
  35. });
  36. render(
  37. <UptimeRulesEdit
  38. {...routerProps}
  39. onChangeTitle={handleChangeTitle}
  40. userTeamIds={[]}
  41. organization={organization}
  42. project={project}
  43. params={{projectId: project.slug, ruleId: uptimeRule.id}}
  44. />,
  45. {organization}
  46. );
  47. await screen.findByText('Set a URL to monitor');
  48. expect(screen.getByRole('textbox', {name: 'Project'})).toBeDisabled();
  49. expect(screen.getByRole('textbox', {name: 'Environment'})).toBeDisabled();
  50. const url = screen.getByRole('textbox', {name: 'URL'});
  51. expect(url).toBeDisabled();
  52. expect(url).toHaveValue(uptimeRule.url);
  53. const name = screen.getByRole('textbox', {name: 'Uptime rule name'});
  54. expect(name).toBeEnabled();
  55. expect(name).toHaveValue(uptimeRule.name);
  56. await selectEvent.openMenu(screen.getByRole('textbox', {name: 'Owner'}));
  57. expect(screen.getByRole('menuitemradio', {name: 'Foo Bar'})).toBeChecked();
  58. });
  59. it('can delete rule', async function () {
  60. const {organization, project, routerProps} = initializeOrg();
  61. OrganizationStore.onUpdate(organization);
  62. const uptimeRule = UptimeRuleFixture();
  63. const handleChangeTitle = jest.fn();
  64. MockApiClient.addMockResponse({
  65. url: `/projects/${organization.slug}/${project.slug}/uptime/${uptimeRule.id}/`,
  66. method: 'GET',
  67. body: uptimeRule,
  68. });
  69. render(
  70. <UptimeRulesEdit
  71. {...routerProps}
  72. onChangeTitle={handleChangeTitle}
  73. userTeamIds={[]}
  74. organization={organization}
  75. project={project}
  76. params={{projectId: project.slug, ruleId: uptimeRule.id}}
  77. />,
  78. {organization}
  79. );
  80. await screen.findByText('Set a URL to monitor');
  81. const deleteRule = MockApiClient.addMockResponse({
  82. url: `/projects/${organization.slug}/${project.slug}/uptime/${uptimeRule.id}/`,
  83. method: 'DELETE',
  84. });
  85. renderGlobalModal();
  86. await userEvent.click(screen.getByRole('button', {name: 'Delete Rule'}));
  87. const modal = await screen.findByRole('dialog');
  88. await userEvent.click(within(modal).getByRole('button', {name: 'Delete Rule'}));
  89. expect(deleteRule).toHaveBeenCalled();
  90. });
  91. it('can update the name and owner', async function () {
  92. const {organization, project, routerProps} = initializeOrg();
  93. OrganizationStore.onUpdate(organization);
  94. const uptimeRule = UptimeRuleFixture({owner: undefined});
  95. const handleChangeTitle = jest.fn();
  96. MockApiClient.addMockResponse({
  97. url: `/projects/${organization.slug}/${project.slug}/uptime/${uptimeRule.id}/`,
  98. method: 'GET',
  99. body: uptimeRule,
  100. });
  101. render(
  102. <UptimeRulesEdit
  103. {...routerProps}
  104. onChangeTitle={handleChangeTitle}
  105. userTeamIds={[]}
  106. organization={organization}
  107. project={project}
  108. params={{projectId: project.slug, ruleId: uptimeRule.id}}
  109. />,
  110. {organization}
  111. );
  112. await screen.findByText('Set a URL to monitor');
  113. const name = screen.getByRole('textbox', {name: 'Uptime rule name'});
  114. await userEvent.clear(name);
  115. await userEvent.type(name, 'Updated name');
  116. await selectEvent.select(screen.getByRole('textbox', {name: 'Owner'}), 'Foo Bar');
  117. const updateMock = MockApiClient.addMockResponse({
  118. url: `/projects/${organization.slug}/${project.slug}/uptime/${uptimeRule.id}/`,
  119. method: 'PUT',
  120. });
  121. await userEvent.click(screen.getByRole('button', {name: 'Save Changes'}));
  122. expect(updateMock).toHaveBeenCalledWith(
  123. expect.anything(),
  124. expect.objectContaining({
  125. data: expect.objectContaining({name: 'Updated name', owner: 'user:1'}),
  126. })
  127. );
  128. });
  129. });