From abad83b3d73fdb8ff281b32e663978b6f5f32552 Mon Sep 17 00:00:00 2001 From: Devon Date: Mon, 16 Dec 2024 12:06:07 -0500 Subject: [PATCH] [JN-1539] fixing portal select in create user (#1335) --- ui-admin/src/user/CreateUserModal.test.tsx | 40 ++++++++++++++++ ui-admin/src/user/CreateUserModal.tsx | 54 +++++++++++++++------- 2 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 ui-admin/src/user/CreateUserModal.test.tsx diff --git a/ui-admin/src/user/CreateUserModal.test.tsx b/ui-admin/src/user/CreateUserModal.test.tsx new file mode 100644 index 0000000000..45aa72d624 --- /dev/null +++ b/ui-admin/src/user/CreateUserModal.test.tsx @@ -0,0 +1,40 @@ +import React from 'react' +import { mockPortal, renderInPortalRouter } from 'test-utils/mocking-utils' +import { screen } from '@testing-library/react' +import { mockAdminUser } from '../test-utils/user-mocking-utils' +import Api from 'api/api' +import CreateUserModal from './CreateUserModal' +import { userEvent } from '@testing-library/user-event' +import { select } from 'react-select-event' + +describe('CreateUserModal', () => { + test('Portal select functions', async () => { + jest.spyOn(Api, 'fetchRoles').mockResolvedValue([]) + const createSpy = jest.spyOn(Api, 'createPortalUser').mockResolvedValue(mockAdminUser(false)) + const portals = [{ + ...mockPortal(), + shortcode: 'test1', + name: 'P1' + }, { + ...mockPortal(), + shortcode: 'test2', + name: 'P2' + }] + + renderInPortalRouter(portals[1], + ) + await userEvent.type(screen.getByLabelText('Email'), 'foo@bar.com') + await userEvent.click(screen.getByText('Create')) + + expect(createSpy).toHaveBeenCalledWith({ + username: 'foo@bar.com', superuser: false, portalShortcode: 'test1', roleNames: [] + }) + + createSpy.mockClear() + await select(screen.getByLabelText('Portal'), ['P2']) + await userEvent.click(screen.getByText('Create')) + expect(createSpy).toHaveBeenCalledWith({ + username: 'foo@bar.com', superuser: false, portalShortcode: 'test2', roleNames: [] + }) + }) +}) diff --git a/ui-admin/src/user/CreateUserModal.tsx b/ui-admin/src/user/CreateUserModal.tsx index a38913e9ef..fafddd87fc 100644 --- a/ui-admin/src/user/CreateUserModal.tsx +++ b/ui-admin/src/user/CreateUserModal.tsx @@ -9,6 +9,9 @@ import { Store } from 'react-notifications-component' import Select from 'react-select' import { doApiLoad, useLoadingEffect } from '../api/api-utils' import { RoleSelector } from './AdminUserDetail' +import useReactSingleSelect from '../util/react-select-utils' + +const DEFAULT_ROLE = 'study_admin' /** creates a new admin user */ const CreateUserModal = ({ onDismiss, portals, userCreated }: @@ -16,26 +19,26 @@ const CreateUserModal = ({ onDismiss, portals, userCreated }: portals: Portal[], userCreated: () => void }) => { const [isLoading, setIsLoading] = useState(false) - const [portalShortcode, setPortalShortcode] = - useState(portals.length > 0 ? portals[0].shortcode : null) const { user } = useUser() const [newUser, setNewUser] = useState({ username: '', superuser: false, - portalShortcode, - roleNames: ['study_admin'] + portalShortcode: portals.length > 0 ? portals[0].shortcode : null, + roleNames: [] }) - const portalOpts = portals.map(portal => ({ label: portal.name, value: portal.shortcode })) - const selectedPortalOpt = portalOpts.find(portalOpt => portalOpt.value === portalShortcode) const [roles, setRoles] = useState([]) - useLoadingEffect(async () => { + const { isLoading: rolesLoading } = useLoadingEffect(async () => { const fetchedRoles = await Api.fetchRoles() + setNewUser({ + ...newUser, + roleNames: fetchedRoles.filter(role => role.name === DEFAULT_ROLE).map(role => role.name) + }) setRoles(fetchedRoles) }) const createUser = async () => { - doApiLoad(async () => { + await doApiLoad(async () => { let createdUser: AdminUser if (!newUser.superuser) { createdUser = await Api.createPortalUser(newUser) @@ -59,12 +62,12 @@ const CreateUserModal = ({ onDismiss, portals, userCreated }:
e.preventDefault()}>
-
{user?.superuser &&
Superuser
@@ -79,12 +82,13 @@ const CreateUserModal = ({ onDismiss, portals, userCreated }:
} { !newUser.superuser &&
-
-