organizationSettingsForm.spec.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {Organization} 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. access={new Set(['org:write'])}
  37. initialData={Organization()}
  38. onSave={onSave}
  39. />
  40. );
  41. render(<Indicators />);
  42. const input = screen.getByRole('textbox', {name: 'Display Name'});
  43. const saveOnBlur = jest.spyOn(indicatorActions, 'saveOnBlurUndoMessage');
  44. await userEvent.clear(input);
  45. await userEvent.type(input, 'New Name');
  46. await userEvent.tab();
  47. expect(putMock).toHaveBeenCalledWith(
  48. `/organizations/${organization.slug}/`,
  49. expect.objectContaining({
  50. method: 'PUT',
  51. data: {
  52. name: 'New Name',
  53. },
  54. })
  55. );
  56. expect(saveOnBlur).toHaveBeenCalledWith(
  57. {
  58. new: 'New Name',
  59. old: 'Organization Name',
  60. },
  61. expect.anything(),
  62. 'name'
  63. );
  64. const model = saveOnBlur.mock.calls[0][1];
  65. // Test "undo" call undo directly
  66. expect(model.getValue('name')).toBe('New Name');
  67. model.undo();
  68. expect(model.getValue('name')).toBe('Organization Name');
  69. // `saveOnBlurUndoMessage` saves the new field, so reimplement this
  70. await model.saveField('name', 'Organization Name');
  71. // Initial data should be updated to original name
  72. expect(model.initialData.name).toBe('Organization Name');
  73. putMock.mockReset();
  74. // Blurring the name field again should NOT trigger a save
  75. await userEvent.click(input);
  76. await userEvent.tab();
  77. expect(putMock).not.toHaveBeenCalled();
  78. });
  79. it('can change slug', async function () {
  80. putMock = MockApiClient.addMockResponse({
  81. url: `/organizations/${organization.slug}/`,
  82. method: 'PUT',
  83. body: organization,
  84. });
  85. render(
  86. <OrganizationSettingsForm
  87. {...routerProps}
  88. access={new Set(['org:write'])}
  89. initialData={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. {...routerProps}
  117. access={new Set(['org:write'])}
  118. initialData={Organization({codecovAccess: false})}
  119. onSave={onSave}
  120. />,
  121. {
  122. organization: {
  123. ...organization,
  124. features: ['codecov-integration'],
  125. },
  126. }
  127. );
  128. await userEvent.click(
  129. screen.getByRole('checkbox', {name: /Enable Code Coverage Insights/})
  130. );
  131. expect(putMock).toHaveBeenCalledWith(
  132. '/organizations/org-slug/',
  133. expect.objectContaining({
  134. data: {
  135. codecovAccess: true,
  136. },
  137. })
  138. );
  139. });
  140. });