projectTeams.spec.tsx 9.4 KB

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