Skip to content

Commit

Permalink
Selecting a date should set local time hours to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
vasharma05 committed Apr 22, 2024
1 parent 6c31a53 commit 099cdd9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/components/inputs/date/ohri-date.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const OHRIDate: React.FC<OHRIFormFieldProps> = ({ question, onChange, handler, p
}, [encounterContext.sessionMode, question.readonly, question.inlineRendering, layoutType, workspaceLayout]);

const onDateChange = ([date]) => {
const refinedDate = date instanceof Date ? new Date(date.getTime() - date.getTimezoneOffset() * 60000) : date;
const refinedDate = date instanceof Date ? new Date(date.setHours(0, 0, 0, 0)) : date;
setFieldValue(question.id, refinedDate);
onChange(question.id, refinedDate, setErrors, setWarnings);
onTimeChange(false, true);
Expand All @@ -49,7 +49,7 @@ const OHRIDate: React.FC<OHRIFormFieldProps> = ({ question, onChange, handler, p
useEffect(() => {
if (!isEmpty(previousValue)) {
const date = previousValue.value;
const refinedDate = date instanceof Date ? new Date(date.getTime() - date.getTimezoneOffset() * 60000) : date;
const refinedDate = date instanceof Date ? new Date(date.setHours(0, 0, 0, 0)) : date;
setFieldValue(question.id, refinedDate);
onChange(question.id, refinedDate, setErrors, setWarnings);
onTimeChange(false, true);
Expand Down
12 changes: 6 additions & 6 deletions src/ohri-form.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import labour_and_delivery_test_form from '../__mocks__/forms/ohri-forms/labour_
import sample_fields_form from '../__mocks__/forms/ohri-forms/sample_fields.json';
import postSubmission_test_form from '../__mocks__/forms/ohri-forms/post-submission-test-form.json';
import { evaluatePostSubmissionExpression } from './utils/post-submission-action-helper';
import dayjs from 'dayjs';

import {
assertFormHasAllFields,
Expand Down Expand Up @@ -204,7 +205,8 @@ describe('OHRI Forms:', () => {
const generalPopulationField = await findRadioGroupMember(screen, 'General population');

// Simulate user interaction
fireEvent.blur(enrolmentDateField, { target: { value: '2023-09-09T00:00:00.000Z' } });
// console.log('Checking', .toDate());
fireEvent.blur(enrolmentDateField, { target: { value: dayjs('2023-09-09') } });
fireEvent.blur(uniqueIdField, { target: { value: 'U0-001109' } });
fireEvent.click(motherEnrolledField);
fireEvent.click(generalPopulationField);
Expand Down Expand Up @@ -440,8 +442,8 @@ describe('OHRI Forms:', () => {
fireEvent.change(lmpField, { target: { value: '2022-07-06' } });

// verify
await act(async () => expect(lmpField.value).toBe(new Date('2022-07-06').toLocaleDateString(locale)));
await act(async () => expect(eddField.value).toBe(new Date('2023-04-12').toLocaleDateString(locale)));
await act(async () => expect(lmpField.value).toBe('06/07/2022'));
await act(async () => expect(eddField.value).toBe('12/04/2023'));
});

it('Should evaluate months on ART', async () => {
Expand All @@ -465,9 +467,7 @@ describe('OHRI Forms:', () => {
fireEvent.blur(artStartDateField, { target: { value: '05/02/2022' } });

// verify
await act(async () =>
expect(artStartDateField.value).toBe(new Date('2022-02-05T00:00:00.000+0000').toLocaleDateString(locale)),
);
await act(async () => expect(artStartDateField.value).toBe('05/02/2022'));
await act(async () => expect(assumeTodayToBe).toBe('7/11/2022'));
await act(async () => expect(monthsOnARTField.value).toBe('7'));
});
Expand Down

0 comments on commit 099cdd9

Please sign in to comment.