ownerInput.spec.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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', function () {
  28. const {container} = render(
  29. <OwnerInput
  30. params={{orgId: org.slug, projectId: project.slug}}
  31. organization={org}
  32. initialText="url:src @dummy@example.com"
  33. project={project}
  34. />
  35. );
  36. const submitButton = screen.getByRole('button', {name: 'Save Changes'});
  37. expect(put).not.toHaveBeenCalled();
  38. // if text is unchanged, submit button is disabled
  39. userEvent.click(submitButton);
  40. expect(put).not.toHaveBeenCalled();
  41. const textarea = screen.getByRole('textbox', {name: 'Ownership Rules'});
  42. userEvent.clear(textarea);
  43. userEvent.type(textarea, 'new');
  44. userEvent.click(submitButton);
  45. expect(put).toHaveBeenCalled();
  46. expect(container).toSnapshot();
  47. });
  48. it('updates on add preserving existing text', async function () {
  49. render(
  50. <OwnerInput
  51. params={{orgId: org.slug, projectId: project.slug}}
  52. organization={org}
  53. initialText="url:src @dummy@example.com"
  54. project={project}
  55. />
  56. );
  57. // Set a path, as path is selected bu default.
  58. userEvent.type(screen.getByRole('textbox', {name: 'Rule pattern'}), 'file.js');
  59. // Select the user.
  60. await selectEvent.select(
  61. screen.getByRole('textbox', {name: 'Rule owner'}),
  62. 'Foo Bar'
  63. );
  64. // Add the new rule.
  65. userEvent.click(screen.getByRole('button', {name: 'Add rule'}));
  66. expect(put).toHaveBeenCalledWith(
  67. '/projects/org-slug/project-slug/ownership/',
  68. expect.objectContaining({
  69. data: {
  70. raw: 'url:src @dummy@example.com' + '\n' + 'path:file.js bob@example.com',
  71. },
  72. })
  73. );
  74. });
  75. });