inviteRequestRow.spec.tsx 6.1 KB

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