projectTeams.spec.jsx 7.2 KB

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