organizationSettingsForm.spec.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import * as indicatorActions from 'sentry/actionCreators/indicator';
  5. import Indicators from 'sentry/components/indicators';
  6. import OrganizationSettingsForm from 'sentry/views/settings/organizationGeneralSettings/organizationSettingsForm';
  7. jest.mock('sentry/actionCreators/indicator');
  8. describe('OrganizationSettingsForm', function () {
  9. const {organization, routerProps} = initializeOrg();
  10. let putMock: jest.Mock;
  11. const onSave = jest.fn();
  12. beforeEach(function () {
  13. MockApiClient.clearMockResponses();
  14. MockApiClient.addMockResponse({
  15. url: `/organizations/${organization.slug}/auth-provider/`,
  16. method: 'GET',
  17. });
  18. MockApiClient.addMockResponse({
  19. url: `/organizations/${organization.slug}/integrations/?provider_key=github`,
  20. method: 'GET',
  21. body: {
  22. providers: [{canAdd: true}],
  23. },
  24. });
  25. onSave.mockReset();
  26. });
  27. it('can change a form field', async function () {
  28. putMock = MockApiClient.addMockResponse({
  29. url: `/organizations/${organization.slug}/`,
  30. method: 'PUT',
  31. body: organization,
  32. });
  33. render(
  34. <OrganizationSettingsForm
  35. {...routerProps}
  36. initialData={OrganizationFixture()}
  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. act(() => {
  67. model.undo();
  68. });
  69. expect(model.getValue('name')).toBe('Organization Name');
  70. // `saveOnBlurUndoMessage` saves the new field, so reimplement this
  71. act(() => {
  72. model.saveField('name', 'Organization Name');
  73. });
  74. // Initial data should be updated to original name
  75. await waitFor(() => expect(model.initialData.name).toBe('Organization Name'));
  76. putMock.mockReset();
  77. // Blurring the name field again should NOT trigger a save
  78. await userEvent.click(input);
  79. await userEvent.tab();
  80. expect(putMock).not.toHaveBeenCalled();
  81. });
  82. it('can change slug', async function () {
  83. putMock = MockApiClient.addMockResponse({
  84. url: `/organizations/${organization.slug}/`,
  85. method: 'PUT',
  86. body: organization,
  87. });
  88. render(
  89. <OrganizationSettingsForm
  90. {...routerProps}
  91. initialData={OrganizationFixture()}
  92. onSave={onSave}
  93. />
  94. );
  95. const input = screen.getByRole('textbox', {name: 'Organization Slug'});
  96. await userEvent.clear(input);
  97. await userEvent.type(input, 'NEW SLUG');
  98. await userEvent.tab();
  99. expect(putMock).not.toHaveBeenCalled();
  100. await userEvent.click(screen.getByRole('button', {name: 'Save'}));
  101. expect(putMock).toHaveBeenCalledWith(
  102. '/organizations/org-slug/',
  103. expect.objectContaining({
  104. data: {
  105. slug: 'new-slug',
  106. },
  107. })
  108. );
  109. });
  110. it('can enable codecov', async function () {
  111. putMock = MockApiClient.addMockResponse({
  112. url: `/organizations/${organization.slug}/`,
  113. method: 'PUT',
  114. body: {...organization, codecovAccess: true},
  115. });
  116. render(
  117. <OrganizationSettingsForm
  118. {...routerProps}
  119. initialData={OrganizationFixture({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. });