index.spec.tsx 5.0 KB

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