inviteMembersModal.spec.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import InviteMembersModal from 'sentry/components/modals/inviteMembersModal';
  3. import TeamStore from 'sentry/stores/teamStore';
  4. describe('InviteMembersModal', function () {
  5. const team = TestStubs.Team();
  6. const org = TestStubs.Organization({access: ['member:write'], teams: [team]});
  7. TeamStore.loadInitialData([team]);
  8. const modalProps = {
  9. Body: p => p.children,
  10. Header: p => p.children,
  11. Footer: p => p.children,
  12. };
  13. const noWriteOrg = TestStubs.Organization({
  14. access: [],
  15. });
  16. const roles = [
  17. {
  18. id: 'admin',
  19. name: 'Admin',
  20. desc: 'This is the admin role',
  21. allowed: true,
  22. },
  23. {
  24. id: 'member',
  25. name: 'Member',
  26. desc: 'This is the member role',
  27. allowed: true,
  28. },
  29. ];
  30. MockApiClient.addMockResponse({
  31. url: `/organizations/${org.slug}/members/me/`,
  32. method: 'GET',
  33. body: {roles},
  34. });
  35. it('renders', function () {
  36. const wrapper = mountWithTheme(
  37. <InviteMembersModal {...modalProps} organization={org} />
  38. );
  39. // Starts with one invite row
  40. expect(wrapper.find('StyledInviteRow')).toHaveLength(1);
  41. // We have two roles loaded from the members/me endpoint, defaulting to the
  42. // 'member' role.
  43. expect(wrapper.find('RoleSelectControl').props().roles).toHaveLength(roles.length);
  44. expect(wrapper.find('RoleSelectControl SingleValue').first().text()).toBe('Member');
  45. });
  46. it('renders without organization.access', function () {
  47. const organization = TestStubs.Organization({access: undefined});
  48. const wrapper = mountWithTheme(
  49. <InviteMembersModal {...modalProps} organization={organization} />
  50. );
  51. expect(wrapper.find('StyledInviteRow').exists()).toBe(true);
  52. });
  53. it('can add a second row', function () {
  54. const wrapper = mountWithTheme(
  55. <InviteMembersModal {...modalProps} organization={org} />
  56. );
  57. expect(wrapper.find('StyledInviteRow')).toHaveLength(1);
  58. wrapper.find('AddButton').simulate('click');
  59. expect(wrapper.find('StyledInviteRow')).toHaveLength(2);
  60. });
  61. it('errors on duplicate emails', function () {
  62. const wrapper = mountWithTheme(
  63. <InviteMembersModal {...modalProps} organization={org} />
  64. );
  65. wrapper.find('AddButton').simulate('click');
  66. expect(wrapper.find('StyledInviteRow')).toHaveLength(2);
  67. const rows = wrapper.find('StyledInviteRow');
  68. rows
  69. .at(0)
  70. .props()
  71. .onChangeEmails([{value: 'test@test.com'}]);
  72. rows
  73. .at(1)
  74. .props()
  75. .onChangeEmails([{value: 'test@test.com'}]);
  76. wrapper.update();
  77. expect(wrapper.find('StatusMessage[status="error"]').text()).toBe(
  78. 'Duplicate emails between invite rows.'
  79. );
  80. });
  81. it('indicates the total invites on the invite button', function () {
  82. const wrapper = mountWithTheme(
  83. <InviteMembersModal {...modalProps} organization={org} />
  84. );
  85. wrapper
  86. .find('StyledInviteRow')
  87. .first()
  88. .props()
  89. .onChangeEmails([{value: 'test1@test.com'}, {value: 'test2@test.com'}]);
  90. wrapper.update();
  91. expect(wrapper.find('Button[data-test-id="send-invites"]').text()).toBe(
  92. 'Send invites (2)'
  93. );
  94. });
  95. it('can be closed', function () {
  96. const close = jest.fn();
  97. const wrapper = mountWithTheme(
  98. <InviteMembersModal {...modalProps} organization={org} closeModal={close} />
  99. );
  100. wrapper.find('Button[data-test-id="cancel"]').simulate('click');
  101. expect(close).toHaveBeenCalled();
  102. });
  103. it('sends all successful invites', async function () {
  104. const createMemberMock = MockApiClient.addMockResponse({
  105. url: `/organizations/${org.slug}/members/`,
  106. method: 'POST',
  107. });
  108. const wrapper = mountWithTheme(
  109. <InviteMembersModal {...modalProps} organization={org} />
  110. );
  111. wrapper.find('AddButton').simulate('click');
  112. // Setup two rows, one email each, the first with a admin role.
  113. const inviteRowProps = wrapper.find('StyledInviteRow').first().props();
  114. inviteRowProps.onChangeEmails([{value: 'test1@test.com'}]);
  115. inviteRowProps.onChangeRole({value: 'admin'});
  116. inviteRowProps.onChangeTeams([{value: 'team1'}]);
  117. wrapper
  118. .find('StyledInviteRow')
  119. .at(1)
  120. .props()
  121. .onChangeEmails([{value: 'test2@test.com'}]);
  122. wrapper.update();
  123. wrapper.find('FooterContent Button[priority="primary"]').simulate('click');
  124. // Verify data sent to the backend
  125. expect(createMemberMock).toHaveBeenCalledTimes(2);
  126. expect(createMemberMock).toHaveBeenNthCalledWith(
  127. 1,
  128. `/organizations/${org.slug}/members/`,
  129. expect.objectContaining({
  130. data: {email: 'test1@test.com', role: 'admin', teams: ['team1']},
  131. })
  132. );
  133. expect(createMemberMock).toHaveBeenNthCalledWith(
  134. 2,
  135. `/organizations/${org.slug}/members/`,
  136. expect.objectContaining({
  137. data: {email: 'test2@test.com', role: 'member', teams: []},
  138. })
  139. );
  140. // Pending invites being created..
  141. expect(
  142. wrapper.find('InviteRowControl SelectControl EmailLabel LoadingIndicator')
  143. ).toHaveLength(2);
  144. expect(wrapper.find('Button[data-test-id="cancel"][disabled]').exists()).toBe(true);
  145. expect(wrapper.find('Button[data-test-id="send-invites"][disabled]').exists()).toBe(
  146. true
  147. );
  148. expect(wrapper.find('StatusMessage LoadingIndicator').exists()).toBe(true);
  149. // Await request completion
  150. await tick();
  151. wrapper.update();
  152. expect(wrapper.find('StatusMessage').text()).toBe('Sent 2 invites');
  153. expect(wrapper.find('Button[data-test-id="close"]').exists()).toBe(true);
  154. expect(wrapper.find('Button[data-test-id="send-more"]').exists()).toBe(true);
  155. expect(wrapper.find('SelectControl EmailLabel IconCheckmark').exists()).toBe(true);
  156. // Send more reset the modal
  157. wrapper.find('Button[data-test-id="send-more"]').simulate('click');
  158. expect(wrapper.find('InviteRowControl SelectControl EmailLabel').exists()).toBe(
  159. false
  160. );
  161. });
  162. it('marks failed invites', async function () {
  163. const faildCreateMemberMock = MockApiClient.addMockResponse({
  164. url: `/organizations/${org.slug}/members/`,
  165. method: 'POST',
  166. statusCode: 400,
  167. });
  168. const wrapper = mountWithTheme(
  169. <InviteMembersModal {...modalProps} organization={org} />
  170. );
  171. const inviteRowProps = wrapper.find('StyledInviteRow').first().props();
  172. inviteRowProps.onChangeEmails([{value: 'bademail'}]);
  173. wrapper.update();
  174. wrapper.find('FooterContent Button[priority="primary"]').simulate('click');
  175. expect(faildCreateMemberMock).toHaveBeenCalled();
  176. // Await request completion
  177. await tick();
  178. wrapper.update();
  179. expect(wrapper.find('StatusMessage').text()).toBe(
  180. 'Sent 0 invites, 1 failed to send.'
  181. );
  182. expect(wrapper.find('SelectControl EmailLabel IconWarning').exists()).toBe(true);
  183. });
  184. it('can send initial email', async function () {
  185. const createMemberMock = MockApiClient.addMockResponse({
  186. url: `/organizations/${org.slug}/members/`,
  187. method: 'POST',
  188. });
  189. const initialEmail = 'test@gmail.com';
  190. const initialData = [{emails: new Set([initialEmail])}];
  191. const wrapper = mountWithTheme(
  192. <InviteMembersModal {...modalProps} organization={org} initialData={initialData} />
  193. );
  194. expect(wrapper.find('MultiValue').first().text().includes(initialEmail)).toBe(true);
  195. wrapper.find('FooterContent Button[priority="primary"]').simulate('click');
  196. await tick();
  197. wrapper.update();
  198. expect(createMemberMock).toHaveBeenCalledWith(
  199. `/organizations/${org.slug}/members/`,
  200. expect.objectContaining({
  201. data: {email: initialEmail, role: 'member', teams: []},
  202. })
  203. );
  204. expect(wrapper.find('StatusMessage').text()).toBe('Sent 1 invite');
  205. });
  206. it('can send initial email with role and team', async function () {
  207. const createMemberMock = MockApiClient.addMockResponse({
  208. url: `/organizations/${org.slug}/members/`,
  209. method: 'POST',
  210. });
  211. const initialEmail = 'test@gmail.com';
  212. const role = 'admin';
  213. const initialData = [
  214. {emails: new Set([initialEmail]), role, teams: new Set([team.slug])},
  215. ];
  216. const wrapper = mountWithTheme(
  217. <InviteMembersModal {...modalProps} organization={org} initialData={initialData} />
  218. );
  219. expect(
  220. wrapper
  221. .find('SelectControl[data-test-id="select-emails"]')
  222. .text()
  223. .includes(initialEmail)
  224. ).toBe(true);
  225. expect(
  226. wrapper.find('SelectControl[data-test-id="select-role"]').text().toLowerCase()
  227. ).toBe(role);
  228. expect(
  229. wrapper
  230. .find('SelectControl[data-test-id="select-teams"]')
  231. .text()
  232. .includes(team.slug)
  233. ).toBe(true);
  234. wrapper.find('FooterContent Button[priority="primary"]').simulate('click');
  235. await tick();
  236. wrapper.update();
  237. expect(createMemberMock).toHaveBeenCalledWith(
  238. `/organizations/${org.slug}/members/`,
  239. expect.objectContaining({
  240. data: {email: initialEmail, role, teams: [team.slug]},
  241. })
  242. );
  243. expect(wrapper.find('StatusMessage').text()).toBe('Sent 1 invite');
  244. });
  245. describe('member invite request mode', function () {
  246. it('has adjusted wording', function () {
  247. const wrapper = mountWithTheme(
  248. <InviteMembersModal {...modalProps} organization={noWriteOrg} />
  249. );
  250. expect(wrapper.find('Button[data-test-id="send-invites"]').text()).toBe(
  251. 'Send invite request'
  252. );
  253. expect(wrapper.find('Heading Tooltip').exists()).toBe(true);
  254. });
  255. it('POSTS to the invite-request endpoint', function () {
  256. const createInviteRequestMock = MockApiClient.addMockResponse({
  257. url: `/organizations/${org.slug}/invite-requests/`,
  258. method: 'POST',
  259. });
  260. const wrapper = mountWithTheme(
  261. <InviteMembersModal {...modalProps} organization={noWriteOrg} />
  262. );
  263. const inviteRowProps = wrapper.find('StyledInviteRow').first().props();
  264. inviteRowProps.onChangeEmails([{value: 'test1@test.com'}]);
  265. inviteRowProps.onChangeRole({value: 'admin'});
  266. inviteRowProps.onChangeTeams([{value: 'team1'}]);
  267. wrapper
  268. .find('StyledInviteRow')
  269. .first()
  270. .props()
  271. .onChangeEmails([{value: 'test2@test.com'}]);
  272. wrapper.update();
  273. wrapper.find('FooterContent Button[priority="primary"]').simulate('click');
  274. expect(createInviteRequestMock).toHaveBeenCalledTimes(1);
  275. });
  276. });
  277. });