inviteRequestRow.spec.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import selectEvent from 'react-select-event';
  2. import {
  3. render,
  4. renderGlobalModal,
  5. screen,
  6. userEvent,
  7. } from 'sentry-test/reactTestingLibrary';
  8. import TeamStore from 'sentry/stores/teamStore';
  9. import {OrgRole} from 'sentry/types';
  10. import InviteRequestRow from 'sentry/views/settings/organizationMembers/inviteRequestRow';
  11. const roles: OrgRole[] = [
  12. {
  13. id: 'admin',
  14. name: 'Admin',
  15. desc: 'This is the admin role',
  16. minimumTeamRole: '',
  17. allowed: true,
  18. },
  19. {
  20. id: 'member',
  21. name: 'Member',
  22. desc: 'This is the member role',
  23. minimumTeamRole: '',
  24. allowed: true,
  25. },
  26. {
  27. id: 'owner',
  28. name: 'Owner',
  29. desc: 'This is the owner role',
  30. minimumTeamRole: '',
  31. allowed: false,
  32. },
  33. ];
  34. describe('InviteRequestRow', function () {
  35. const orgWithoutAdminAccess = TestStubs.Organization({
  36. access: [],
  37. });
  38. const orgWithAdminAccess = TestStubs.Organization({
  39. access: ['member:admin'],
  40. });
  41. const inviteRequestBusy: Record<string, boolean> = {};
  42. const inviteRequest = TestStubs.Member({
  43. user: null,
  44. inviterName: TestStubs.User().name,
  45. inviterId: TestStubs.User().id,
  46. inviteStatus: 'requested_to_be_invited',
  47. role: 'member',
  48. teams: ['myteam'],
  49. });
  50. const joinRequest = TestStubs.Member({
  51. user: null,
  52. inviteStatus: 'requested_to_join',
  53. role: 'member',
  54. teams: ['myteam'],
  55. });
  56. it('renders request to be invited', function () {
  57. render(
  58. <InviteRequestRow
  59. organization={orgWithoutAdminAccess}
  60. inviteRequest={inviteRequest}
  61. inviteRequestBusy={inviteRequestBusy}
  62. onApprove={() => {}}
  63. onDeny={() => {}}
  64. onUpdate={() => {}}
  65. allRoles={roles}
  66. />
  67. );
  68. expect(screen.getByText(inviteRequest.email)).toBeInTheDocument();
  69. expect(
  70. screen.getByText(`Requested by ${inviteRequest.inviterName}`)
  71. ).toBeInTheDocument();
  72. });
  73. it('renders request to join', function () {
  74. render(
  75. <InviteRequestRow
  76. organization={orgWithoutAdminAccess}
  77. inviteRequest={joinRequest}
  78. inviteRequestBusy={inviteRequestBusy}
  79. onApprove={() => {}}
  80. onDeny={() => {}}
  81. onUpdate={() => {}}
  82. allRoles={roles}
  83. />
  84. );
  85. expect(screen.getByText(joinRequest.email)).toBeInTheDocument();
  86. expect(screen.getByRole('button', {name: 'Approve'})).toBeInTheDocument();
  87. expect(screen.getByRole('button', {name: 'Deny'})).toBeInTheDocument();
  88. });
  89. it('admin can approve invite request', async function () {
  90. const mockApprove = jest.fn();
  91. const mockDeny = jest.fn();
  92. render(
  93. <InviteRequestRow
  94. organization={orgWithAdminAccess}
  95. inviteRequest={inviteRequest}
  96. inviteRequestBusy={inviteRequestBusy}
  97. onApprove={mockApprove}
  98. onDeny={mockDeny}
  99. onUpdate={() => {}}
  100. allRoles={roles}
  101. />
  102. );
  103. await userEvent.click(screen.getByRole('button', {name: 'Approve'}));
  104. renderGlobalModal();
  105. await userEvent.click(screen.getByTestId('confirm-button'));
  106. expect(mockApprove).toHaveBeenCalledWith(inviteRequest);
  107. expect(mockDeny).not.toHaveBeenCalled();
  108. });
  109. it('admin can deny invite request', async function () {
  110. const mockApprove = jest.fn();
  111. const mockDeny = jest.fn();
  112. render(
  113. <InviteRequestRow
  114. organization={orgWithAdminAccess}
  115. inviteRequest={inviteRequest}
  116. inviteRequestBusy={inviteRequestBusy}
  117. onApprove={mockApprove}
  118. onDeny={mockDeny}
  119. onUpdate={() => {}}
  120. allRoles={roles}
  121. />
  122. );
  123. await userEvent.click(screen.getByRole('button', {name: 'Deny'}));
  124. expect(mockDeny).toHaveBeenCalledWith(inviteRequest);
  125. expect(mockApprove).not.toHaveBeenCalled();
  126. });
  127. it('non-admin can not approve or deny invite request', function () {
  128. render(
  129. <InviteRequestRow
  130. organization={orgWithoutAdminAccess}
  131. inviteRequest={inviteRequest}
  132. inviteRequestBusy={inviteRequestBusy}
  133. onApprove={() => {}}
  134. onDeny={() => {}}
  135. onUpdate={() => {}}
  136. allRoles={roles}
  137. />
  138. );
  139. expect(screen.getByRole('button', {name: 'Approve'})).toBeDisabled();
  140. expect(screen.getByRole('button', {name: 'Deny'})).toBeDisabled();
  141. });
  142. it('admin can change role and teams', async function () {
  143. const adminInviteRequest = TestStubs.Member({
  144. user: null,
  145. inviterName: TestStubs.User().name,
  146. inviterId: TestStubs.User().id,
  147. inviteStatus: 'requested_to_be_invited',
  148. role: 'admin',
  149. teams: ['myteam'],
  150. });
  151. void TeamStore.loadInitialData([
  152. TestStubs.Team({id: '1', slug: 'one'}),
  153. TestStubs.Team({id: '2', slug: 'two'}),
  154. ]);
  155. const mockUpdate = jest.fn();
  156. render(
  157. <InviteRequestRow
  158. organization={orgWithAdminAccess}
  159. inviteRequest={adminInviteRequest}
  160. inviteRequestBusy={inviteRequestBusy}
  161. onApprove={() => {}}
  162. onDeny={() => {}}
  163. onUpdate={mockUpdate}
  164. allRoles={roles}
  165. />
  166. );
  167. // Select role from first select input
  168. await selectEvent.select(screen.getAllByRole('textbox')[0], 'Member');
  169. expect(mockUpdate).toHaveBeenCalledWith({role: 'member'});
  170. // Select teams from first select input
  171. await selectEvent.select(screen.getAllByRole('textbox')[1], ['#one']);
  172. expect(mockUpdate).toHaveBeenCalledWith({teams: ['one']});
  173. TeamStore.reset();
  174. });
  175. it('cannot be approved when invitee role is not allowed', function () {
  176. const ownerInviteRequest = TestStubs.Member({
  177. user: null,
  178. inviterName: TestStubs.User().name,
  179. inviterId: TestStubs.User().id,
  180. inviteStatus: 'requested_to_be_invited',
  181. role: 'owner',
  182. teams: ['myteam'],
  183. });
  184. const mockUpdate = jest.fn();
  185. render(
  186. <InviteRequestRow
  187. organization={orgWithoutAdminAccess}
  188. inviteRequest={ownerInviteRequest}
  189. inviteRequestBusy={inviteRequestBusy}
  190. onApprove={() => {}}
  191. onDeny={() => {}}
  192. onUpdate={mockUpdate}
  193. allRoles={roles}
  194. />
  195. );
  196. expect(screen.getByRole('button', {name: 'Approve'})).toBeDisabled();
  197. });
  198. });