ownerInput.spec.jsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import selectEvent from 'react-select-event';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import MemberListStore from 'sentry/stores/memberListStore';
  4. import OwnerInput from 'sentry/views/settings/project/projectOwnership/ownerInput';
  5. describe('Project Ownership Input', function () {
  6. let org;
  7. let project;
  8. let put;
  9. beforeEach(function () {
  10. org = TestStubs.Organization();
  11. project = TestStubs.Project();
  12. MockApiClient.addMockResponse({
  13. url: '/organizations/org-slug/members/',
  14. method: 'GET',
  15. body: TestStubs.Members(),
  16. });
  17. put = MockApiClient.addMockResponse({
  18. url: `/projects/${org.slug}/${project.slug}/ownership/`,
  19. method: 'PUT',
  20. body: {raw: 'url:src @dummy@example.com'},
  21. });
  22. MemberListStore.init();
  23. MemberListStore.loadInitialData([
  24. TestStubs.User({id: '1', email: 'bob@example.com'}),
  25. ]);
  26. });
  27. it('renders', async function () {
  28. const {container} = render(
  29. <OwnerInput
  30. organization={org}
  31. initialText="url:src @dummy@example.com"
  32. project={project}
  33. />
  34. );
  35. const submitButton = screen.getByRole('button', {name: 'Save'});
  36. expect(put).not.toHaveBeenCalled();
  37. // if text is unchanged, submit button is disabled
  38. await userEvent.click(submitButton);
  39. expect(put).not.toHaveBeenCalled();
  40. const textarea = screen.getByRole('textbox', {name: 'Ownership Rules'});
  41. await userEvent.clear(textarea);
  42. await userEvent.type(textarea, 'new');
  43. await userEvent.click(submitButton);
  44. expect(put).toHaveBeenCalled();
  45. expect(container).toSnapshot();
  46. });
  47. it('updates on add preserving existing text', async function () {
  48. render(
  49. <OwnerInput
  50. organization={org}
  51. initialText="url:src @dummy@example.com"
  52. project={project}
  53. />
  54. );
  55. // Set a path, as path is selected bu default.
  56. await userEvent.type(screen.getByRole('textbox', {name: 'Rule pattern'}), 'file.js');
  57. // Select the user.
  58. await selectEvent.select(
  59. screen.getByRole('textbox', {name: 'Rule owner'}),
  60. 'Foo Bar'
  61. );
  62. // Add the new rule.
  63. await userEvent.click(screen.getByRole('button', {name: 'Add rule'}));
  64. expect(put).toHaveBeenCalledWith(
  65. '/projects/org-slug/project-slug/ownership/',
  66. expect.objectContaining({
  67. data: {
  68. raw: 'url:src @dummy@example.com' + '\n' + 'path:file.js bob@example.com',
  69. },
  70. })
  71. );
  72. });
  73. });