organizationSettingsForm.spec.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent} 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. 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. {...routerProps}
  87. initialData={OrganizationFixture()}
  88. onSave={onSave}
  89. />
  90. );
  91. const input = screen.getByRole('textbox', {name: 'Organization Slug'});
  92. await userEvent.clear(input);
  93. await userEvent.type(input, 'NEW SLUG');
  94. await userEvent.tab();
  95. expect(putMock).not.toHaveBeenCalled();
  96. await userEvent.click(screen.getByRole('button', {name: 'Save'}));
  97. expect(putMock).toHaveBeenCalledWith(
  98. '/organizations/org-slug/',
  99. expect.objectContaining({
  100. data: {
  101. slug: 'new-slug',
  102. },
  103. })
  104. );
  105. });
  106. it('can enable codecov', async function () {
  107. putMock = MockApiClient.addMockResponse({
  108. url: `/organizations/${organization.slug}/`,
  109. method: 'PUT',
  110. body: {...organization, codecovAccess: true},
  111. });
  112. render(
  113. <OrganizationSettingsForm
  114. {...routerProps}
  115. initialData={OrganizationFixture({codecovAccess: false})}
  116. onSave={onSave}
  117. />,
  118. {
  119. organization: {
  120. ...organization,
  121. features: ['codecov-integration'],
  122. },
  123. }
  124. );
  125. await userEvent.click(
  126. screen.getByRole('checkbox', {name: /Enable Code Coverage Insights/})
  127. );
  128. expect(putMock).toHaveBeenCalledWith(
  129. '/organizations/org-slug/',
  130. expect.objectContaining({
  131. data: {
  132. codecovAccess: true,
  133. },
  134. })
  135. );
  136. });
  137. });