organizationSettingsForm.spec.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import * as indicatorActions 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. const saveOnBlur = jest.spyOn(indicatorActions, 'saveOnBlurUndoMessage');
  36. await userEvent.clear(input);
  37. await userEvent.type(input, 'New Name');
  38. await userEvent.tab();
  39. expect(putMock).toHaveBeenCalledWith(
  40. `/organizations/${organization.slug}/`,
  41. expect.objectContaining({
  42. method: 'PUT',
  43. data: {
  44. name: 'New Name',
  45. },
  46. })
  47. );
  48. expect(saveOnBlur).toHaveBeenCalledWith(
  49. {
  50. new: 'New Name',
  51. old: 'Organization Name',
  52. },
  53. expect.anything(),
  54. 'name'
  55. );
  56. const model = saveOnBlur.mock.calls[0][1];
  57. // Test "undo" call undo directly
  58. expect(model.getValue('name')).toBe('New Name');
  59. model.undo();
  60. expect(model.getValue('name')).toBe('Organization Name');
  61. // `saveOnBlurUndoMessage` saves the new field, so reimplement this
  62. await model.saveField('name', 'Organization Name');
  63. // Initial data should be updated to original name
  64. expect(model.initialData.name).toBe('Organization Name');
  65. putMock.mockReset();
  66. // Blurring the name field again should NOT trigger a save
  67. await userEvent.click(input);
  68. await userEvent.tab();
  69. expect(putMock).not.toHaveBeenCalled();
  70. });
  71. it('can change slug', async function () {
  72. putMock = MockApiClient.addMockResponse({
  73. url: `/organizations/${organization.slug}/`,
  74. method: 'PUT',
  75. body: organization,
  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. await userEvent.clear(input);
  88. await userEvent.type(input, 'NEW SLUG');
  89. await userEvent.tab();
  90. expect(putMock).not.toHaveBeenCalled();
  91. await 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. it('can enable codecov', async function () {
  102. putMock = MockApiClient.addMockResponse({
  103. url: `/organizations/${organization.slug}/`,
  104. method: 'PUT',
  105. body: {...organization, codecovAccess: true},
  106. });
  107. render(
  108. <OrganizationSettingsForm
  109. location={TestStubs.location()}
  110. orgId={organization.slug}
  111. access={new Set(['org:write'])}
  112. initialData={TestStubs.Organization({codecovAccess: false})}
  113. onSave={onSave}
  114. />,
  115. {
  116. organization: {
  117. ...organization,
  118. features: ['codecov-integration'],
  119. },
  120. }
  121. );
  122. await userEvent.click(
  123. screen.getByRole('checkbox', {name: /Enable Code Coverage Insights/})
  124. );
  125. expect(putMock).toHaveBeenCalledWith(
  126. '/organizations/org-slug/',
  127. expect.objectContaining({
  128. data: {
  129. codecovAccess: true,
  130. },
  131. })
  132. );
  133. });
  134. });