diff --git a/frontend/src/routes/guest/register/index.test.tsx b/frontend/src/routes/guest/register/index.test.tsx index 2863233310172efed13a54444400a6a3fc804f0e..c0d41bfac0d8af0744fe8a792eac4565621ef9d7 100644 --- a/frontend/src/routes/guest/register/index.test.tsx +++ b/frontend/src/routes/guest/register/index.test.tsx @@ -3,6 +3,7 @@ import fetchMock, { enableFetchMocks } from 'jest-fetch-mock' import { render, screen } from 'test-utils' import AdapterDateFns from '@mui/lab/AdapterDateFns' import { LocalizationProvider } from '@mui/lab' +import { FeatureContext } from 'contexts' import GuestRegister from './index' enableFetchMocks() @@ -15,8 +16,6 @@ const testData = { private_email: 'test@example.org', fnr: '08015214555', passport: 'DK-123456', - date_of_birth: '1952-01-08', - gender: '', }, role: { ou_name_en: 'English organizational unit name', @@ -32,14 +31,24 @@ const testData = { }, } +const allFeaturesOn = { + displayContactAtUnit: true, + displayComment: true, + displayContactAtUnitGuestInput: true, + showGenderFieldForGuest: true, +} + test('Field showing values correctly', async () => { fetchMock.mockIf('/api/ui/v1/invited/', () => Promise.resolve<any>(JSON.stringify(testData)) ) + render( - <LocalizationProvider dateAdapter={AdapterDateFns}> - <GuestRegister /> - </LocalizationProvider> + <FeatureContext.Provider value={allFeaturesOn}> + <LocalizationProvider dateAdapter={AdapterDateFns}> + <GuestRegister /> + </LocalizationProvider> + </FeatureContext.Provider> ) await screen.findByDisplayValue(testData.person.first_name) @@ -48,13 +57,12 @@ test('Field showing values correctly', async () => { await screen.findByDisplayValue(testData.person.fnr) // Check that suggestions for date of birth and gender are showing - await screen.findByDisplayValue(testData.person.date_of_birth) + await screen.findByDisplayValue('1952-01-08') await screen.findByDisplayValue('male') // Passport nationality. The i18n-mock sets up en as the i18n.language property, so look for the English name await screen.findByText('DK') await screen.findByDisplayValue('123456') - await screen.findByDisplayValue(testData.person.date_of_birth) // Mobile phone country code await screen.findByDisplayValue('NO') @@ -72,18 +80,19 @@ test('Field showing values correctly', async () => { await screen.findByDisplayValue(testData.role.contact_person_unit) }) -test('Gender and birth date suggestions not if no national ID given', async () => { - const existingDateOfBirth = testData.person.date_of_birth - testData.person.fnr = '' - testData.person.date_of_birth = '' - +test('Gender and birth date suggestions not showing if no national ID given', async () => { + // Clear the fnr + // @ts-ignore + testData.person.fnr = null fetchMock.mockIf('/api/ui/v1/invited/', () => Promise.resolve<any>(JSON.stringify(testData)) ) render( - <LocalizationProvider dateAdapter={AdapterDateFns}> - <GuestRegister /> - </LocalizationProvider> + <FeatureContext.Provider value={allFeaturesOn}> + <LocalizationProvider dateAdapter={AdapterDateFns}> + <GuestRegister /> + </LocalizationProvider> + </FeatureContext.Provider> ) // Wait a bit so that all the values are showing @@ -92,31 +101,37 @@ test('Gender and birth date suggestions not if no national ID given', async () = // No national is given in the input data so there should be no // suggestion for the birthdate or gender - const dateOfBirth = screen.queryByDisplayValue(existingDateOfBirth) + const dateOfBirth = screen.queryByDisplayValue('1952-01-08') expect(dateOfBirth).toBeNull() const gender = screen.queryByDisplayValue('male') expect(gender).toBeNull() }) -test('Gender and birth date suggestions not if no national ID given', async () => { +test('Gender and birth date suggestions not overriding existing values', async () => { // Make the date of birth and national ID not match testData.person.fnr = '08015214555' + // @ts-ignore testData.person.date_of_birth = '1960-01-08' // Also set the gender to female to check that it is not overridden by a suggestion + // @ts-ignore testData.person.gender = 'female' fetchMock.mockIf('/api/ui/v1/invited/', () => Promise.resolve<any>(JSON.stringify(testData)) ) render( - <LocalizationProvider dateAdapter={AdapterDateFns}> - <GuestRegister /> - </LocalizationProvider> + <FeatureContext.Provider value={allFeaturesOn}> + <LocalizationProvider dateAdapter={AdapterDateFns}> + <GuestRegister /> + </LocalizationProvider> + </FeatureContext.Provider> ) // In this a date of birth was already set, and it should not have been overridden by a suggestion + // @ts-ignore await screen.findByDisplayValue(testData.person.date_of_birth) + // Check that the gender has not been overridden await screen.findByDisplayValue('female') }) diff --git a/frontend/src/routes/guest/register/steps/register.tsx b/frontend/src/routes/guest/register/steps/register.tsx index 53b058e47722d502ada87b574fee2326a24b8809..efe84febfb08e8d81c5d371393b975d210550004 100644 --- a/frontend/src/routes/guest/register/steps/register.tsx +++ b/frontend/src/routes/guest/register/steps/register.tsx @@ -70,8 +70,11 @@ const GuestRegisterStep = forwardRef( // Set suggestion for gender field is a gender is not already given in the input const [gender, setGender] = useState<string>( - initialGuestData.gender ?? extractGenderOrBlank(initialGuestData.fnr) + !initialGuestData.gender || !initialGuestData.gender.trim() + ? extractGenderOrBlank(initialGuestData.fnr) + : initialGuestData.gender ) + const [idErrorState, setIdErrorState] = useState<string>('') const [phoneErrorState, setPhoneErrorState] = useState<string>('') const { displayContactAtUnitGuestInput } = useContext(FeatureContext)