-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathuseEvaluateFormFieldExpressions.test.ts
More file actions
254 lines (218 loc) · 8.1 KB
/
useEvaluateFormFieldExpressions.test.ts
File metadata and controls
254 lines (218 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { renderHook, act } from '@testing-library/react';
import { useEvaluateFormFieldExpressions } from './useEvaluateFormFieldExpressions';
import { type FormProcessorContextProps, type FormField } from '../types';
import { evaluateExpression } from '../utils/expression-runner';
import { evalConditionalRequired, evaluateConditionalAnswered, evaluateHide } from '../utils/form-helper';
import { isEmpty } from '../validators/form-validator';
// ---- MOCK DEPENDENCIES ----
// Mock utility functions so we can control their behavior in tests
jest.mock('../utils/expression-runner', () => ({
evaluateExpression: jest.fn(),
}));
jest.mock('../utils/form-helper', () => ({
evalConditionalRequired: jest.fn(),
evaluateConditionalAnswered: jest.fn(),
evaluateHide: jest.fn(),
isPageContentVisible: jest.fn(), // assume handled inside hook
}));
jest.mock('../validators/form-validator', () => ({
isEmpty: jest.fn(),
}));
jest.mock('../utils/common-utils', () => ({
updateFormSectionReferences: jest.fn((formJson) => formJson), // identity for simplicity
}));
// ---- TEST SUITE ----
describe('useEvaluateFormFieldExpressions', () => {
// Mock form values that simulate user input
const mockFormValues = { field1: 'value1' };
// Minimal viable field with conditional expressions on required/readonly/etc.
const mockField: FormField = {
type: 'text',
id: 'field1',
required: true,
readonly: 'someExpression', // dynamic readonly field
questionOptions: {
answers: [
{
label: 'Yes',
value: 'yes',
hide: { hideWhenExpression: 'answerHideExpr' },
disable: { disableWhenExpression: 'answerDisableExpr', isDisabled: false },
},
],
rendering: 'repeating',
repeatOptions: { limitExpression: 'limitExpr' },
},
validators: [{ type: 'conditionalAnswered' }],
meta: {},
};
// Mock context simulating a full form setup
const mockProcessorContext: FormProcessorContextProps = {
formJson: {
name: 'testForm',
pages: [
{
label: 'Page 1',
hide: { hideWhenExpression: 'pageHideExpr' },
sections: [
{
label: 'Section 1',
hide: { hideWhenExpression: 'sectionHideExpr' },
isExpanded: 'false',
questions: [mockField],
},
],
},
],
processor: undefined,
uuid: 'test uuid 0',
referencedForms: [],
encounterType: 'test encounter type',
},
formFields: [mockField],
patient: {},
sessionMode: 'enter',
visit: undefined,
sessionDate: undefined,
location: undefined,
currentProvider: undefined,
layoutType: 'phone',
processor: undefined,
};
// ---- SETUP/TEARDOWN ----
beforeEach(() => {
// Control mock return values before each test
(evaluateExpression as jest.Mock).mockReturnValue(true);
(evalConditionalRequired as jest.Mock).mockReturnValue(true);
(evaluateConditionalAnswered as jest.Mock).mockImplementation(() => {});
(evaluateHide as jest.Mock).mockImplementation((node) => {
node.value.isHidden = true;
});
(isEmpty as jest.Mock).mockReturnValue(false);
});
afterEach(() => {
jest.clearAllMocks();
});
// ---- MAIN TEST CASE ----
it('evaluates all expressions correctly and updates form state', async () => {
const { result } = renderHook(() =>
useEvaluateFormFieldExpressions(mockFormValues, mockProcessorContext),
);
// Wait for useEffect to run
await act(async () => {
await Promise.resolve();
});
const [evaluatedField] = result.current.evaluatedFields;
// Field-level assertions
expect(evaluatedField.isHidden).toBe(false);
expect(evaluatedField.isRequired).toBe(true);
expect(evaluatedField.isDisabled).toBe(false);
expect(evaluatedField.readonly).toBe(true);
// Answer-level assertions
expect(evaluatedField.questionOptions.answers?.[0].isHidden).toBe(true);
expect(evaluatedField.questionOptions.answers?.[0].disable.isDisabled).toBe(true);
expect(evaluatedField.questionOptions.repeatOptions?.limit).toBe(true);
// Overall form assertions
expect(result.current.evaluatedFormJson).toEqual(mockProcessorContext.formJson);
expect(result.current.evaluatedPagesVisibility).toBe(true);
});
// ---- EDGE CASES ----
describe('useEvaluateFormFieldExpressions edge cases', () => {
beforeEach(() => {
(evaluateExpression as jest.Mock).mockReturnValue(true);
(evalConditionalRequired as jest.Mock).mockReturnValue(true);
(evaluateConditionalAnswered as jest.Mock).mockImplementation(() => {});
(evaluateHide as jest.Mock).mockImplementation((node) => {
node.value.isHidden = true;
});
(isEmpty as jest.Mock).mockReturnValue(false);
});
afterEach(() => {
jest.clearAllMocks();
});
it('handles empty formFields gracefully', () => {
const processorContextEmptyFields: FormProcessorContextProps = {
...mockProcessorContext,
formFields: [],
};
const { result } = renderHook(() =>
useEvaluateFormFieldExpressions(mockFormValues, processorContextEmptyFields),
);
expect(result.current.evaluatedFields).toEqual([]);
expect(result.current.evaluatedFormJson).toEqual(mockProcessorContext.formJson);
});
it('correctly evaluates when no hide expression is provided', () => {
const fieldNoHide: FormField = {
id: 'field3',
type: 'obs',
questionOptions: { rendering: null },
};
const contextNoHide = {
...mockProcessorContext,
formFields: [fieldNoHide],
};
const { result } = renderHook(() =>
useEvaluateFormFieldExpressions(mockFormValues, contextNoHide),
);
expect(result.current.evaluatedFields[0].isHidden).toBe(false);
});
it('handles readonly expressions that are boolean strings as boolean strings', () => {
const fieldWithReadonlyTrue: FormField = {
id: 'field5',
type: 'obs',
questionOptions: { rendering: null },
readonly: 'true', // should remain a string
};
const contextReadonlyTrue = {
...mockProcessorContext,
formFields: [fieldWithReadonlyTrue],
};
const { result } = renderHook(() =>
useEvaluateFormFieldExpressions(mockFormValues, contextReadonlyTrue),
);
expect(result.current.evaluatedFields[0].readonly).toBe('true');
});
it('handles readonly expressions that are non-boolean strings', () => {
const fieldWithReadonlyExpr: FormField = {
id: 'field6',
type: 'obs',
questionOptions: { rendering: null },
readonly: 'someExpression',
meta: {},
};
const contextReadonlyExpr = {
...mockProcessorContext,
formFields: [fieldWithReadonlyExpr],
};
(evaluateExpression as jest.Mock).mockReturnValueOnce(false);
const { result } = renderHook(() =>
useEvaluateFormFieldExpressions(mockFormValues, contextReadonlyExpr),
);
expect(result.current.evaluatedFields[0].meta.readonlyExpression).toBe('someExpression');
expect(result.current.evaluatedFields[0].readonly).toBe(false);
});
it('handles answers without hide or disable expressions', () => {
const fieldWithSimpleAnswers: FormField = {
id: 'field7',
type: 'testType',
questionOptions: {
answers: [
{ label: 'Option 1', value: 'opt1' },
{ label: 'Option 2', value: 'opt2' },
],
rendering: 'radio',
},
};
const contextSimpleAnswers = {
...mockProcessorContext,
formFields: [fieldWithSimpleAnswers],
};
(isEmpty as jest.Mock).mockReturnValue(true); // simulate no hide/disable logic
const { result } = renderHook(() =>
useEvaluateFormFieldExpressions(mockFormValues, contextSimpleAnswers),
);
expect(result.current.evaluatedFields[0].questionOptions.answers[0].isHidden).toBeUndefined();
expect(result.current.evaluatedFields[0].questionOptions.answers[0].disable).toBeUndefined();
});
});
});