index.spec.tsx 5.3 KB

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