Skip to content

Commit aae10ec

Browse files
committed
(fix) Improve common-expression-helpers
Improves our suite of common expression helpers by fixing bugs and adding documentation. Specific changes include the following: - Fix addWeeksToDate mutation bug (now uses dayjs, doesn't modify input) - Fix calcEDD and calcTreatmentEndDate return types (remove empty object init) - Fix formatDate dead code (remove unreachable null/undefined checks) - Fix calcBMI formula readability with heightInMeters variable - Fix calcHeightForAgeZscore unused weight parameter - Fix isDateAfter to properly handle Date objects - Add customParseFormat plugin for custom date format support - Extract shared Z-score calculation logic into calculateZScoreFromRef - Add isDateAfterSimple and isDateAfterOffset for consistent date APIs - Add comprehensive JSDoc comments to all functions - Add deprecation warnings for functions with hardcoded concept UUIDs
1 parent 7ede9d8 commit aae10ec

File tree

2 files changed

+402
-166
lines changed

2 files changed

+402
-166
lines changed

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,79 @@ 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('isDateAfterOffset', () => {
134+
it('should return true if selected date is after base date plus offset in months', () => {
135+
const selectedDate = new Date('2022-01-01');
136+
const baseDate = new Date('2021-01-01');
137+
expect(helpers.isDateAfterOffset(selectedDate, baseDate, 6, 'months')).toBe(true);
138+
});
139+
140+
it('should return true if selected date is after base date plus offset in weeks', () => {
141+
const selectedDate = new Date('2021-02-08');
142+
const baseDate = new Date('2021-01-01');
143+
expect(helpers.isDateAfterOffset(selectedDate, baseDate, 5, 'weeks')).toBe(true);
144+
});
145+
146+
it('should return true if selected date is after base date plus offset in days', () => {
147+
const selectedDate = new Date('2021-01-31');
148+
const baseDate = new Date('2021-01-01');
149+
expect(helpers.isDateAfterOffset(selectedDate, baseDate, 30, 'days')).toBe(true);
150+
});
151+
152+
it('should return true if selected date is after base date plus offset in years', () => {
153+
const selectedDate = new Date('2022-01-01');
154+
const baseDate = new Date('2021-01-01');
155+
expect(helpers.isDateAfterOffset(selectedDate, baseDate, 1, 'years')).toBe(true);
156+
});
157+
158+
it('should return false if selected date is before base date plus offset', () => {
159+
const selectedDate = new Date('2021-06-01');
160+
const baseDate = new Date('2021-01-01');
161+
expect(helpers.isDateAfterOffset(selectedDate, baseDate, 1, 'years')).toBe(false);
162+
});
163+
});
164+
165+
describe('addWeeksToDate', () => {
166+
it('should add weeks to a date correctly', () => {
167+
const date = new Date('2021-01-01');
168+
const result = helpers.addWeeksToDate(date, 2);
169+
expect(result).toEqual(new Date('2021-01-15'));
170+
});
171+
172+
it('should not mutate the original date', () => {
173+
const date = new Date('2021-01-01');
174+
const originalTime = date.getTime();
175+
helpers.addWeeksToDate(date, 2);
176+
expect(date.getTime()).toBe(originalTime);
177+
});
178+
});
179+
107180
describe('useFieldValue', () => {
108181
it('should return the field value if the key exists', () => {
109182
helpers.allFieldValues = { question1: 'value1' };

0 commit comments

Comments
 (0)