projectTeams.spec.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {mountGlobalModal} from 'sentry-test/modal';
  3. import * as modals from 'app/actionCreators/modal';
  4. import App from 'app/views/app';
  5. import ProjectTeams from 'app/views/settings/project/projectTeams';
  6. jest.unmock('app/actionCreators/modal');
  7. describe('ProjectTeams', function () {
  8. let org;
  9. let project;
  10. let team;
  11. const team2 = {
  12. id: '2',
  13. slug: 'team-slug-2',
  14. name: 'Team Name 2',
  15. hasAccess: true,
  16. };
  17. beforeEach(function () {
  18. jest.spyOn(modals, 'openCreateTeamModal');
  19. org = TestStubs.Organization();
  20. project = TestStubs.ProjectDetails();
  21. team = TestStubs.Team();
  22. MockApiClient.addMockResponse({
  23. url: `/projects/${org.slug}/${project.slug}/`,
  24. method: 'GET',
  25. body: project,
  26. });
  27. MockApiClient.addMockResponse({
  28. url: `/projects/${org.slug}/${project.slug}/teams/`,
  29. method: 'GET',
  30. body: [team],
  31. });
  32. MockApiClient.addMockResponse({
  33. url: `/organizations/${org.slug}/teams/`,
  34. method: 'GET',
  35. body: [team, team2],
  36. });
  37. });
  38. afterEach(function () {
  39. MockApiClient.clearMockResponses();
  40. modals.openCreateTeamModal.mockRestore();
  41. });
  42. it('renders', async function () {
  43. const wrapper = mountWithTheme(
  44. <ProjectTeams
  45. params={{orgId: org.slug, projectId: project.slug}}
  46. organization={org}
  47. />,
  48. TestStubs.routerContext()
  49. );
  50. // Wait for team list to fetch.
  51. await wrapper.update();
  52. expect(wrapper).toSnapshot();
  53. });
  54. it('can remove a team from project', async function () {
  55. MockApiClient.addMockResponse({
  56. url: `/projects/${org.slug}/${project.slug}/teams/`,
  57. method: 'GET',
  58. body: [team, team2],
  59. });
  60. const endpoint = `/projects/${org.slug}/${project.slug}/teams/${team.slug}/`;
  61. const mock = MockApiClient.addMockResponse({
  62. url: endpoint,
  63. method: 'DELETE',
  64. statusCode: 200,
  65. });
  66. const endpoint2 = `/projects/${org.slug}/${project.slug}/teams/${team2.slug}/`;
  67. const mock2 = MockApiClient.addMockResponse({
  68. url: endpoint2,
  69. method: 'DELETE',
  70. statusCode: 200,
  71. });
  72. const wrapper = mountWithTheme(
  73. <ProjectTeams
  74. params={{orgId: org.slug, projectId: project.slug}}
  75. organization={org}
  76. />,
  77. TestStubs.routerContext()
  78. );
  79. // Wait for team list to fetch.
  80. await wrapper.update();
  81. expect(mock).not.toHaveBeenCalled();
  82. // Click "Remove"
  83. wrapper.find('PanelBody Button').first().simulate('click');
  84. expect(mock).toHaveBeenCalledWith(
  85. endpoint,
  86. expect.objectContaining({
  87. method: 'DELETE',
  88. })
  89. );
  90. await tick();
  91. // Remove second team
  92. wrapper.update().find('PanelBody Button').first().simulate('click');
  93. // Modal opens because this is the last team in project
  94. // Click confirm
  95. const modal = await mountGlobalModal();
  96. modal.find('Button[priority="primary"]').simulate('click');
  97. expect(mock2).toHaveBeenCalledWith(
  98. endpoint2,
  99. expect.objectContaining({
  100. method: 'DELETE',
  101. })
  102. );
  103. });
  104. it('removes team from project when project team is not in org list', async function () {
  105. MockApiClient.clearMockResponses();
  106. MockApiClient.addMockResponse({
  107. url: `/projects/${org.slug}/${project.slug}/teams/`,
  108. method: 'GET',
  109. body: [team, team2],
  110. });
  111. const endpoint = `/projects/${org.slug}/${project.slug}/teams/${team.slug}/`;
  112. const mock = MockApiClient.addMockResponse({
  113. url: endpoint,
  114. method: 'DELETE',
  115. });
  116. const endpoint2 = `/projects/${org.slug}/${project.slug}/teams/${team2.slug}/`;
  117. const mock2 = MockApiClient.addMockResponse({
  118. url: endpoint2,
  119. method: 'DELETE',
  120. });
  121. MockApiClient.addMockResponse({
  122. url: `/organizations/${org.slug}/teams/`,
  123. method: 'GET',
  124. body: [
  125. TestStubs.Team({
  126. id: '3',
  127. slug: 'team-slug-3',
  128. name: 'Team Name 3',
  129. hasAccess: true,
  130. }),
  131. ],
  132. });
  133. const wrapper = mountWithTheme(
  134. <ProjectTeams
  135. params={{orgId: org.slug, projectId: project.slug}}
  136. organization={org}
  137. />,
  138. TestStubs.routerContext()
  139. );
  140. // Wait for team list to fetch.
  141. await wrapper.update();
  142. expect(mock).not.toHaveBeenCalled();
  143. // Click "Remove"
  144. wrapper.find('PanelBody Button').first().simulate('click');
  145. expect(mock).toHaveBeenCalledWith(
  146. endpoint,
  147. expect.objectContaining({
  148. method: 'DELETE',
  149. })
  150. );
  151. await tick();
  152. // Remove second team
  153. wrapper.update().find('PanelBody Button').first().simulate('click');
  154. // Modal opens because this is the last team in project
  155. // Click confirm
  156. const modal = await mountGlobalModal();
  157. modal.find('Button[priority="primary"]').simulate('click');
  158. expect(mock2).toHaveBeenCalledWith(
  159. endpoint2,
  160. expect.objectContaining({
  161. method: 'DELETE',
  162. })
  163. );
  164. });
  165. it('can associate a team with project', async function () {
  166. const endpoint = `/projects/${org.slug}/${project.slug}/teams/${team2.slug}/`;
  167. const mock = MockApiClient.addMockResponse({
  168. url: endpoint,
  169. method: 'POST',
  170. statusCode: 200,
  171. });
  172. const wrapper = mountWithTheme(
  173. <ProjectTeams
  174. params={{orgId: org.slug, projectId: project.slug}}
  175. organization={org}
  176. />,
  177. TestStubs.routerContext()
  178. );
  179. // Wait for team list to fetch.
  180. await wrapper.update();
  181. expect(mock).not.toHaveBeenCalled();
  182. // open dropdown
  183. wrapper.find('DropdownButton').simulate('click');
  184. // click a team
  185. const el = wrapper.find('AutoCompleteItem').first();
  186. el.simulate('click');
  187. expect(mock).toHaveBeenCalledWith(
  188. endpoint,
  189. expect.objectContaining({
  190. method: 'POST',
  191. })
  192. );
  193. });
  194. it('creates a new team adds it to current project using the "create team modal" in dropdown', async function () {
  195. MockApiClient.addMockResponse({
  196. url: '/internal/health/',
  197. body: {},
  198. });
  199. MockApiClient.addMockResponse({
  200. url: '/assistant/?v2',
  201. body: {},
  202. });
  203. MockApiClient.addMockResponse({
  204. url: '/organizations/',
  205. body: [org],
  206. });
  207. const addTeamToProject = MockApiClient.addMockResponse({
  208. url: `/projects/${org.slug}/${project.slug}/teams/new-team/`,
  209. method: 'POST',
  210. });
  211. const createTeam = MockApiClient.addMockResponse({
  212. url: `/organizations/${org.slug}/teams/`,
  213. method: 'POST',
  214. body: {slug: 'new-team'},
  215. });
  216. const wrapper = mountWithTheme(
  217. <App params={{orgId: org.slug}}>
  218. <ProjectTeams
  219. params={{orgId: org.slug, projectId: project.slug}}
  220. project={project}
  221. organization={org}
  222. />
  223. </App>,
  224. TestStubs.routerContext()
  225. );
  226. // Wait for team list to fetch.
  227. await wrapper.update();
  228. // Open the dropdown
  229. wrapper.find('TeamSelect DropdownButton').simulate('click');
  230. // Click "Create Team" inside of dropdown
  231. wrapper.find('TeamSelect StyledCreateTeamLink').simulate('click');
  232. // action creator to open "create team modal" is called
  233. expect(modals.openCreateTeamModal).toHaveBeenCalledWith(
  234. expect.objectContaining({
  235. project: expect.objectContaining({
  236. slug: project.slug,
  237. }),
  238. organization: expect.objectContaining({
  239. slug: org.slug,
  240. }),
  241. })
  242. );
  243. // Two ticks are required
  244. await tick();
  245. await tick();
  246. wrapper.update();
  247. wrapper.find('input[name="slug"]').simulate('change', {target: {value: 'new-team'}});
  248. wrapper.find('[data-test-id="create-team-form"] form').simulate('submit');
  249. expect(createTeam).toHaveBeenCalledTimes(1);
  250. expect(createTeam).toHaveBeenCalledWith(
  251. '/organizations/org-slug/teams/',
  252. expect.objectContaining({
  253. data: {slug: 'new-team'},
  254. })
  255. );
  256. await tick();
  257. expect(addTeamToProject).toHaveBeenCalledTimes(1);
  258. expect(addTeamToProject).toHaveBeenCalledWith(
  259. '/projects/org-slug/project-slug/teams/new-team/',
  260. expect.anything()
  261. );
  262. });
  263. });