inviteMembersModal.spec.jsx 11 KB

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