organizationSettingsForm.spec.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {saveOnBlurUndoMessage} from 'sentry/actionCreators/indicator';
  3. import Indicators from 'sentry/components/indicators';
  4. import OrganizationSettingsForm from 'sentry/views/settings/organizationGeneralSettings/organizationSettingsForm';
  5. jest.mock('sentry/actionCreators/indicator');
  6. describe('OrganizationSettingsForm', function () {
  7. const organization = TestStubs.Organization();
  8. let putMock;
  9. const onSave = jest.fn();
  10. beforeEach(function () {
  11. MockApiClient.clearMockResponses();
  12. MockApiClient.addMockResponse({
  13. url: `/organizations/${organization.slug}/auth-provider/`,
  14. method: 'GET',
  15. });
  16. onSave.mockReset();
  17. });
  18. it('can change a form field', async function () {
  19. putMock = MockApiClient.addMockResponse({
  20. url: `/organizations/${organization.slug}/`,
  21. method: 'PUT',
  22. body: organization,
  23. });
  24. render(
  25. <OrganizationSettingsForm
  26. location={TestStubs.location()}
  27. orgId={organization.slug}
  28. access={new Set(['org:write'])}
  29. initialData={TestStubs.Organization()}
  30. onSave={onSave}
  31. />
  32. );
  33. render(<Indicators />);
  34. const input = screen.getByRole('textbox', {name: 'Display Name'});
  35. userEvent.clear(input);
  36. userEvent.type(input, 'New Name');
  37. userEvent.tab();
  38. expect(putMock).toHaveBeenCalledWith(
  39. `/organizations/${organization.slug}/`,
  40. expect.objectContaining({
  41. method: 'PUT',
  42. data: {
  43. name: 'New Name',
  44. },
  45. })
  46. );
  47. await new Promise(resolve => {
  48. saveOnBlurUndoMessage.mockImplementationOnce(async function (
  49. change,
  50. model,
  51. fieldName
  52. ) {
  53. expect(fieldName).toBe('name');
  54. expect(change.old).toBe('Organization Name');
  55. expect(change.new).toBe('New Name');
  56. // Test "undo" call undo directly
  57. expect(model.getValue('name')).toBe('New Name');
  58. model.undo();
  59. expect(model.getValue('name')).toBe('Organization Name');
  60. // `saveOnBlurUndoMessage` saves the new field, so reimplement this
  61. await model.saveField('name', 'Organization Name');
  62. // Initial data should be updated to original name
  63. expect(model.initialData.name).toBe('Organization Name');
  64. putMock.mockReset();
  65. // Blurring the name field again should NOT trigger a save
  66. userEvent.click(input);
  67. userEvent.tab();
  68. expect(putMock).not.toHaveBeenCalled();
  69. resolve();
  70. });
  71. });
  72. });
  73. it('can change slug', function () {
  74. putMock = MockApiClient.addMockResponse({
  75. url: `/organizations/${organization.slug}/`,
  76. method: 'PUT',
  77. body: organization,
  78. });
  79. render(
  80. <OrganizationSettingsForm
  81. location={TestStubs.location()}
  82. orgId={organization.slug}
  83. access={new Set(['org:write'])}
  84. initialData={TestStubs.Organization()}
  85. onSave={onSave}
  86. />
  87. );
  88. const input = screen.getByRole('textbox', {name: 'Organization Slug'});
  89. userEvent.clear(input);
  90. userEvent.type(input, 'NEW SLUG');
  91. userEvent.tab();
  92. expect(putMock).not.toHaveBeenCalled();
  93. userEvent.click(screen.getByRole('button', {name: 'Save'}));
  94. expect(putMock).toHaveBeenCalledWith(
  95. '/organizations/org-slug/',
  96. expect.objectContaining({
  97. data: {
  98. slug: 'new-slug',
  99. },
  100. })
  101. );
  102. });
  103. });