projectTeams.spec.tsx 9.3 KB

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