diff --git a/src/hooks/useInitialValues.test.ts b/src/hooks/useInitialValues.test.ts index cf61d3c6..6b3d93e2 100644 --- a/src/hooks/useInitialValues.test.ts +++ b/src/hooks/useInitialValues.test.ts @@ -115,12 +115,11 @@ jest.mock('../utils/expression-runner', () => { }; }); -describe('useInialValues', () => { +describe('useInitialValues', () => { const encounterDate = new Date(); afterEach(() => { - allFormFields = allFormFields.slice(0, 6); - allFormFields.forEach(field => { + allFormFields.slice(0, 6).forEach(field => { delete field.value; }); }); @@ -182,10 +181,10 @@ describe('useInialValues', () => { notes: 'Mother is in perfect condition', screening_methods: [], // child one - date_of_birth: '7/24/2023', + date_of_birth: new Date('2023-07-24T00:00:00.000+0000').toLocaleDateString('en-US'), infant_name: 'TBD', // child two - date_of_birth_1: '7/24/2023', + date_of_birth_1: new Date('2023-07-24T00:00:00.000+0000').toLocaleDateString('en-US'), infant_name_1: ' TDB II', }); expect(allFormFields.find(field => field.id === 'date_of_birth_1')).not.toBeNull(); @@ -231,7 +230,9 @@ describe('useInialValues', () => { notes: '', screening_methods: [], date_of_birth: '', + date_of_birth_1: '', infant_name: '', + infant_name_1: '', latest_mother_hiv_status: '664AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', }); }); diff --git a/src/ohri-form.component.test.tsx b/src/ohri-form.component.test.tsx index e14ede63..7ef12d66 100644 --- a/src/ohri-form.component.test.tsx +++ b/src/ohri-form.component.test.tsx @@ -221,8 +221,8 @@ describe('OHRI Forms:', () => { fireEvent.change(lmpField, { target: { value: '2022-07-06' } }); // verify - await act(async () => expect(lmpField.value).toBe('7/6/2022')); - await act(async () => expect(eddField.value).toBe('4/12/2023')); + await act(async () => expect(lmpField.value).toBe(new Date('2022-07-06').toLocaleDateString('en-US'))); + await act(async () => expect(eddField.value).toBe(new Date('2023-04-12').toLocaleDateString('en-US'))); }); it('Should evaluate months on ART', async () => { @@ -244,7 +244,7 @@ describe('OHRI Forms:', () => { fireEvent.blur(artStartDateField, { target: { value: '05/02/2022' } }); // verify - await act(async () => expect(artStartDateField.value).toBe('5/2/2022')); + await act(async () => expect(artStartDateField.value).toBe(new Date('2022-05-02T00:00:00.000+0000').toLocaleDateString('en-US'))); await act(async () => expect(assumeTodayToBe).toBe('7/11/2022')); await act(async () => expect(monthsOnARTField.value).toBe('5')); }); @@ -283,7 +283,7 @@ describe('OHRI Forms:', () => { await act(async () => expect(mrn.value).toBe('')); // verify - await act(async () => expect(enrollmentDate.value).toBe('7/6/1975')); + await act(async () => expect(enrollmentDate.value).toBe(new Date('1975-07-06T00:00:00.000Z').toLocaleDateString('en-US'))); await act(async () => expect(mrn.value).toBe('')); await act(async () => expect(mrn).toBeVisible()); }); diff --git a/src/utils/common-expression-helpers.ts b/src/utils/common-expression-helpers.ts index 78169f0b..27143caf 100644 --- a/src/utils/common-expression-helpers.ts +++ b/src/utils/common-expression-helpers.ts @@ -1,4 +1,3 @@ -'use '; import dayjs from 'dayjs'; import duration from 'dayjs/plugin/duration'; dayjs.extend(duration); @@ -35,7 +34,7 @@ export class CommonExpressionHelpers { return new Date(); } - includes = (collection: any[], value: any) => { + includes = (collection: T[], value: T) => { return collection?.includes(value); }; @@ -72,7 +71,7 @@ export class CommonExpressionHelpers { return selectedDate.getTime() > calculatedDate.getTime(); }; - addWeeksToDate = (date, weeks) => { + addWeeksToDate = (date: Date, weeks: number) => { date.setDate(date.getDate() + 7 * weeks); return date; @@ -91,12 +90,12 @@ export class CommonExpressionHelpers { return null; }; - calcBMI = (height, weight) => { - let r; + calcBMI = (height: number, weight: number) => { + let r: string; if (height && weight) { r = (weight / (((height / 100) * height) / 100)).toFixed(1); } - return height && weight ? parseFloat(r) : null; + return r ? parseFloat(r) : null; }; /** @@ -104,7 +103,7 @@ export class CommonExpressionHelpers { * @param lmpQuestionId * @returns */ - calcEDD = lmp => { + calcEDD = (lmp: Date) => { let resultEdd = {}; if (lmp) { resultEdd = new Date(lmp.getTime() + 280 * 24 * 60 * 60 * 1000); @@ -112,9 +111,9 @@ export class CommonExpressionHelpers { return lmp ? resultEdd : null; }; - calcMonthsOnART = artStartDate => { + calcMonthsOnART = (artStartDate: Date) => { let today = new Date(); - let resultMonthsOnART; + let resultMonthsOnART: number; let artInDays = Math.round((today.getTime() - artStartDate.getTime?.()) / 86400000); if (artStartDate && artInDays >= 30) { resultMonthsOnART = Math.floor(artInDays / 30); @@ -122,8 +121,8 @@ export class CommonExpressionHelpers { return artStartDate ? resultMonthsOnART : null; }; - calcViralLoadStatus = viralLoadCount => { - let resultViralLoadStatus; + calcViralLoadStatus = (viralLoadCount: number) => { + let resultViralLoadStatus: string; if (viralLoadCount) { if (viralLoadCount > 50) { resultViralLoadStatus = 'a6768be6-c08e-464d-8f53-5f4229508e54'; @@ -131,18 +130,18 @@ export class CommonExpressionHelpers { resultViralLoadStatus = '5d5e42cc-acc4-4069-b3a8-7163e0db5d96'; } } - return viralLoadCount ? resultViralLoadStatus : null; + return resultViralLoadStatus ?? null; }; calcNextVisitDate = (followupDate, arvDispensedInDays) => { - let resultNextVisitDate = {}; + let resultNextVisitDate: Date; if (followupDate && arvDispensedInDays) { resultNextVisitDate = new Date(followupDate.getTime() + arvDispensedInDays * 24 * 60 * 60 * 1000); } - return followupDate && arvDispensedInDays ? resultNextVisitDate : null; + return resultNextVisitDate ?? null; }; - calcTreatmentEndDate = (followupDate, arvDispensedInDays, patientStatus) => { + calcTreatmentEndDate = (followupDate: Date, arvDispensedInDays: number, patientStatus: string) => { let resultTreatmentEndDate = {}; let extraDaysAdded = 30 + arvDispensedInDays; if (followupDate && arvDispensedInDays && patientStatus == '160429AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') { @@ -153,7 +152,7 @@ export class CommonExpressionHelpers { : null; }; - calcAgeBasedOnDate = dateValue => { + calcAgeBasedOnDate = (dateValue?: ConstructorParameters[0] | null) => { let targetYear = null; if (dateValue) { targetYear = new Date(dateValue).getFullYear(); @@ -166,57 +165,69 @@ export class CommonExpressionHelpers { }; //Ampath Helper Functions - calcBSA = (height, weight) => { - let result; + calcBSA = (height: number, weight: number) => { + let result: string; if (height && weight) { result = Math.sqrt((height * weight) / 3600).toFixed(2); } - return height && weight ? parseFloat(result) : null; + return result ? parseFloat(result) : null; }; - arrayContains = (array, members) => { - if (Array.isArray(members)) { - if (members.length === 0) { - return true; - } + arrayContains = (array: T[], members: T[] | T) => { + if (!array || !Array.isArray(array)) { + return false; + } - let contains = true; + if (array.length === 0) { + return members === undefined || members === null || (Array.isArray(members) && members.length === 0); + } - for (let i = 0; i < members.length; i++) { - const val = members[i]; - if (array.indexOf(val) === -1) { - contains = false; - } - } + if (!Array.isArray(members)) { + members = [members]; + } - return contains; - } else { - return array.indexOf(members) !== -1; + if (members.length === 0) { + return true; } - }; - arrayContainsAny = (array, members) => { - if (Array.isArray(members)) { - if (members.length === 0) { - return true; + for (let val of members) { + if (array.indexOf(val) === -1) { + return false; } - let contains = false; + } + + return true; + }; - for (let i = 0; i < members.length; i++) { - const val = members[i]; - if (array.indexOf(val) !== -1) { - contains = true; - } + arrayContainsAny = (array: T[], members: T[]) => { + if (!array || !Array.isArray(array)) { + return false; + } + + if (array.length === 0) { + return members === undefined || members === null || (Array.isArray(members) && members.length === 0); + } + + if (!Array.isArray(members)) { + members = [members]; + } + + if (members.length === 0) { + return true; + } + + for (let val of members) { + if (array.indexOf(val) !== -1) { + return true; } - return contains; - } else { - return array.indexOf(members) !== -1; } + + return false; }; - formatDate = (value, format, offset) => { - format = format || 'yyyy-MM-dd'; - offset = offset || '+0300'; + formatDate = (value: ConstructorParameters[0], format?: string | null, offset?: string | null) => { + format = format ?? 'yyyy-MM-dd'; + offset = offset ?? '+0300'; if (!(value instanceof Date)) { value = new Date(value); @@ -228,7 +239,7 @@ export class CommonExpressionHelpers { return value; }; - extractRepeatingGroupValues = (key, array) => { + extractRepeatingGroupValues = (key: string | number | symbol, array: Record[]) => { const values = array.map(function(item) { return item[key]; }); @@ -249,8 +260,8 @@ export class CommonExpressionHelpers { return gravida; } - calcTimeDifference = (obsDate, timeFrame) => { - let daySinceLastObs; + calcTimeDifference = (obsDate: Date | dayjs.Dayjs, timeFrame: 'd' | 'w' | 'm' | 'y') => { + let daySinceLastObs: number | string = ''; const endDate = dayjs(); if (obsDate) { if (timeFrame == 'd') { @@ -266,7 +277,7 @@ export class CommonExpressionHelpers { daySinceLastObs = Math.abs(Math.round(endDate.diff(obsDate, 'year', true))); } } - return daySinceLastObs == '' ? '0' : daySinceLastObs; + return daySinceLastObs === '' ? '0' : daySinceLastObs; }; }