index.spec.tsx 5.1 KB

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