|
| 1 | +import { EncounterFormProcessor } from './encounter-form-processor'; |
| 2 | +import { type FormSchema } from '../../types'; |
| 3 | + |
| 4 | +describe('EncounterFormProcessor', () => { |
| 5 | + describe('prepareFormSchema - validateCalculateExpressions', () => { |
| 6 | + let processor: EncounterFormProcessor; |
| 7 | + let consoleSpy: jest.SpyInstance; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + processor = new EncounterFormProcessor(null); |
| 11 | + consoleSpy = jest.spyOn(console, 'error').mockImplementation(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + consoleSpy.mockRestore(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should warn when a calculateExpression contains a quoted string that matches a field ID', () => { |
| 19 | + const schema: FormSchema = { |
| 20 | + name: 'Test Form', |
| 21 | + pages: [ |
| 22 | + { |
| 23 | + label: 'Page 1', |
| 24 | + sections: [ |
| 25 | + { |
| 26 | + label: 'Section 1', |
| 27 | + isExpanded: 'true', |
| 28 | + questions: [ |
| 29 | + { |
| 30 | + label: 'Last Menstrual Period', |
| 31 | + type: 'obs', |
| 32 | + id: 'lmp', |
| 33 | + questionOptions: { |
| 34 | + rendering: 'date', |
| 35 | + concept: 'test-concept', |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + label: 'Expected Date of Delivery', |
| 40 | + type: 'obs', |
| 41 | + id: 'edd', |
| 42 | + questionOptions: { |
| 43 | + rendering: 'date', |
| 44 | + concept: 'test-concept', |
| 45 | + calculate: { |
| 46 | + calculateExpression: "calcEDD('lmp')", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + ], |
| 55 | + processor: 'EncounterFormProcessor', |
| 56 | + encounterType: 'test-encounter-type', |
| 57 | + referencedForms: [], |
| 58 | + uuid: 'test-form-uuid', |
| 59 | + } as unknown as FormSchema; |
| 60 | + |
| 61 | + processor.prepareFormSchema(schema); |
| 62 | + |
| 63 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 64 | + expect.stringContaining("incorrectly quotes the field ID 'lmp' as a string"), |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not warn when a calculateExpression uses bare variable references', () => { |
| 69 | + const schema: FormSchema = { |
| 70 | + name: 'Test Form', |
| 71 | + pages: [ |
| 72 | + { |
| 73 | + label: 'Page 1', |
| 74 | + sections: [ |
| 75 | + { |
| 76 | + label: 'Section 1', |
| 77 | + isExpanded: 'true', |
| 78 | + questions: [ |
| 79 | + { |
| 80 | + label: 'Last Menstrual Period', |
| 81 | + type: 'obs', |
| 82 | + id: 'lmp', |
| 83 | + questionOptions: { |
| 84 | + rendering: 'date', |
| 85 | + concept: 'test-concept', |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + label: 'Expected Date of Delivery', |
| 90 | + type: 'obs', |
| 91 | + id: 'edd', |
| 92 | + questionOptions: { |
| 93 | + rendering: 'date', |
| 94 | + concept: 'test-concept', |
| 95 | + calculate: { |
| 96 | + calculateExpression: 'calcEDD(lmp)', |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + ], |
| 105 | + processor: 'EncounterFormProcessor', |
| 106 | + encounterType: 'test-encounter-type', |
| 107 | + referencedForms: [], |
| 108 | + uuid: 'test-form-uuid', |
| 109 | + } as unknown as FormSchema; |
| 110 | + |
| 111 | + processor.prepareFormSchema(schema); |
| 112 | + |
| 113 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should not warn when a quoted string does not match any field ID', () => { |
| 117 | + const schema: FormSchema = { |
| 118 | + name: 'Test Form', |
| 119 | + pages: [ |
| 120 | + { |
| 121 | + label: 'Page 1', |
| 122 | + sections: [ |
| 123 | + { |
| 124 | + label: 'Section 1', |
| 125 | + isExpanded: 'true', |
| 126 | + questions: [ |
| 127 | + { |
| 128 | + label: 'Duration', |
| 129 | + type: 'obs', |
| 130 | + id: 'duration', |
| 131 | + questionOptions: { |
| 132 | + rendering: 'number', |
| 133 | + concept: 'test-concept', |
| 134 | + calculate: { |
| 135 | + calculateExpression: "calcTimeDifference(onsetDate, 'd')", |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + ], |
| 140 | + }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + ], |
| 144 | + processor: 'EncounterFormProcessor', |
| 145 | + encounterType: 'test-encounter-type', |
| 146 | + referencedForms: [], |
| 147 | + uuid: 'test-form-uuid', |
| 148 | + } as unknown as FormSchema; |
| 149 | + |
| 150 | + processor.prepareFormSchema(schema); |
| 151 | + |
| 152 | + // 'd' is not a field ID, so no warning should be issued |
| 153 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should warn for nested questions in obsGroups', () => { |
| 157 | + const schema: FormSchema = { |
| 158 | + name: 'Test Form', |
| 159 | + pages: [ |
| 160 | + { |
| 161 | + label: 'Page 1', |
| 162 | + sections: [ |
| 163 | + { |
| 164 | + label: 'Section 1', |
| 165 | + isExpanded: 'true', |
| 166 | + questions: [ |
| 167 | + { |
| 168 | + label: 'Height', |
| 169 | + type: 'obs', |
| 170 | + id: 'height', |
| 171 | + questionOptions: { |
| 172 | + rendering: 'number', |
| 173 | + concept: 'test-concept', |
| 174 | + }, |
| 175 | + }, |
| 176 | + { |
| 177 | + label: 'Weight', |
| 178 | + type: 'obs', |
| 179 | + id: 'weight', |
| 180 | + questionOptions: { |
| 181 | + rendering: 'number', |
| 182 | + concept: 'test-concept', |
| 183 | + }, |
| 184 | + }, |
| 185 | + { |
| 186 | + label: 'Vitals Group', |
| 187 | + type: 'obsGroup', |
| 188 | + id: 'vitalsGroup', |
| 189 | + questionOptions: { |
| 190 | + rendering: 'group', |
| 191 | + concept: 'test-concept', |
| 192 | + }, |
| 193 | + questions: [ |
| 194 | + { |
| 195 | + label: 'BMI', |
| 196 | + type: 'obs', |
| 197 | + id: 'bmi', |
| 198 | + questionOptions: { |
| 199 | + rendering: 'number', |
| 200 | + concept: 'test-concept', |
| 201 | + calculate: { |
| 202 | + calculateExpression: "calcBMI('height', 'weight')", |
| 203 | + }, |
| 204 | + }, |
| 205 | + }, |
| 206 | + ], |
| 207 | + }, |
| 208 | + ], |
| 209 | + }, |
| 210 | + ], |
| 211 | + }, |
| 212 | + ], |
| 213 | + processor: 'EncounterFormProcessor', |
| 214 | + encounterType: 'test-encounter-type', |
| 215 | + referencedForms: [], |
| 216 | + uuid: 'test-form-uuid', |
| 217 | + } as unknown as FormSchema; |
| 218 | + |
| 219 | + processor.prepareFormSchema(schema); |
| 220 | + |
| 221 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("incorrectly quotes the field ID 'height' as a string")); |
| 222 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("incorrectly quotes the field ID 'weight' as a string")); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments