organizationSettingsForm.spec.jsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. MockApiClient.addMockResponse({
  17. url: `/organizations/${organization.slug}/integrations/?provider_key=github`,
  18. method: 'GET',
  19. body: {
  20. providers: [{canAdd: true}],
  21. },
  22. });
  23. onSave.mockReset();
  24. });
  25. it('can change a form field', async function () {
  26. putMock = MockApiClient.addMockResponse({
  27. url: `/organizations/${organization.slug}/`,
  28. method: 'PUT',
  29. body: organization,
  30. });
  31. render(
  32. <OrganizationSettingsForm
  33. location={TestStubs.location()}
  34. orgId={organization.slug}
  35. access={new Set(['org:write'])}
  36. initialData={TestStubs.Organization()}
  37. onSave={onSave}
  38. />
  39. );
  40. render(<Indicators />);
  41. const input = screen.getByRole('textbox', {name: 'Display Name'});
  42. const saveOnBlur = jest.spyOn(indicatorActions, 'saveOnBlurUndoMessage');
  43. await userEvent.clear(input);
  44. await userEvent.type(input, 'New Name');
  45. await userEvent.tab();
  46. expect(putMock).toHaveBeenCalledWith(
  47. `/organizations/${organization.slug}/`,
  48. expect.objectContaining({
  49. method: 'PUT',
  50. data: {
  51. name: 'New Name',
  52. },
  53. })
  54. );
  55. expect(saveOnBlur).toHaveBeenCalledWith(
  56. {
  57. new: 'New Name',
  58. old: 'Organization Name',
  59. },
  60. expect.anything(),
  61. 'name'
  62. );
  63. const model = saveOnBlur.mock.calls[0][1];
  64. // Test "undo" call undo directly
  65. expect(model.getValue('name')).toBe('New Name');
  66. model.undo();
  67. expect(model.getValue('name')).toBe('Organization Name');
  68. // `saveOnBlurUndoMessage` saves the new field, so reimplement this
  69. await model.saveField('name', 'Organization Name');
  70. // Initial data should be updated to original name
  71. expect(model.initialData.name).toBe('Organization Name');
  72. putMock.mockReset();
  73. // Blurring the name field again should NOT trigger a save
  74. await userEvent.click(input);
  75. await userEvent.tab();
  76. expect(putMock).not.toHaveBeenCalled();
  77. });
  78. it('can change slug', async function () {
  79. putMock = MockApiClient.addMockResponse({
  80. url: `/organizations/${organization.slug}/`,
  81. method: 'PUT',
  82. body: organization,
  83. });
  84. render(
  85. <OrganizationSettingsForm
  86. location={TestStubs.location()}
  87. orgId={organization.slug}
  88. access={new Set(['org:write'])}
  89. initialData={TestStubs.Organization()}
  90. onSave={onSave}
  91. />
  92. );
  93. const input = screen.getByRole('textbox', {name: 'Organization Slug'});
  94. await userEvent.clear(input);
  95. await userEvent.type(input, 'NEW SLUG');
  96. await userEvent.tab();
  97. expect(putMock).not.toHaveBeenCalled();
  98. await userEvent.click(screen.getByRole('button', {name: 'Save'}));
  99. expect(putMock).toHaveBeenCalledWith(
  100. '/organizations/org-slug/',
  101. expect.objectContaining({
  102. data: {
  103. slug: 'new-slug',
  104. },
  105. })
  106. );
  107. });
  108. it('can enable codecov', async function () {
  109. putMock = MockApiClient.addMockResponse({
  110. url: `/organizations/${organization.slug}/`,
  111. method: 'PUT',
  112. body: {...organization, codecovAccess: true},
  113. });
  114. render(
  115. <OrganizationSettingsForm
  116. location={TestStubs.location()}
  117. orgId={organization.slug}
  118. access={new Set(['org:write'])}
  119. initialData={TestStubs.Organization({codecovAccess: false})}
  120. onSave={onSave}
  121. />,
  122. {
  123. organization: {
  124. ...organization,
  125. features: ['codecov-integration'],
  126. },
  127. }
  128. );
  129. await userEvent.click(
  130. screen.getByRole('checkbox', {name: /Enable Code Coverage Insights/})
  131. );
  132. expect(putMock).toHaveBeenCalledWith(
  133. '/organizations/org-slug/',
  134. expect.objectContaining({
  135. data: {
  136. codecovAccess: true,
  137. },
  138. })
  139. );
  140. });
  141. it('enable PR bot is disabled without GitHub integration', function () {
  142. render(
  143. <OrganizationSettingsForm
  144. location={TestStubs.location()}
  145. orgId={organization.slug}
  146. access={new Set(['org:write'])}
  147. initialData={TestStubs.Organization()}
  148. onSave={onSave}
  149. />,
  150. {
  151. organization: {
  152. ...organization,
  153. features: ['pr-comment-bot'],
  154. },
  155. }
  156. );
  157. expect(
  158. screen.getByRole('checkbox', {name: /Enable Pull Request Bot/})
  159. ).toBeDisabled();
  160. });
  161. });