Skip to content

Commit d4222e5

Browse files
authored
(fix) Improve common-expression-helpers (#672)
1 parent 8277e8f commit d4222e5

File tree

2 files changed

+415
-226
lines changed

2 files changed

+415
-226
lines changed

src/utils/common-expression-helpers.test.ts

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,68 @@ describe('CommonExpressionHelpers', () => {
104104
});
105105
});
106106

107+
describe('isDateAfterSimple', () => {
108+
it('should return true if the left date is after the right date', () => {
109+
const left = new Date('2021-12-31');
110+
const right = '2021-01-01';
111+
expect(helpers.isDateAfterSimple(left, right)).toBe(true);
112+
});
113+
114+
it('should return false if the left date is not after the right date', () => {
115+
const left = new Date('2021-01-01');
116+
const right = '2021-12-31';
117+
expect(helpers.isDateAfterSimple(left, right)).toBe(false);
118+
});
119+
120+
it('should accept a Date object as the right parameter', () => {
121+
const left = new Date('2021-12-31');
122+
const right = new Date('2021-01-01');
123+
expect(helpers.isDateAfterSimple(left, right)).toBe(true);
124+
});
125+
126+
it('should use custom format when provided', () => {
127+
const left = new Date('2021-12-31');
128+
const right = '31/01/2021';
129+
expect(helpers.isDateAfterSimple(left, right, 'DD/MM/YYYY')).toBe(true);
130+
});
131+
});
132+
133+
describe('addWeeksToDate', () => {
134+
it('should add weeks to a date correctly', () => {
135+
const date = new Date('2021-01-01');
136+
const result = helpers.addWeeksToDate(date, 2);
137+
expect(result).toEqual(new Date('2021-01-15'));
138+
});
139+
140+
it('should not mutate the original date', () => {
141+
const date = new Date('2021-01-01');
142+
const originalTime = date.getTime();
143+
helpers.addWeeksToDate(date, 2);
144+
expect(date.getTime()).toBe(originalTime);
145+
});
146+
});
147+
148+
describe('addDaysToDate', () => {
149+
it('should add days to a date correctly', () => {
150+
const date = new Date('2021-01-01');
151+
const result = helpers.addDaysToDate(date, 10);
152+
expect(result).toEqual(new Date('2021-01-11'));
153+
});
154+
155+
it('should not mutate the original date', () => {
156+
const date = new Date('2021-01-01');
157+
const originalTime = date.getTime();
158+
helpers.addDaysToDate(date, 10);
159+
expect(date.getTime()).toBe(originalTime);
160+
});
161+
162+
it('should handle negative days', () => {
163+
const date = new Date('2021-01-15');
164+
const result = helpers.addDaysToDate(date, -5);
165+
expect(result).toEqual(new Date('2021-01-10'));
166+
});
167+
});
168+
107169
describe('useFieldValue', () => {
108170
it('should return the field value if the key exists', () => {
109171
helpers.allFieldValues = { question1: 'value1' };
@@ -155,6 +217,23 @@ describe('CommonExpressionHelpers', () => {
155217
});
156218
});
157219

220+
describe('calcBSA', () => {
221+
it('should return the correct BSA value using Mosteller formula', () => {
222+
// BSA = sqrt((height * weight) / 3600)
223+
// For height=180cm, weight=75kg: sqrt((180 * 75) / 3600) = sqrt(3.75) ≈ 1.94
224+
const height = 180;
225+
const weight = 75;
226+
expect(helpers.calcBSA(height, weight)).toBeCloseTo(1.94, 2);
227+
});
228+
229+
it('should return null if height or weight is not provided', () => {
230+
expect(helpers.calcBSA(null, 75)).toBe(null);
231+
expect(helpers.calcBSA(180, null)).toBe(null);
232+
expect(helpers.calcBSA(0, 75)).toBe(null);
233+
expect(helpers.calcBSA(180, 0)).toBe(null);
234+
});
235+
});
236+
158237
describe('calcEDD', () => {
159238
it('should return the expected date of delivery', () => {
160239
const lmp = new Date('2021-01-01');
@@ -349,8 +428,8 @@ describe('CommonExpressionHelpers', () => {
349428
expect(helpers.calcTimeDifference(obsDate, 'y')).toBe(1);
350429
});
351430

352-
it('should return "0" if obsDate is not provided', () => {
353-
expect(helpers.calcTimeDifference(null, 'd')).toBe('0');
431+
it('should return 0 if obsDate is not provided', () => {
432+
expect(helpers.calcTimeDifference(null, 'd')).toBe(0);
354433
});
355434
});
356435

0 commit comments

Comments
 (0)