Skip to content

Commit

Permalink
Updated the calender to not accomodate future dates (#2865)
Browse files Browse the repository at this point in the history
* Updated the calender to not accomodate future dates

* Added test for prior calender update

* Updated handlechange to contron date selection

* Used dynamic futer date in testing instead of a static one
  • Loading branch information
arpit-chakraborty authored Dec 26, 2024
1 parent c1d2e5f commit 74305b0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/screens/UserPortal/Settings/Settings.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,36 @@ describe('Testing Settings Screen [User Portal]', () => {
});
});
});

it('prevents selecting future dates for birth date', async () => {
await act(async () => {
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Settings />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);
});

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);
});
14 changes: 14 additions & 0 deletions src/screens/UserPortal/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]}
/>
</Col>
</Row>
Expand Down

0 comments on commit 74305b0

Please sign in to comment.