projectTeams.spec.jsx 7.4 KB

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