organizationSettingsForm.spec.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. });
  23. render(
  24. <OrganizationSettingsForm
  25. location={TestStubs.location()}
  26. orgId={organization.slug}
  27. access={new Set(['org:write'])}
  28. initialData={TestStubs.Organization()}
  29. onSave={onSave}
  30. />
  31. );
  32. render(<Indicators />);
  33. const input = screen.getByRole('textbox', {name: 'Display Name'});
  34. userEvent.clear(input);
  35. userEvent.type(input, 'New Name');
  36. userEvent.tab();
  37. expect(putMock).toHaveBeenCalledWith(
  38. `/organizations/${organization.slug}/`,
  39. expect.objectContaining({
  40. method: 'PUT',
  41. data: {
  42. name: 'New Name',
  43. },
  44. })
  45. );
  46. await new Promise(resolve => {
  47. saveOnBlurUndoMessage.mockImplementationOnce(async function (
  48. change,
  49. model,
  50. fieldName
  51. ) {
  52. expect(fieldName).toBe('name');
  53. expect(change.old).toBe('Organization Name');
  54. expect(change.new).toBe('New Name');
  55. // Test "undo" call undo directly
  56. expect(model.getValue('name')).toBe('New Name');
  57. model.undo();
  58. expect(model.getValue('name')).toBe('Organization Name');
  59. // `saveOnBlurUndoMessage` saves the new field, so reimplement this
  60. await model.saveField('name', 'Organization Name');
  61. // Initial data should be updated to original name
  62. expect(model.initialData.name).toBe('Organization Name');
  63. putMock.mockReset();
  64. // Blurring the name field again should NOT trigger a save
  65. userEvent.click(input);
  66. userEvent.tab();
  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. render(
  78. <OrganizationSettingsForm
  79. location={TestStubs.location()}
  80. orgId={organization.slug}
  81. access={new Set(['org:write'])}
  82. initialData={TestStubs.Organization()}
  83. onSave={onSave}
  84. />
  85. );
  86. const input = screen.getByRole('textbox', {name: 'Organization Slug'});
  87. userEvent.clear(input);
  88. userEvent.type(input, 'NEW SLUG');
  89. userEvent.tab();
  90. expect(putMock).not.toHaveBeenCalled();
  91. userEvent.click(screen.getByRole('button', {name: 'Save'}));
  92. expect(putMock).toHaveBeenCalledWith(
  93. '/organizations/org-slug/',
  94. expect.objectContaining({
  95. data: {
  96. slug: 'new-slug',
  97. },
  98. })
  99. );
  100. });
  101. });