projectTeams.spec.tsx 9.3 KB

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