diff --git a/src/screens/UserPortal/Settings/Settings.spec.tsx b/src/screens/UserPortal/Settings/Settings.spec.tsx index 39d853862f..184789ab04 100644 --- a/src/screens/UserPortal/Settings/Settings.spec.tsx +++ b/src/screens/UserPortal/Settings/Settings.spec.tsx @@ -410,3 +410,36 @@ describe('Testing Settings Screen [User Portal]', () => { }); }); }); + +it('prevents selecting future dates for birth date', async () => { + await act(async () => { + render( + + + + + + + + + , + ); + }); + + const birthDateInput = screen.getByLabelText( + 'Birth Date', + ) as HTMLInputElement; + const today = new Date().toISOString().split('T')[0]; + const futureDate = new Date(); + futureDate.setFullYear(futureDate.getFullYear() + 100); + const futureDateString = futureDate.toISOString().split('T')[0]; + + // Trying future date + fireEvent.change(birthDateInput, { target: { value: futureDateString } }); + // Checking if value is not updated to future date + expect(birthDateInput.value).not.toBe(futureDateString); + + // Checking if value set correctly + fireEvent.change(birthDateInput, { target: { value: today } }); + expect(birthDateInput.value).toBe(today); +}); diff --git a/src/screens/UserPortal/Settings/Settings.tsx b/src/screens/UserPortal/Settings/Settings.tsx index cc222468fc..385c3d639e 100644 --- a/src/screens/UserPortal/Settings/Settings.tsx +++ b/src/screens/UserPortal/Settings/Settings.tsx @@ -126,6 +126,19 @@ export default function settings(): JSX.Element { * @param value - The new value for the field. */ const handleFieldChange = (fieldName: string, value: string): void => { + // If the field is 'birthDate', validate the date + if (fieldName === 'birthDate') { + const today = new Date(); + const selectedDate = new Date(value); + + // Prevent updating the state if the selected date is in the future + if (selectedDate > today) { + console.error('Future dates are not allowed for the birth date.'); + return; // Exit without updating the state + } + } + + // Update state if the value passes validation setisUpdated(true); setUserDetails((prevState) => ({ ...prevState, @@ -449,6 +462,7 @@ export default function settings(): JSX.Element { handleFieldChange('birthDate', e.target.value) } className={`${styles.cardControl}`} + max={new Date().toISOString().split('T')[0]} />