projectTeams.spec.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import {TeamFixture} from 'sentry-fixture/team';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  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 type {Organization, Project} from 'sentry/types';
  12. import ProjectTeams from 'sentry/views/settings/project/projectTeams';
  13. describe('ProjectTeams', function () {
  14. let org: Organization;
  15. let project: Project;
  16. const team1WithAdmin = TeamFixture({
  17. access: ['team:read', 'team:write', 'team:admin'],
  18. });
  19. const team2WithAdmin = TeamFixture({
  20. id: '2',
  21. slug: 'team-slug-2',
  22. name: 'Team Name 2',
  23. hasAccess: true,
  24. access: ['team:read', 'team:write', 'team:admin'],
  25. });
  26. const team3NoAdmin = TeamFixture({
  27. id: '3',
  28. slug: 'team-slug-3',
  29. name: 'Team Name 3',
  30. hasAccess: true,
  31. access: ['team:read'],
  32. });
  33. beforeEach(function () {
  34. const initialData = initializeOrg();
  35. org = initialData.organization;
  36. project = {
  37. ...initialData.project,
  38. access: ['project:admin', 'project:write', 'project:admin'],
  39. };
  40. TeamStore.loadInitialData([team1WithAdmin, team2WithAdmin]);
  41. MockApiClient.addMockResponse({
  42. url: `/projects/${org.slug}/${project.slug}/`,
  43. method: 'GET',
  44. body: project,
  45. });
  46. MockApiClient.addMockResponse({
  47. url: `/projects/${org.slug}/${project.slug}/teams/`,
  48. method: 'GET',
  49. body: [team1WithAdmin],
  50. });
  51. MockApiClient.addMockResponse({
  52. url: `/organizations/${org.slug}/teams/`,
  53. method: 'GET',
  54. body: [team1WithAdmin, team2WithAdmin],
  55. });
  56. });
  57. afterEach(function () {
  58. MockApiClient.clearMockResponses();
  59. });
  60. it('can remove a team from project', async function () {
  61. MockApiClient.addMockResponse({
  62. url: `/projects/${org.slug}/${project.slug}/teams/`,
  63. method: 'GET',
  64. body: [team1WithAdmin, team2WithAdmin],
  65. });
  66. const endpoint1 = `/projects/${org.slug}/${project.slug}/teams/${team1WithAdmin.slug}/`;
  67. const mock1 = MockApiClient.addMockResponse({
  68. url: endpoint1,
  69. method: 'DELETE',
  70. statusCode: 200,
  71. });
  72. const endpoint2 = `/projects/${org.slug}/${project.slug}/teams/${team2WithAdmin.slug}/`;
  73. const mock2 = MockApiClient.addMockResponse({
  74. url: endpoint2,
  75. method: 'DELETE',
  76. statusCode: 200,
  77. });
  78. render(<ProjectTeams organization={org} project={project} />);
  79. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  80. expect(mock1).not.toHaveBeenCalled();
  81. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  82. renderGlobalModal();
  83. expect(screen.getByRole('dialog')).toBeInTheDocument();
  84. await userEvent.click(screen.getByText('Remove Team'));
  85. expect(mock1).toHaveBeenCalledWith(
  86. endpoint1,
  87. expect.objectContaining({
  88. method: 'DELETE',
  89. })
  90. );
  91. expect(screen.queryByText('#team-slug')).not.toBeInTheDocument();
  92. // Remove second team
  93. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  94. await userEvent.click(screen.getByText('Remove Team'));
  95. expect(mock2).toHaveBeenCalledWith(
  96. endpoint2,
  97. expect.objectContaining({
  98. method: 'DELETE',
  99. })
  100. );
  101. });
  102. it('cannot remove a team without admin scopes', async function () {
  103. MockApiClient.addMockResponse({
  104. url: `/projects/${org.slug}/${project.slug}/teams/`,
  105. method: 'GET',
  106. body: [team1WithAdmin, team2WithAdmin, team3NoAdmin],
  107. });
  108. const endpoint1 = `/projects/${org.slug}/${project.slug}/teams/${team1WithAdmin.slug}/`;
  109. const mock1 = MockApiClient.addMockResponse({
  110. url: endpoint1,
  111. method: 'DELETE',
  112. statusCode: 200,
  113. });
  114. const endpoint3 = `/projects/${org.slug}/${project.slug}/teams/${team3NoAdmin.slug}/`;
  115. const mock3 = MockApiClient.addMockResponse({
  116. url: endpoint3,
  117. method: 'DELETE',
  118. statusCode: 200,
  119. });
  120. render(<ProjectTeams organization={org} project={project} />);
  121. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  122. // Remove first team
  123. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  124. renderGlobalModal();
  125. await userEvent.click(screen.getByText('Remove Team'));
  126. expect(mock1).toHaveBeenCalledWith(
  127. endpoint1,
  128. expect.objectContaining({
  129. method: 'DELETE',
  130. })
  131. );
  132. expect(screen.queryByText('#team-slug')).not.toBeInTheDocument();
  133. // Remove third team, but button should be disabled
  134. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[1]);
  135. expect(mock3).not.toHaveBeenCalled();
  136. });
  137. it('removes team from project when project team is not in org list', async function () {
  138. MockApiClient.addMockResponse({
  139. url: `/projects/${org.slug}/${project.slug}/teams/`,
  140. method: 'GET',
  141. body: [team1WithAdmin, team2WithAdmin],
  142. });
  143. const endpoint1 = `/projects/${org.slug}/${project.slug}/teams/${team1WithAdmin.slug}/`;
  144. const mock1 = MockApiClient.addMockResponse({
  145. url: endpoint1,
  146. method: 'DELETE',
  147. });
  148. const endpoint2 = `/projects/${org.slug}/${project.slug}/teams/${team2WithAdmin.slug}/`;
  149. const mock2 = MockApiClient.addMockResponse({
  150. url: endpoint2,
  151. method: 'DELETE',
  152. });
  153. MockApiClient.addMockResponse({
  154. url: `/organizations/${org.slug}/teams/`,
  155. method: 'GET',
  156. body: [team3NoAdmin],
  157. });
  158. render(<ProjectTeams organization={org} project={project} />);
  159. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  160. expect(mock1).not.toHaveBeenCalled();
  161. // Remove first team
  162. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  163. renderGlobalModal();
  164. await userEvent.click(screen.getByText('Remove Team'));
  165. expect(mock1).toHaveBeenCalledWith(
  166. endpoint1,
  167. expect.objectContaining({
  168. method: 'DELETE',
  169. })
  170. );
  171. expect(screen.queryByText('#team-slug')).not.toBeInTheDocument();
  172. // Remove second team
  173. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  174. // Modal opens because this is the last team in project
  175. expect(screen.getByRole('dialog')).toBeInTheDocument();
  176. // Click confirm
  177. await userEvent.click(screen.getByText('Remove Team'));
  178. expect(mock2).toHaveBeenCalledWith(
  179. endpoint2,
  180. expect.objectContaining({
  181. method: 'DELETE',
  182. })
  183. );
  184. });
  185. it('can associate a team with project', async function () {
  186. const endpoint = `/projects/${org.slug}/${project.slug}/teams/${team2WithAdmin.slug}/`;
  187. const mock = MockApiClient.addMockResponse({
  188. url: endpoint,
  189. method: 'POST',
  190. statusCode: 200,
  191. });
  192. render(<ProjectTeams organization={org} project={project} />);
  193. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  194. expect(mock).not.toHaveBeenCalled();
  195. // Add a team
  196. await userEvent.click(screen.getAllByRole('button', {name: 'Add Team'})[1]);
  197. await userEvent.click(screen.getByText('#team-slug-2'));
  198. expect(mock).toHaveBeenCalledWith(
  199. endpoint,
  200. expect.objectContaining({
  201. method: 'POST',
  202. })
  203. );
  204. });
  205. it('creates a new team adds it to current project using the "create team modal" in dropdown', async function () {
  206. MockApiClient.addMockResponse({
  207. url: '/assistant/',
  208. body: {},
  209. });
  210. MockApiClient.addMockResponse({
  211. url: '/organizations/',
  212. body: [org],
  213. });
  214. const addTeamToProject = MockApiClient.addMockResponse({
  215. url: `/projects/${org.slug}/${project.slug}/teams/new-team/`,
  216. method: 'POST',
  217. });
  218. const createTeam = MockApiClient.addMockResponse({
  219. url: `/organizations/${org.slug}/teams/`,
  220. method: 'POST',
  221. body: TeamFixture({slug: 'new-team'}),
  222. });
  223. render(<ProjectTeams project={project} organization={org} />);
  224. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  225. // Add new team
  226. await userEvent.click(screen.getAllByRole('button', {name: 'Add Team'})[1]);
  227. // XXX(epurkhiser): Create Team should really be a button
  228. await userEvent.click(screen.getByRole('link', {name: 'Create Team'}));
  229. renderGlobalModal();
  230. await screen.findByRole('dialog');
  231. await userEvent.type(screen.getByRole('textbox', {name: 'Team Name'}), 'new-team');
  232. await userEvent.click(screen.getByRole('button', {name: 'Create Team'}));
  233. await waitFor(() => expect(createTeam).toHaveBeenCalledTimes(1));
  234. expect(createTeam).toHaveBeenCalledWith(
  235. '/organizations/org-slug/teams/',
  236. expect.objectContaining({
  237. data: {slug: 'new-team'},
  238. })
  239. );
  240. expect(addTeamToProject).toHaveBeenCalledTimes(1);
  241. expect(addTeamToProject).toHaveBeenCalledWith(
  242. '/projects/org-slug/project-slug/teams/new-team/',
  243. expect.anything()
  244. );
  245. });
  246. });