index.spec.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 organization = TestStubs.Organization({
  62. access: ['org:admin'],
  63. features: ['org-roles-for-teams'],
  64. });
  65. render(<TeamSettings {...routerProps} team={team} params={{teamId: team.slug}} />, {
  66. organization,
  67. });
  68. // set org role
  69. const unsetDropdown = await screen.findByText('None');
  70. await selectEvent.select(unsetDropdown, 'Owner');
  71. await userEvent.click(screen.getByRole('button', {name: 'Save'}));
  72. expect(putMock).toHaveBeenCalledWith(
  73. `/teams/org-slug/${team.slug}/`,
  74. expect.objectContaining({
  75. data: {
  76. orgRole: 'owner',
  77. },
  78. })
  79. );
  80. // unset org role
  81. const setDropdown = await screen.findByText('Owner');
  82. await selectEvent.select(setDropdown, 'None');
  83. await userEvent.click(screen.getByRole('button', {name: 'Save'}));
  84. expect(putMock).toHaveBeenCalledWith(
  85. `/teams/org-slug/${team.slug}/`,
  86. expect.objectContaining({
  87. data: {
  88. orgRole: '',
  89. },
  90. })
  91. );
  92. });
  93. it('needs team:admin in order to see an enabled Remove Team button', function () {
  94. const team = TestStubs.Team();
  95. const organization = TestStubs.Organization({access: []});
  96. render(<TeamSettings {...routerProps} team={team} params={{teamId: team.slug}} />, {
  97. organization,
  98. });
  99. expect(screen.getByTestId('button-remove-team')).toBeDisabled();
  100. });
  101. it('needs org:admin in order to set team org-role', function () {
  102. const team = TestStubs.Team();
  103. const organization = TestStubs.Organization({
  104. access: [],
  105. features: ['org-roles-for-teams'],
  106. });
  107. render(<TeamSettings {...routerProps} team={team} params={{teamId: team.slug}} />, {
  108. organization,
  109. });
  110. expect(screen.getByRole('textbox', {name: 'Organization Role'})).toBeDisabled();
  111. });
  112. it('cannot set team org-role for idp:provisioned team', function () {
  113. const team = TestStubs.Team({flags: {'idp:provisioned': true}});
  114. const organization = TestStubs.Organization({
  115. access: ['org:admin'],
  116. features: ['org-roles-for-teams'],
  117. });
  118. render(<TeamSettings {...routerProps} team={team} params={{teamId: team.slug}} />, {
  119. organization,
  120. });
  121. expect(screen.getByRole('textbox', {name: 'Organization Role'})).toBeDisabled();
  122. });
  123. it('can remove team', async function () {
  124. const team = TestStubs.Team({hasAccess: true});
  125. const deleteMock = MockApiClient.addMockResponse({
  126. url: `/teams/org-slug/${team.slug}/`,
  127. method: 'DELETE',
  128. });
  129. TeamStore.loadInitialData([team]);
  130. render(<TeamSettings {...routerProps} params={{teamId: team.slug}} team={team} />);
  131. // Click "Remove Team button
  132. await userEvent.click(screen.getByRole('button', {name: 'Remove Team'}));
  133. // Wait for modal
  134. renderGlobalModal();
  135. await userEvent.click(screen.getByTestId('confirm-button'));
  136. expect(deleteMock).toHaveBeenCalledWith(
  137. `/teams/org-slug/${team.slug}/`,
  138. expect.objectContaining({
  139. method: 'DELETE',
  140. })
  141. );
  142. await waitFor(() =>
  143. expect(browserHistory.replace).toHaveBeenCalledWith('/settings/org-slug/teams/')
  144. );
  145. expect(TeamStore.getAll()).toEqual([]);
  146. });
  147. });