projectTeams.spec.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import React from 'react';
  2. import {shallow, mountWithTheme} from 'sentry-test/enzyme';
  3. import App from 'app/views/app';
  4. import ProjectTeams from 'app/views/settings/project/projectTeams';
  5. import * as modals from 'app/actionCreators/modal';
  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 = shallow(
  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).toMatchSnapshot();
  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
  84. .find('PanelBody Button')
  85. .first()
  86. .simulate('click');
  87. expect(mock).toHaveBeenCalledWith(
  88. endpoint,
  89. expect.objectContaining({
  90. method: 'DELETE',
  91. })
  92. );
  93. await tick();
  94. // Remove second team
  95. wrapper
  96. .update()
  97. .find('PanelBody Button')
  98. .first()
  99. .simulate('click');
  100. // Modal opens because this is the last team in project
  101. // Click confirm
  102. wrapper.find('ModalDialog Button[priority="primary"]').simulate('click');
  103. expect(mock2).toHaveBeenCalledWith(
  104. endpoint2,
  105. expect.objectContaining({
  106. method: 'DELETE',
  107. })
  108. );
  109. });
  110. it('removes team from project when project team is not in org list', async function() {
  111. MockApiClient.clearMockResponses();
  112. MockApiClient.addMockResponse({
  113. url: `/projects/${org.slug}/${project.slug}/teams/`,
  114. method: 'GET',
  115. body: [team, team2],
  116. });
  117. const endpoint = `/projects/${org.slug}/${project.slug}/teams/${team.slug}/`;
  118. const mock = MockApiClient.addMockResponse({
  119. url: endpoint,
  120. method: 'DELETE',
  121. });
  122. const endpoint2 = `/projects/${org.slug}/${project.slug}/teams/${team2.slug}/`;
  123. const mock2 = MockApiClient.addMockResponse({
  124. url: endpoint2,
  125. method: 'DELETE',
  126. });
  127. MockApiClient.addMockResponse({
  128. url: `/organizations/${org.slug}/teams/`,
  129. method: 'GET',
  130. body: [
  131. TestStubs.Team({
  132. id: '3',
  133. slug: 'team-slug-3',
  134. name: 'Team Name 3',
  135. hasAccess: true,
  136. }),
  137. ],
  138. });
  139. const wrapper = mountWithTheme(
  140. <ProjectTeams
  141. params={{orgId: org.slug, projectId: project.slug}}
  142. organization={org}
  143. />,
  144. TestStubs.routerContext()
  145. );
  146. // Wait for team list to fetch.
  147. await wrapper.update();
  148. expect(mock).not.toHaveBeenCalled();
  149. // Click "Remove"
  150. wrapper
  151. .find('PanelBody Button')
  152. .first()
  153. .simulate('click');
  154. expect(mock).toHaveBeenCalledWith(
  155. endpoint,
  156. expect.objectContaining({
  157. method: 'DELETE',
  158. })
  159. );
  160. await tick();
  161. // Remove second team
  162. wrapper
  163. .update()
  164. .find('PanelBody Button')
  165. .first()
  166. .simulate('click');
  167. // Modal opens because this is the last team in project
  168. // Click confirm
  169. wrapper.find('ModalDialog Button[priority="primary"]').simulate('click');
  170. expect(mock2).toHaveBeenCalledWith(
  171. endpoint2,
  172. expect.objectContaining({
  173. method: 'DELETE',
  174. })
  175. );
  176. });
  177. it('can associate a team with project', async function() {
  178. const endpoint = `/projects/${org.slug}/${project.slug}/teams/${team2.slug}/`;
  179. const mock = MockApiClient.addMockResponse({
  180. url: endpoint,
  181. method: 'POST',
  182. statusCode: 200,
  183. });
  184. const wrapper = mountWithTheme(
  185. <ProjectTeams
  186. params={{orgId: org.slug, projectId: project.slug}}
  187. organization={org}
  188. />,
  189. TestStubs.routerContext()
  190. );
  191. // Wait for team list to fetch.
  192. await wrapper.update();
  193. expect(mock).not.toHaveBeenCalled();
  194. // open dropdown
  195. wrapper.find('DropdownButton').simulate('click');
  196. // click a team
  197. const el = wrapper.find('AutoCompleteItem').first();
  198. el.simulate('click');
  199. expect(mock).toHaveBeenCalledWith(
  200. endpoint,
  201. expect.objectContaining({
  202. method: 'POST',
  203. })
  204. );
  205. });
  206. it('creates a new team adds it to current project using the "create team modal" in dropdown', async function() {
  207. MockApiClient.addMockResponse({
  208. url: '/internal/health/',
  209. body: {},
  210. });
  211. MockApiClient.addMockResponse({
  212. url: '/assistant/',
  213. body: {},
  214. });
  215. MockApiClient.addMockResponse({
  216. url: '/organizations/',
  217. body: [org],
  218. });
  219. const addTeamToProject = MockApiClient.addMockResponse({
  220. url: `/projects/${org.slug}/${project.slug}/teams/new-team/`,
  221. method: 'POST',
  222. });
  223. const createTeam = MockApiClient.addMockResponse({
  224. url: `/organizations/${org.slug}/teams/`,
  225. method: 'POST',
  226. body: {slug: 'new-team'},
  227. });
  228. const wrapper = mountWithTheme(
  229. <App params={{orgId: org.slug}}>
  230. <ProjectTeams
  231. params={{orgId: org.slug, projectId: project.slug}}
  232. project={project}
  233. organization={org}
  234. />
  235. </App>,
  236. TestStubs.routerContext()
  237. );
  238. // Wait for team list to fetch.
  239. await wrapper.update();
  240. // Open the dropdown
  241. wrapper.find('TeamSelect DropdownButton').simulate('click');
  242. // Click "Create Team" inside of dropdown
  243. wrapper.find('TeamSelect StyledCreateTeamLink').simulate('click');
  244. // action creator to open "create team modal" is called
  245. expect(modals.openCreateTeamModal).toHaveBeenCalledWith(
  246. expect.objectContaining({
  247. project: expect.objectContaining({
  248. slug: project.slug,
  249. }),
  250. organization: expect.objectContaining({
  251. slug: org.slug,
  252. }),
  253. })
  254. );
  255. // Two ticks are required
  256. await tick();
  257. await tick();
  258. wrapper.update();
  259. wrapper.find('input[name="slug"]').simulate('change', {target: {value: 'new-team'}});
  260. wrapper.find('[data-test-id="create-team-form"] form').simulate('submit');
  261. expect(createTeam).toHaveBeenCalledTimes(1);
  262. expect(createTeam).toHaveBeenCalledWith(
  263. '/organizations/org-slug/teams/',
  264. expect.objectContaining({
  265. data: {slug: 'new-team'},
  266. })
  267. );
  268. await tick();
  269. expect(addTeamToProject).toHaveBeenCalledTimes(1);
  270. expect(addTeamToProject).toHaveBeenCalledWith(
  271. '/projects/org-slug/project-slug/teams/new-team/',
  272. expect.anything()
  273. );
  274. });
  275. });