teamMembers.spec.jsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {
  3. openInviteMembersModal,
  4. openTeamAccessRequestModal,
  5. } from 'sentry/actionCreators/modal';
  6. import {Client} from 'sentry/api';
  7. import TeamMembers from 'sentry/views/settings/organizationTeams/teamMembers';
  8. jest.mock('sentry/actionCreators/modal', () => ({
  9. openInviteMembersModal: jest.fn(),
  10. openTeamAccessRequestModal: jest.fn(),
  11. }));
  12. describe('TeamMembers', function () {
  13. let createMock;
  14. const organization = TestStubs.Organization();
  15. const team = TestStubs.Team();
  16. const members = TestStubs.Members();
  17. const member = TestStubs.Member({
  18. id: '9',
  19. email: 'sentry9@test.com',
  20. name: 'Sentry 9 Name',
  21. });
  22. beforeEach(function () {
  23. Client.clearMockResponses();
  24. Client.addMockResponse({
  25. url: `/organizations/${organization.slug}/members/`,
  26. method: 'GET',
  27. body: [member],
  28. });
  29. Client.addMockResponse({
  30. url: `/teams/${organization.slug}/${team.slug}/members/`,
  31. method: 'GET',
  32. body: members,
  33. });
  34. createMock = Client.addMockResponse({
  35. url: `/organizations/${organization.slug}/members/${member.id}/teams/${team.slug}/`,
  36. method: 'POST',
  37. });
  38. });
  39. it('can add member to team with open membership', async function () {
  40. const org = TestStubs.Organization({access: [], openMembership: true});
  41. render(
  42. <TeamMembers params={{orgId: org.slug, teamId: team.slug}} organization={org} />
  43. );
  44. userEvent.click((await screen.findAllByRole('button', {name: 'Add Member'}))[0]);
  45. userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
  46. expect(createMock).toHaveBeenCalled();
  47. });
  48. it('can add member to team with team:admin permission', async function () {
  49. const org = TestStubs.Organization({access: ['team:admin'], openMembership: false});
  50. render(
  51. <TeamMembers params={{orgId: org.slug, teamId: team.slug}} organization={org} />
  52. );
  53. userEvent.click((await screen.findAllByRole('button', {name: 'Add Member'}))[0]);
  54. userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
  55. expect(createMock).toHaveBeenCalled();
  56. });
  57. it('can add member to team with org:write permission', async function () {
  58. const org = TestStubs.Organization({access: ['org:write'], openMembership: false});
  59. render(
  60. <TeamMembers params={{orgId: org.slug, teamId: team.slug}} organization={org} />
  61. );
  62. userEvent.click((await screen.findAllByRole('button', {name: 'Add Member'}))[0]);
  63. userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
  64. expect(createMock).toHaveBeenCalled();
  65. });
  66. it('can request access to add member to team without permission', async function () {
  67. const org = TestStubs.Organization({access: [], openMembership: false});
  68. render(
  69. <TeamMembers params={{orgId: org.slug, teamId: team.slug}} organization={org} />
  70. );
  71. userEvent.click((await screen.findAllByRole('button', {name: 'Add Member'}))[0]);
  72. userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
  73. expect(openTeamAccessRequestModal).toHaveBeenCalled();
  74. });
  75. it('can invite member from team dropdown with access', async function () {
  76. const org = TestStubs.Organization({access: ['team:admin'], openMembership: false});
  77. render(
  78. <TeamMembers params={{orgId: org.slug, teamId: team.slug}} organization={org} />
  79. );
  80. userEvent.click((await screen.findAllByRole('button', {name: 'Add Member'}))[0]);
  81. userEvent.click(screen.getByTestId('invite-member'));
  82. expect(openInviteMembersModal).toHaveBeenCalled();
  83. });
  84. it('can invite member from team dropdown with access and `Open Membership` enabled', async function () {
  85. const org = TestStubs.Organization({access: ['team:admin'], openMembership: true});
  86. render(
  87. <TeamMembers params={{orgId: org.slug, teamId: team.slug}} organization={org} />
  88. );
  89. userEvent.click((await screen.findAllByRole('button', {name: 'Add Member'}))[0]);
  90. userEvent.click(screen.getByTestId('invite-member'));
  91. expect(openInviteMembersModal).toHaveBeenCalled();
  92. });
  93. it('can invite member from team dropdown without access and `Open Membership` enabled', async function () {
  94. const org = TestStubs.Organization({access: [], openMembership: true});
  95. render(
  96. <TeamMembers params={{orgId: org.slug, teamId: team.slug}} organization={org} />
  97. );
  98. userEvent.click((await screen.findAllByRole('button', {name: 'Add Member'}))[0]);
  99. userEvent.click(screen.getByTestId('invite-member'));
  100. expect(openInviteMembersModal).toHaveBeenCalled();
  101. });
  102. it('can invite member from team dropdown without access and `Open Membership` disabled', async function () {
  103. const org = TestStubs.Organization({access: [], openMembership: false});
  104. render(
  105. <TeamMembers params={{orgId: org.slug, teamId: team.slug}} organization={org} />
  106. );
  107. userEvent.click((await screen.findAllByRole('button', {name: 'Add Member'}))[0]);
  108. userEvent.click(screen.getByTestId('invite-member'));
  109. expect(openInviteMembersModal).toHaveBeenCalled();
  110. });
  111. it('can remove member from team', async function () {
  112. const deleteMock = Client.addMockResponse({
  113. url: `/organizations/${organization.slug}/members/${members[0].id}/teams/${team.slug}/`,
  114. method: 'DELETE',
  115. });
  116. render(
  117. <TeamMembers
  118. params={{orgId: organization.slug, teamId: team.slug}}
  119. organization={organization}
  120. />
  121. );
  122. await screen.findAllByRole('button', {name: 'Add Member'});
  123. expect(deleteMock).not.toHaveBeenCalled();
  124. userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  125. expect(deleteMock).toHaveBeenCalled();
  126. });
  127. it('can only remove self from team', async function () {
  128. const me = TestStubs.Member({
  129. id: '123',
  130. email: 'foo@example.com',
  131. });
  132. Client.addMockResponse({
  133. url: `/teams/${organization.slug}/${team.slug}/members/`,
  134. method: 'GET',
  135. body: [...members, me],
  136. });
  137. const deleteMock = Client.addMockResponse({
  138. url: `/organizations/${organization.slug}/members/${me.id}/teams/${team.slug}/`,
  139. method: 'DELETE',
  140. });
  141. const organizationMember = TestStubs.Organization({access: []});
  142. render(
  143. <TeamMembers
  144. params={{orgId: organization.slug, teamId: team.slug}}
  145. organization={organizationMember}
  146. />
  147. );
  148. await screen.findAllByRole('button', {name: 'Add Member'});
  149. expect(deleteMock).not.toHaveBeenCalled();
  150. expect(screen.getAllByTestId('letter_avatar-avatar')).toHaveLength(
  151. members.length + 1
  152. );
  153. // Can only remove self
  154. expect(screen.getByRole('button', {name: 'Remove'})).toBeInTheDocument();
  155. userEvent.click(screen.getByRole('button', {name: 'Remove'}));
  156. expect(deleteMock).toHaveBeenCalled();
  157. });
  158. it('does not renders team-level roles', async function () {
  159. const me = TestStubs.Member({
  160. id: '123',
  161. email: 'foo@example.com',
  162. role: 'owner',
  163. });
  164. Client.addMockResponse({
  165. url: `/teams/${organization.slug}/${team.slug}/members/`,
  166. method: 'GET',
  167. body: [...members, me],
  168. });
  169. await render(
  170. <TeamMembers
  171. params={{orgId: organization.slug, teamId: team.slug}}
  172. organization={organization}
  173. />
  174. );
  175. const admins = screen.queryByText('Team Admin');
  176. expect(admins).not.toBeInTheDocument();
  177. const contributors = screen.queryByText('Team Contributor');
  178. expect(contributors).not.toBeInTheDocument();
  179. });
  180. it('renders team-level roles with flag', async function () {
  181. const manager = TestStubs.Member({
  182. id: '123',
  183. email: 'foo@example.com',
  184. orgRole: 'manager',
  185. });
  186. Client.addMockResponse({
  187. url: `/teams/${organization.slug}/${team.slug}/members/`,
  188. method: 'GET',
  189. body: [...members, manager],
  190. });
  191. const orgWithTeamRoles = TestStubs.Organization({features: ['team-roles']});
  192. await render(
  193. <TeamMembers
  194. params={{orgId: orgWithTeamRoles.slug, teamId: team.slug}}
  195. organization={orgWithTeamRoles}
  196. />
  197. );
  198. const admins = screen.queryAllByText('Team Admin');
  199. expect(admins).toHaveLength(3);
  200. const contributors = screen.queryAllByText('Contributor');
  201. expect(contributors).toHaveLength(2);
  202. });
  203. });