inviteRequestRow.spec.jsx 5.7 KB

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