ownerInput.spec.tsx 2.7 KB

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