index.spec.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import {browserHistory} from 'react-router';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {
  5. act,
  6. render,
  7. renderGlobalModal,
  8. screen,
  9. userEvent,
  10. waitFor,
  11. within,
  12. } from 'sentry-test/reactTestingLibrary';
  13. import OrganizationsStore from 'sentry/stores/organizationsStore';
  14. import ProjectsStore from 'sentry/stores/projectsStore';
  15. import {trackAnalytics} from 'sentry/utils/analytics';
  16. import OrganizationGeneralSettings from 'sentry/views/settings/organizationGeneralSettings';
  17. jest.mock('sentry/utils/analytics');
  18. describe('OrganizationGeneralSettings', function () {
  19. const ENDPOINT = '/organizations/org-slug/';
  20. const {organization, router} = initializeOrg();
  21. const defaultProps = {
  22. organization,
  23. router,
  24. location: router.location,
  25. params: {orgId: organization.slug},
  26. routes: router.routes,
  27. route: {},
  28. routeParams: router.params,
  29. };
  30. beforeEach(function () {
  31. OrganizationsStore.addOrReplace(organization);
  32. MockApiClient.addMockResponse({
  33. url: `/organizations/${organization.slug}/auth-provider/`,
  34. method: 'GET',
  35. });
  36. MockApiClient.addMockResponse({
  37. url: `/organizations/${organization.slug}/integrations/?provider_key=github`,
  38. method: 'GET',
  39. body: [TestStubs.GitHubIntegration()],
  40. });
  41. });
  42. it('can enable "early adopter"', async function () {
  43. render(<OrganizationGeneralSettings {...defaultProps} />);
  44. const mock = MockApiClient.addMockResponse({
  45. url: ENDPOINT,
  46. method: 'PUT',
  47. });
  48. await userEvent.click(screen.getByRole('checkbox', {name: /early adopter/i}));
  49. await waitFor(() => {
  50. expect(mock).toHaveBeenCalledWith(
  51. ENDPOINT,
  52. expect.objectContaining({
  53. data: {isEarlyAdopter: true},
  54. })
  55. );
  56. });
  57. });
  58. it('can enable "codecov access"', async function () {
  59. const organizationWithCodecovFeature = Organization({
  60. features: ['codecov-integration'],
  61. codecovAccess: false,
  62. });
  63. render(
  64. <OrganizationGeneralSettings
  65. {...defaultProps}
  66. organization={organizationWithCodecovFeature}
  67. />,
  68. {organization: organizationWithCodecovFeature}
  69. );
  70. const mock = MockApiClient.addMockResponse({
  71. url: ENDPOINT,
  72. method: 'PUT',
  73. });
  74. await userEvent.click(
  75. screen.getByRole('checkbox', {name: /Enable Code Coverage Insights/i})
  76. );
  77. await waitFor(() => {
  78. expect(mock).toHaveBeenCalledWith(
  79. ENDPOINT,
  80. expect.objectContaining({
  81. data: {codecovAccess: true},
  82. })
  83. );
  84. });
  85. expect(trackAnalytics).toHaveBeenCalled();
  86. });
  87. it('changes org slug and redirects to new slug', async function () {
  88. render(<OrganizationGeneralSettings {...defaultProps} />);
  89. const mock = MockApiClient.addMockResponse({
  90. url: ENDPOINT,
  91. method: 'PUT',
  92. body: {...organization, slug: 'new-slug'},
  93. });
  94. await userEvent.clear(screen.getByRole('textbox', {name: /slug/i}));
  95. await userEvent.type(screen.getByRole('textbox', {name: /slug/i}), 'new-slug');
  96. await userEvent.click(screen.getByLabelText('Save'));
  97. await waitFor(() => {
  98. expect(mock).toHaveBeenCalledWith(
  99. ENDPOINT,
  100. expect.objectContaining({
  101. data: {slug: 'new-slug'},
  102. })
  103. );
  104. expect(browserHistory.replace).toHaveBeenCalledWith('/settings/new-slug/');
  105. });
  106. });
  107. it('changes org slug and redirects to new customer-domain', async function () {
  108. const org = Organization({features: ['customer-domains']});
  109. const updateMock = MockApiClient.addMockResponse({
  110. url: `/organizations/${organization.slug}/`,
  111. method: 'PUT',
  112. body: {...org, slug: 'acme', links: {organizationUrl: 'https://acme.sentry.io'}},
  113. });
  114. render(<OrganizationGeneralSettings {...defaultProps} organization={org} />);
  115. const input = screen.getByRole('textbox', {name: /slug/i});
  116. await userEvent.clear(input);
  117. await userEvent.type(input, 'acme');
  118. await userEvent.click(screen.getByLabelText('Save'));
  119. await waitFor(() => {
  120. expect(updateMock).toHaveBeenCalledWith(
  121. '/organizations/org-slug/',
  122. expect.objectContaining({
  123. data: {
  124. slug: 'acme',
  125. },
  126. })
  127. );
  128. expect(window.location.replace).toHaveBeenCalledWith(
  129. 'https://acme.sentry.io/settings/organization/'
  130. );
  131. });
  132. });
  133. it('disables the entire form if user does not have write access', function () {
  134. const readOnlyOrg = Organization({access: ['org:read']});
  135. render(<OrganizationGeneralSettings {...defaultProps} organization={readOnlyOrg} />, {
  136. organization: readOnlyOrg,
  137. });
  138. const formElements = [
  139. ...screen.getAllByRole('textbox'),
  140. ...screen.getAllByRole('button'),
  141. ...screen.getAllByRole('checkbox'),
  142. ];
  143. for (const formElement of formElements) {
  144. expect(formElement).toBeDisabled();
  145. }
  146. expect(
  147. screen.getByText(
  148. 'These settings can only be edited by users with the organization owner or manager role.'
  149. )
  150. ).toBeInTheDocument();
  151. });
  152. it('does not have remove organization button without org:admin permission', function () {
  153. render(
  154. <OrganizationGeneralSettings
  155. {...defaultProps}
  156. organization={Organization({
  157. access: ['org:write'],
  158. })}
  159. />
  160. );
  161. expect(
  162. screen.queryByRole('button', {name: /remove organization/i})
  163. ).not.toBeInTheDocument();
  164. });
  165. it('can remove organization when org admin', async function () {
  166. act(() => ProjectsStore.loadInitialData([TestStubs.Project({slug: 'project'})]));
  167. render(
  168. <OrganizationGeneralSettings
  169. {...defaultProps}
  170. organization={Organization({access: ['org:admin']})}
  171. />
  172. );
  173. renderGlobalModal();
  174. const mock = MockApiClient.addMockResponse({
  175. url: ENDPOINT,
  176. method: 'DELETE',
  177. });
  178. await userEvent.click(screen.getByRole('button', {name: /remove organization/i}));
  179. const modal = screen.getByRole('dialog');
  180. expect(
  181. within(modal).getByText('This will also remove the following associated projects:')
  182. ).toBeInTheDocument();
  183. expect(within(modal).getByText('project')).toBeInTheDocument();
  184. await userEvent.click(
  185. within(modal).getByRole('button', {name: /remove organization/i})
  186. );
  187. await waitFor(() => {
  188. expect(mock).toHaveBeenCalledWith(
  189. ENDPOINT,
  190. expect.objectContaining({
  191. method: 'DELETE',
  192. })
  193. );
  194. });
  195. });
  196. });