index.spec.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import {browserHistory} from 'react-router';
  2. import selectEvent from 'react-select-event';
  3. import {
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. waitFor,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import TeamStore from 'sentry/stores/teamStore';
  11. import TeamSettings from 'sentry/views/settings/organizationTeams/teamSettings';
  12. describe('TeamSettings', function () {
  13. beforeEach(function () {
  14. MockApiClient.clearMockResponses();
  15. jest.spyOn(window.location, 'assign');
  16. });
  17. afterEach(function () {
  18. window.location.assign.mockRestore();
  19. });
  20. it('can change slug', async function () {
  21. const team = TestStubs.Team();
  22. const putMock = MockApiClient.addMockResponse({
  23. url: `/teams/org-slug/${team.slug}/`,
  24. method: 'PUT',
  25. body: {
  26. slug: 'new-slug',
  27. },
  28. });
  29. render(<TeamSettings team={team} params={{teamId: team.slug}} />);
  30. const input = screen.getByRole('textbox', {name: 'Team Slug'});
  31. await userEvent.clear(input);
  32. await userEvent.type(input, 'NEW SLUG');
  33. await userEvent.click(screen.getByRole('button', {name: 'Save'}));
  34. expect(putMock).toHaveBeenCalledWith(
  35. `/teams/org-slug/${team.slug}/`,
  36. expect.objectContaining({
  37. data: {
  38. slug: 'new-slug',
  39. },
  40. })
  41. );
  42. await waitFor(() =>
  43. expect(browserHistory.replace).toHaveBeenCalledWith(
  44. '/settings/org-slug/teams/new-slug/settings/'
  45. )
  46. );
  47. });
  48. it('can set team org-role', async function () {
  49. const team = TestStubs.Team({orgRole: ''});
  50. const putMock = MockApiClient.addMockResponse({
  51. url: `/teams/org-slug/${team.slug}/`,
  52. method: 'PUT',
  53. body: {
  54. slug: 'new-slug',
  55. orgRole: 'owner',
  56. },
  57. });
  58. const context = TestStubs.routerContext([
  59. {
  60. organization: TestStubs.Organization({
  61. access: ['org:admin'],
  62. features: ['org-roles-for-teams'],
  63. }),
  64. },
  65. ]);
  66. render(<TeamSettings team={team} params={{teamId: team.slug}} />, {
  67. context,
  68. });
  69. // set org role
  70. const unsetDropdown = await screen.findByText('None');
  71. await selectEvent.select(unsetDropdown, 'Owner');
  72. await userEvent.click(screen.getByRole('button', {name: 'Save'}));
  73. expect(putMock).toHaveBeenCalledWith(
  74. `/teams/org-slug/${team.slug}/`,
  75. expect.objectContaining({
  76. data: {
  77. orgRole: 'owner',
  78. },
  79. })
  80. );
  81. // unset org role
  82. const setDropdown = await screen.findByText('Owner');
  83. await selectEvent.select(setDropdown, 'None');
  84. await userEvent.click(screen.getByRole('button', {name: 'Save'}));
  85. expect(putMock).toHaveBeenCalledWith(
  86. `/teams/org-slug/${team.slug}/`,
  87. expect.objectContaining({
  88. data: {
  89. orgRole: '',
  90. },
  91. })
  92. );
  93. });
  94. it('needs team:admin in order to see an enabled Remove Team button', function () {
  95. const team = TestStubs.Team();
  96. const context = TestStubs.routerContext([
  97. {
  98. organization: TestStubs.Organization({access: []}),
  99. },
  100. ]);
  101. render(<TeamSettings team={team} params={{teamId: team.slug}} />, {
  102. context,
  103. });
  104. expect(screen.getByTestId('button-remove-team')).toBeDisabled();
  105. });
  106. it('needs org:admin in order to set team org-role', function () {
  107. const team = TestStubs.Team();
  108. const context = TestStubs.routerContext([
  109. {
  110. organization: TestStubs.Organization({
  111. access: [],
  112. features: ['org-roles-for-teams'],
  113. }),
  114. },
  115. ]);
  116. render(<TeamSettings team={team} params={{teamId: team.slug}} />, {
  117. context,
  118. });
  119. expect(screen.getByRole('textbox', {name: 'Organization Role'})).toBeDisabled();
  120. });
  121. it('cannot set team org-role for idp:provisioned team', function () {
  122. const team = TestStubs.Team({flags: {'idp:provisioned': true}});
  123. const context = TestStubs.routerContext([
  124. {
  125. organization: TestStubs.Organization({
  126. access: ['org:admin'],
  127. features: ['org-roles-for-teams'],
  128. }),
  129. },
  130. ]);
  131. render(<TeamSettings team={team} params={{teamId: team.slug}} />, {
  132. context,
  133. });
  134. expect(screen.getByRole('textbox', {name: 'Organization Role'})).toBeDisabled();
  135. });
  136. it('can remove team', async function () {
  137. const team = TestStubs.Team({hasAccess: true});
  138. const deleteMock = MockApiClient.addMockResponse({
  139. url: `/teams/org-slug/${team.slug}/`,
  140. method: 'DELETE',
  141. });
  142. TeamStore.loadInitialData([{slug: 'team-slug', hasAccess: true}]);
  143. render(<TeamSettings params={{teamId: team.slug}} team={team} />);
  144. // Click "Remove Team button
  145. await userEvent.click(screen.getByRole('button', {name: 'Remove Team'}));
  146. // Wait for modal
  147. renderGlobalModal();
  148. await userEvent.click(screen.getByTestId('confirm-button'));
  149. expect(deleteMock).toHaveBeenCalledWith(
  150. `/teams/org-slug/${team.slug}/`,
  151. expect.objectContaining({
  152. method: 'DELETE',
  153. })
  154. );
  155. await waitFor(() =>
  156. expect(browserHistory.replace).toHaveBeenCalledWith('/settings/org-slug/teams/')
  157. );
  158. expect(TeamStore.getAll()).toEqual([]);
  159. });
  160. });