organizationSettingsForm.spec.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {saveOnBlurUndoMessage} from 'sentry/actionCreators/indicator';
  3. import OrganizationSettingsForm from 'sentry/views/settings/organizationGeneralSettings/organizationSettingsForm';
  4. jest.mock('sentry/actionCreators/indicator');
  5. describe('OrganizationSettingsForm', function () {
  6. const organization = TestStubs.Organization();
  7. let putMock;
  8. const onSave = jest.fn();
  9. beforeEach(function () {
  10. MockApiClient.clearMockResponses();
  11. MockApiClient.addMockResponse({
  12. url: `/organizations/${organization.slug}/auth-provider/`,
  13. method: 'GET',
  14. });
  15. onSave.mockReset();
  16. });
  17. it('can change a form field', async function () {
  18. putMock = MockApiClient.addMockResponse({
  19. url: `/organizations/${organization.slug}/`,
  20. method: 'PUT',
  21. data: {
  22. name: 'New Name',
  23. },
  24. });
  25. const wrapper = mountWithTheme(
  26. <OrganizationSettingsForm
  27. location={TestStubs.location()}
  28. orgId={organization.slug}
  29. access={new Set('org:admin')}
  30. initialData={TestStubs.Organization()}
  31. onSave={onSave}
  32. />
  33. );
  34. const input = wrapper.find('input[name="name"]');
  35. expect(input).toHaveLength(1);
  36. input.simulate('change', {target: {value: 'New Name'}});
  37. input.simulate('blur');
  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. input.simulate('blur');
  67. expect(putMock).not.toHaveBeenCalled();
  68. resolve();
  69. });
  70. });
  71. });
  72. it('can change slug', function () {
  73. putMock = MockApiClient.addMockResponse({
  74. url: `/organizations/${organization.slug}/`,
  75. method: 'PUT',
  76. });
  77. const wrapper = mountWithTheme(
  78. <OrganizationSettingsForm
  79. location={TestStubs.location()}
  80. orgId={organization.slug}
  81. access={new Set('org:admin')}
  82. initialData={TestStubs.Organization()}
  83. onSave={onSave}
  84. />
  85. );
  86. wrapper
  87. .find('input[name="slug"]')
  88. .simulate('change', {target: {value: 'NEW SLUG'}})
  89. .simulate('blur');
  90. expect(putMock).not.toHaveBeenCalled();
  91. wrapper.find('button[aria-label="Save"]').simulate('click');
  92. expect(putMock).toHaveBeenCalledWith(
  93. '/organizations/org-slug/',
  94. expect.objectContaining({
  95. data: {
  96. slug: 'new-slug',
  97. },
  98. })
  99. );
  100. });
  101. });