inviteRequestRow.spec.jsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(screen.getByText(inviteRequest.inviterName)).toBeInTheDocument();
  65. });
  66. it('renders request to join', function () {
  67. render(
  68. <InviteRequestRow
  69. orgId={orgId}
  70. organization={orgWithoutAdminAccess}
  71. inviteRequest={joinRequest}
  72. inviteRequestBusy={inviteRequestBusy}
  73. allRoles={roles}
  74. />
  75. );
  76. expect(screen.getByText(joinRequest.email)).toBeInTheDocument();
  77. expect(screen.getByRole('button', {name: 'Approve'})).toBeInTheDocument();
  78. expect(screen.getByRole('button', {name: 'Deny'})).toBeInTheDocument();
  79. });
  80. it('admin can approve invite request', function () {
  81. const mockApprove = jest.fn();
  82. const mockDeny = jest.fn();
  83. render(
  84. <InviteRequestRow
  85. orgId={orgId}
  86. organization={orgWithAdminAccess}
  87. inviteRequest={inviteRequest}
  88. inviteRequestBusy={inviteRequestBusy}
  89. onApprove={mockApprove}
  90. onDeny={mockDeny}
  91. allRoles={roles}
  92. />
  93. );
  94. userEvent.click(screen.getByRole('button', {name: 'Approve'}));
  95. renderGlobalModal();
  96. userEvent.click(screen.getByTestId('confirm-button'));
  97. expect(mockApprove).toHaveBeenCalledWith(inviteRequest);
  98. expect(mockDeny).not.toHaveBeenCalled();
  99. });
  100. it('admin can deny invite request', function () {
  101. const mockApprove = jest.fn();
  102. const mockDeny = jest.fn();
  103. render(
  104. <InviteRequestRow
  105. orgId={orgId}
  106. organization={orgWithAdminAccess}
  107. inviteRequest={inviteRequest}
  108. inviteRequestBusy={inviteRequestBusy}
  109. onApprove={mockApprove}
  110. onDeny={mockDeny}
  111. allRoles={roles}
  112. />
  113. );
  114. userEvent.click(screen.getByRole('button', {name: 'Deny'}));
  115. expect(mockDeny).toHaveBeenCalledWith(inviteRequest);
  116. expect(mockApprove).not.toHaveBeenCalled();
  117. });
  118. it('non-admin can not approve or deny invite request', function () {
  119. render(
  120. <InviteRequestRow
  121. orgId={orgId}
  122. organization={orgWithoutAdminAccess}
  123. inviteRequest={inviteRequest}
  124. inviteRequestBusy={inviteRequestBusy}
  125. onApprove={() => {}}
  126. onDeny={() => {}}
  127. allRoles={roles}
  128. />
  129. );
  130. expect(screen.getByRole('button', {name: 'Approve'})).toBeDisabled();
  131. expect(screen.getByRole('button', {name: 'Deny'})).toBeDisabled();
  132. });
  133. it('admin can change role and teams', async function () {
  134. const adminInviteRequest = TestStubs.Member({
  135. user: null,
  136. inviterName: TestStubs.User().name,
  137. inviterId: TestStubs.User().id,
  138. inviteStatus: 'requested_to_be_invited',
  139. role: 'admin',
  140. teams: ['myteam'],
  141. });
  142. void TeamStore.loadInitialData([
  143. {id: '1', slug: 'one'},
  144. {id: '2', slug: 'two'},
  145. ]);
  146. const mockUpdate = jest.fn();
  147. render(
  148. <InviteRequestRow
  149. orgId={orgId}
  150. organization={orgWithAdminAccess}
  151. inviteRequest={adminInviteRequest}
  152. inviteRequestBusy={inviteRequestBusy}
  153. allRoles={roles}
  154. onUpdate={mockUpdate}
  155. />
  156. );
  157. // Select role from first select input
  158. await selectEvent.select(screen.getAllByRole('textbox')[0], 'Member');
  159. expect(mockUpdate).toHaveBeenCalledWith({role: 'member'});
  160. // Select teams from first select input
  161. await selectEvent.select(screen.getAllByRole('textbox')[1], ['#one']);
  162. expect(mockUpdate).toHaveBeenCalledWith({teams: ['one']});
  163. TeamStore.reset();
  164. });
  165. it('cannot be approved when invitee role is not allowed', function () {
  166. const ownerInviteRequest = TestStubs.Member({
  167. user: null,
  168. inviterName: TestStubs.User().name,
  169. inviterId: TestStubs.User().id,
  170. inviteStatus: 'requested_to_be_invited',
  171. role: 'owner',
  172. teams: ['myteam'],
  173. });
  174. const mockUpdate = jest.fn();
  175. render(
  176. <InviteRequestRow
  177. orgId={orgId}
  178. organization={orgWithoutAdminAccess}
  179. inviteRequest={ownerInviteRequest}
  180. inviteRequestBusy={inviteRequestBusy}
  181. allRoles={roles}
  182. onUpdate={mockUpdate}
  183. />
  184. );
  185. expect(screen.getByRole('button', {name: 'Approve'})).toBeDisabled();
  186. });
  187. });