From d3d9f45224a9516ed3fab781d8d7c59cff414296 Mon Sep 17 00:00:00 2001 From: Arpit Chakraborty Date: Wed, 25 Dec 2024 17:19:00 +0530 Subject: [PATCH 1/4] Updated the calender to not accomodate future dates --- src/screens/UserPortal/Settings/Settings.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/screens/UserPortal/Settings/Settings.tsx b/src/screens/UserPortal/Settings/Settings.tsx index cc222468fc..2c382d412b 100644 --- a/src/screens/UserPortal/Settings/Settings.tsx +++ b/src/screens/UserPortal/Settings/Settings.tsx @@ -449,6 +449,7 @@ export default function settings(): JSX.Element { handleFieldChange('birthDate', e.target.value) } className={`${styles.cardControl}`} + max={new Date().toISOString().split('T')[0]} /> From 5ab9418682da2de1d2f3e97971a3f130baaeeda6 Mon Sep 17 00:00:00 2001 From: Arpit Chakraborty Date: Thu, 26 Dec 2024 11:48:05 +0530 Subject: [PATCH 2/4] Added test for prior calender update --- .../UserPortal/Settings/Settings.spec.tsx | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/screens/UserPortal/Settings/Settings.spec.tsx b/src/screens/UserPortal/Settings/Settings.spec.tsx index 39d853862f..f04902c5ad 100644 --- a/src/screens/UserPortal/Settings/Settings.spec.tsx +++ b/src/screens/UserPortal/Settings/Settings.spec.tsx @@ -410,3 +410,34 @@ 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]; + + // Trying future date + fireEvent.change(birthDateInput, { target: { value: '2100-01-01' } }); + + // Checking if value is not updated to future date + expect(birthDateInput.value).not.toBe('2100-01-01'); + + // Checking if value set correctly + fireEvent.change(birthDateInput, { target: { value: today } }); + expect(birthDateInput.value).toBe(today); +}); From 4a8a367b50e2a0e9646f9b6b3febc8db27667b7a Mon Sep 17 00:00:00 2001 From: Arpit Chakraborty Date: Thu, 26 Dec 2024 15:22:54 +0530 Subject: [PATCH 3/4] Updated handlechange to contron date selection --- src/screens/UserPortal/Settings/Settings.tsx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/screens/UserPortal/Settings/Settings.tsx b/src/screens/UserPortal/Settings/Settings.tsx index 2c382d412b..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, From a13ae9755e3073dfc37d1b874d94a0d35d610a24 Mon Sep 17 00:00:00 2001 From: Arpit Chakraborty Date: Thu, 26 Dec 2024 20:14:23 +0530 Subject: [PATCH 4/4] Used dynamic futer date in testing instead of a static one --- src/screens/UserPortal/Settings/Settings.spec.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/screens/UserPortal/Settings/Settings.spec.tsx b/src/screens/UserPortal/Settings/Settings.spec.tsx index f04902c5ad..184789ab04 100644 --- a/src/screens/UserPortal/Settings/Settings.spec.tsx +++ b/src/screens/UserPortal/Settings/Settings.spec.tsx @@ -430,12 +430,14 @@ it('prevents selecting future dates for birth date', async () => { '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: '2100-01-01' } }); - + fireEvent.change(birthDateInput, { target: { value: futureDateString } }); // Checking if value is not updated to future date - expect(birthDateInput.value).not.toBe('2100-01-01'); + expect(birthDateInput.value).not.toBe(futureDateString); // Checking if value set correctly fireEvent.change(birthDateInput, { target: { value: today } });