From 67f2e0f1748d564e406bc052dc85507742dcd645 Mon Sep 17 00:00:00 2001 From: mark-prins Date: Tue, 31 Dec 2024 13:46:13 +1300 Subject: [PATCH] fix date test --- client/packages/common/src/intl/utils/DateUtils.test.ts | 6 +++++- client/packages/common/src/intl/utils/DateUtils.ts | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/client/packages/common/src/intl/utils/DateUtils.test.ts b/client/packages/common/src/intl/utils/DateUtils.test.ts index ce26247ac2..2ce8f2639e 100644 --- a/client/packages/common/src/intl/utils/DateUtils.test.ts +++ b/client/packages/common/src/intl/utils/DateUtils.test.ts @@ -18,6 +18,7 @@ describe('getDisplayAge', () => { const hookResult = renderHookWithProvider(() => useFormatDateTime()); const { getDisplayAge } = hookResult.result.current; const today = new Date(); + it('returns age in years when patient is over 1 year or 1 year old', () => { const dob = DateUtils.addYears(today, -9); const result = getDisplayAge(dob); @@ -26,9 +27,12 @@ describe('getDisplayAge', () => { it('returns age in months and days when patient is less than 1 year old', () => { const threeMonthsAgo = DateUtils.addMonths(today, -3); + const dayOffset = + DateUtils.getDaysInMonth(today) - + DateUtils.getDaysInMonth(threeMonthsAgo); const dob = DateUtils.addDays(threeMonthsAgo, -2); const result = getDisplayAge(dob); - expect(result).toBe('3 months, 2 days'); + expect(result).toBe(`3 months, ${2 + dayOffset} days`); }); it('returns age in days when patient is less than 1 month old', () => { diff --git a/client/packages/common/src/intl/utils/DateUtils.ts b/client/packages/common/src/intl/utils/DateUtils.ts index ff47c42a29..36c3d44415 100644 --- a/client/packages/common/src/intl/utils/DateUtils.ts +++ b/client/packages/common/src/intl/utils/DateUtils.ts @@ -10,6 +10,7 @@ import { differenceInMonths, differenceInMinutes, differenceInYears, + getDaysInMonth, isPast, isFuture, isThisWeek, @@ -104,6 +105,7 @@ export const DateUtils = { : new Date(date); return isValid(maybeDate) ? maybeDate : null; }, + getDaysInMonth, /** * While getDateOrNull is naive to the timezone, the timezone will still * change. When converting from the assumed naive zone of GMT to the local