ownerInput.spec.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import selectEvent from 'react-select-event';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import MemberListStore from 'sentry/stores/memberListStore';
  5. import OwnerInput from 'sentry/views/settings/project/projectOwnership/ownerInput';
  6. describe('Project Ownership Input', function () {
  7. const {organization, project} = initializeOrg();
  8. let put: jest.Mock;
  9. beforeEach(function () {
  10. MockApiClient.addMockResponse({
  11. url: `/organizations/${organization.slug}/members/`,
  12. method: 'GET',
  13. body: TestStubs.Members(),
  14. });
  15. put = MockApiClient.addMockResponse({
  16. url: `/projects/${organization.slug}/${project.slug}/ownership/`,
  17. method: 'PUT',
  18. body: {raw: 'url:src @dummy@example.com'},
  19. });
  20. MemberListStore.init();
  21. MemberListStore.loadInitialData([
  22. TestStubs.User({id: '1', email: 'bob@example.com'}),
  23. ]);
  24. });
  25. it('renders', async function () {
  26. const {container} = 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. expect(container).toSnapshot();
  47. });
  48. it('updates on add preserving existing text', async function () {
  49. render(
  50. <OwnerInput
  51. page="issue_details"
  52. onCancel={() => {}}
  53. dateUpdated={null}
  54. organization={organization}
  55. initialText="url:src @dummy@example.com"
  56. project={project}
  57. />
  58. );
  59. // Set a path, as path is selected bu default.
  60. await userEvent.type(screen.getByRole('textbox', {name: 'Rule pattern'}), 'file.js');
  61. // Select the user.
  62. await selectEvent.select(
  63. screen.getByRole('textbox', {name: 'Rule owner'}),
  64. 'Foo Bar'
  65. );
  66. // Add the new rule.
  67. await userEvent.click(screen.getByRole('button', {name: 'Add rule'}));
  68. expect(put).toHaveBeenCalledWith(
  69. `/projects/${organization.slug}/${project.slug}/ownership/`,
  70. expect.objectContaining({
  71. data: {
  72. raw: 'url:src @dummy@example.com' + '\n' + 'path:file.js bob@example.com',
  73. },
  74. })
  75. );
  76. });
  77. });