ownerInput.spec.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import selectEvent from 'react-select-event';
  2. import {Members} from 'sentry-fixture/members';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import MemberListStore from 'sentry/stores/memberListStore';
  6. import OwnerInput from 'sentry/views/settings/project/projectOwnership/ownerInput';
  7. describe('Project Ownership Input', function () {
  8. const {organization, project} = initializeOrg();
  9. let put: jest.Mock;
  10. beforeEach(function () {
  11. MockApiClient.addMockResponse({
  12. url: `/organizations/${organization.slug}/members/`,
  13. method: 'GET',
  14. body: Members(),
  15. });
  16. put = MockApiClient.addMockResponse({
  17. url: `/projects/${organization.slug}/${project.slug}/ownership/`,
  18. method: 'PUT',
  19. body: {raw: 'url:src @dummy@example.com'},
  20. });
  21. MemberListStore.init();
  22. MemberListStore.loadInitialData([
  23. TestStubs.User({id: '1', email: 'bob@example.com'}),
  24. ]);
  25. });
  26. it('renders', async function () {
  27. render(
  28. <OwnerInput
  29. page="issue_details"
  30. onCancel={() => {}}
  31. dateUpdated={null}
  32. organization={organization}
  33. initialText="url:src @dummy@example.com"
  34. project={project}
  35. />
  36. );
  37. const submitButton = screen.getByRole('button', {name: 'Save'});
  38. expect(put).not.toHaveBeenCalled();
  39. // if text is unchanged, submit button is disabled
  40. await userEvent.click(submitButton);
  41. expect(put).not.toHaveBeenCalled();
  42. const textarea = screen.getByRole('textbox', {name: 'Ownership Rules'});
  43. await userEvent.clear(textarea);
  44. await userEvent.type(textarea, 'new');
  45. await userEvent.click(submitButton);
  46. expect(put).toHaveBeenCalled();
  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. });