Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions __mocks__/forms/rfe-forms/component-art.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
{
"type": "js_expression",
"failsWhenExpression": "(new moment(encDate)).isBefore((new moment(myValue)), 'day') || (new moment(encDate)).isSame((new moment(myValue)), 'day')",
"failsWhenExpression": "dayjs(encDate).isBefore(dayjs(myValue), 'day') || dayjs(encDate).isSame(dayjs(myValue), 'day')",
"message": "Date should be before the encounter date."
}
]
Expand Down Expand Up @@ -114,7 +114,7 @@
},
{
"type": "js_expression",
"failsWhenExpression": "(new moment(encDate)).isBefore((new moment(myValue)), 'day') || (new moment(encDate)).isSame((new moment(myValue)), 'day')",
"failsWhenExpression": "dayjs(encDate).isBefore(dayjs(myValue), 'day') || dayjs(encDate).isSame(dayjs(myValue), 'day')",
"message": "ARV start date should be before the encounter date."
}
]
Expand All @@ -134,7 +134,7 @@
},
{
"type": "js_expression",
"failsWhenExpression": "(new moment(encDate)).isBefore((new moment(myValue)), 'day') || (new moment(encDate)).isSame((new moment(myValue)), 'day')",
"failsWhenExpression": "dayjs(encDate).isBefore(dayjs(myValue), 'day') || dayjs(encDate).isSame(dayjs(myValue), 'day')",
"message": "ARV start date should be before the encounter date."
}
]
Expand All @@ -154,7 +154,7 @@
},
{
"type": "js_expression",
"failsWhenExpression": "(new moment(encDate)).isBefore((new moment(myValue)), 'day') || (new moment(encDate)).isSame((new moment(myValue)), 'day')",
"failsWhenExpression": "dayjs(encDate).isBefore(dayjs(myValue), 'day') || dayjs(encDate).isSame(dayjs(myValue), 'day')",
"message": "ARV start date should be before the encounter date."
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useInitialValues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('useInitialValues', () => {

const { result } = renderHook(() => useInitialValues(formProcessor, false, context));

await waitFor(() => expect(result.current.isLoadingInitialValues).toBe(true));
await waitFor(() => expect(result.current.isLoadingInitialValues).toBe(false));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What changed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the initial state from true to false

This waitFor assertion waits for isLoadingInitialValues to be true but that was already the initial state. It's already true immediately so this was passing and moving on to the subsequent assertions before the async getInitialValues promise has resolved


expect(formProcessor.getInitialValues).toHaveBeenCalledWith(context);
expect(result.current.initialValues).toEqual(mockInitialValues);
Expand Down
22 changes: 19 additions & 3 deletions src/utils/common-expression-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class CommonExpressionHelpers {
allFieldValues: Record<string, any> = {};
api = apiFunctions;
isEmpty = isValueEmpty;
dayjs = dayjs;

constructor(node: FormNode, patient: any, allFields: FormField[], allFieldValues: Record<string, any>) {
this.allFields = allFields;
Expand Down Expand Up @@ -101,7 +102,12 @@ export class CommonExpressionHelpers {
* @returns true if left is before right
*/
isDateBefore = (left: Date, right: string | Date, format?: string): boolean => {
const otherDate: Date = right instanceof Date ? right : (format ? dayjs(right, format, true).toDate() : dayjs(right, 'YYYY-MM-DD', true).toDate());
const otherDate: Date =
right instanceof Date
? right
: format
? dayjs(right, format, true).toDate()
: dayjs(right, 'YYYY-MM-DD', true).toDate();
return left?.getTime() < otherDate.getTime();
};

Expand All @@ -113,7 +119,12 @@ export class CommonExpressionHelpers {
* @param timePeriod - The time unit: 'days', 'weeks', 'months', or 'years'
* @returns true if selectedDate >= (baseDate + duration)
*/
isDateAfter = (selectedDate: Date, baseDate: Date, duration: number, timePeriod: 'days' | 'weeks' | 'months' | 'years'): boolean => {
isDateAfter = (
selectedDate: Date,
baseDate: Date,
duration: number,
timePeriod: 'days' | 'weeks' | 'months' | 'years',
): boolean => {
const parsedBaseDate = dayjs(baseDate);

let calculatedDate: Date;
Expand Down Expand Up @@ -165,7 +176,12 @@ export class CommonExpressionHelpers {
* @returns true if left is after right
*/
isDateAfterSimple = (left: Date, right: string | Date, format?: string): boolean => {
const otherDate: Date = right instanceof Date ? right : (format ? dayjs(right, format, true).toDate() : dayjs(right, 'YYYY-MM-DD', true).toDate());
const otherDate: Date =
right instanceof Date
? right
: format
? dayjs(right, format, true).toDate()
: dayjs(right, 'YYYY-MM-DD', true).toDate();
return left?.getTime() > otherDate.getTime();
};

Expand Down
43 changes: 43 additions & 0 deletions src/utils/expression-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ export const testFields: Array<FormField> = [
},
id: 'bodyTemperature',
},
{
label: 'Date of Birth',
type: 'obs',
questionOptions: {
rendering: 'date',
concept: 'date_of_birth_concept',
},
id: 'dateOfBirth',
},
{
label: 'Age in Days',
type: 'obs',
questionOptions: {
rendering: 'number',
concept: 'age_in_days_concept',
},
id: 'ageInDays',
},
];

export const fields: Array<FormField> = [
Expand Down Expand Up @@ -180,6 +198,8 @@ describe('Expression runner', () => {
htsProviderRemarks: '',
referredToPreventionServices: [],
bodyTemperature: 0,
dateOfBirth: '',
ageInDays: 0,
no_interest: '',
depressed: '',
bad_sleep: '',
Expand All @@ -199,6 +219,8 @@ describe('Expression runner', () => {
htsProviderRemarks: '',
referredToPreventionServices: [],
bodyTemperature: 0,
dateOfBirth: '',
ageInDays: 0,
no_interest: '',
depressed: '',
bad_sleep: '',
Expand Down Expand Up @@ -360,4 +382,25 @@ describe('Expression runner', () => {
);
expect(result).toEqual(5);
});

it('should calculate age in days using dayjs diff expression', () => {
// setup - use a known date 30 days ago
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const dateOfBirthString = thirtyDaysAgo.toLocaleDateString('en-CA'); // en-CA produces YYYY-MM-DD in local time
valuesMap['dateOfBirth'] = dateOfBirthString;

// Calculate days difference
const result = evaluateExpression(
"dayjs().diff(dayjs(dateOfBirth), 'day')",
{ value: allFields[6], type: 'field' },
allFields,
valuesMap,
context,
);

// Should be approximately 30 days (allowing for time-of-day variations)
expect(result).toBeGreaterThanOrEqual(29);
expect(result).toBeLessThanOrEqual(31);
});
});