|
| 1 | +import { handleFieldLogic, validateFieldValue } from './fieldLogic'; |
| 2 | +import { evaluateAsyncExpression, evaluateExpression } from '../../../utils/expression-runner'; |
| 3 | +import { type FormField } from '../../../types'; |
| 4 | +import { type FormContextProps } from '../../../provider/form-provider'; |
| 5 | + |
| 6 | +jest.mock('../../../utils/expression-runner', () => ({ |
| 7 | + evaluateExpression: jest.fn(), |
| 8 | + evaluateAsyncExpression: jest.fn().mockResolvedValue({ result: 'mockedResult' }), |
| 9 | +})); |
| 10 | + |
| 11 | +describe('handleFieldLogic', () => { |
| 12 | + let mockContext: FormContextProps; |
| 13 | + let mockFieldCoded: FormField; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + mockContext = { |
| 17 | + methods: { |
| 18 | + getValues: jest.fn().mockReturnValue({}), |
| 19 | + setValue: jest.fn(), |
| 20 | + }, |
| 21 | + formFields: [], |
| 22 | + sessionMode: 'edit', |
| 23 | + patient: {}, |
| 24 | + formFieldValidators: {}, |
| 25 | + formFieldAdapters: { |
| 26 | + obs: { |
| 27 | + transformFieldValue: jest.fn(), |
| 28 | + } |
| 29 | + }, |
| 30 | + formJson: { pages: [] }, |
| 31 | + updateFormField: jest.fn(), |
| 32 | + setForm: jest.fn(), |
| 33 | + } as unknown as FormContextProps; |
| 34 | + |
| 35 | + mockFieldCoded = { |
| 36 | + id: 'testField', |
| 37 | + label: 'Test Field', |
| 38 | + type: 'obs', |
| 39 | + questionOptions: { |
| 40 | + rendering: 'radio', |
| 41 | + answers: [ |
| 42 | + { |
| 43 | + label: 'Test Answer', |
| 44 | + concept: 'testConcept', |
| 45 | + disable: { |
| 46 | + disableWhenExpression: 'myValue > 10', |
| 47 | + }, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }, |
| 51 | + fieldDependents: [], |
| 52 | + sectionDependents: [], |
| 53 | + pageDependents: [], |
| 54 | + validators: [], |
| 55 | + } as unknown as FormField; |
| 56 | + }); |
| 57 | + |
| 58 | + it('should evaluate field answer disabled logic', () => { |
| 59 | + (evaluateExpression as jest.Mock).mockReturnValue(true); |
| 60 | + |
| 61 | + handleFieldLogic(mockFieldCoded, mockContext); |
| 62 | + |
| 63 | + expect(evaluateExpression).toHaveBeenCalledWith( |
| 64 | + 'myValue > 10', |
| 65 | + { value: mockFieldCoded, type: 'field' }, |
| 66 | + mockContext.formFields, |
| 67 | + mockContext.methods.getValues(), |
| 68 | + { |
| 69 | + mode: mockContext.sessionMode, |
| 70 | + patient: mockContext.patient, |
| 71 | + }, |
| 72 | + ); |
| 73 | + expect(mockFieldCoded.questionOptions.answers[0].disable.isDisabled).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle field dependents logic', () => { |
| 77 | + mockFieldCoded.fieldDependents = new Set(['dependentField']); |
| 78 | + mockContext.formFields = [ |
| 79 | + { |
| 80 | + id: 'dependentField', |
| 81 | + type: 'obs', |
| 82 | + questionOptions: { |
| 83 | + calculate: { |
| 84 | + calculateExpression: '2 + 2', |
| 85 | + }, |
| 86 | + }, |
| 87 | + validators: [], |
| 88 | + meta: {}, |
| 89 | + } as unknown as FormField, |
| 90 | + ]; |
| 91 | + handleFieldLogic(mockFieldCoded, mockContext); |
| 92 | + |
| 93 | + expect(mockContext.updateFormField).toHaveBeenCalled(); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('validateFieldValue', () => { |
| 98 | + let mockField: FormField; |
| 99 | + let mockValidators: Record<string, any>; |
| 100 | + let mockContext: any; |
| 101 | + |
| 102 | + beforeEach(() => { |
| 103 | + mockField = { |
| 104 | + id: 'testField', |
| 105 | + validators: [ |
| 106 | + { |
| 107 | + type: 'required', |
| 108 | + }, |
| 109 | + ], |
| 110 | + meta: {}, |
| 111 | + } as unknown as FormField; |
| 112 | + |
| 113 | + mockValidators = { |
| 114 | + required: { |
| 115 | + validate: jest.fn().mockReturnValue([ |
| 116 | + { resultType: 'error', message: 'Field is required' }, |
| 117 | + ]), |
| 118 | + }, |
| 119 | + }; |
| 120 | + |
| 121 | + mockContext = { |
| 122 | + formFields: [], |
| 123 | + values: {}, |
| 124 | + expressionContext: { |
| 125 | + patient: {}, |
| 126 | + mode: 'edit', |
| 127 | + }, |
| 128 | + }; |
| 129 | + }); |
| 130 | + |
| 131 | + it('should validate field value and return errors and warnings', () => { |
| 132 | + const result = validateFieldValue(mockField, '', mockValidators, mockContext); |
| 133 | + |
| 134 | + expect(mockValidators.required.validate).toHaveBeenCalledWith( |
| 135 | + mockField, |
| 136 | + '', |
| 137 | + expect.objectContaining(mockContext), |
| 138 | + ); |
| 139 | + expect(result.errors).toEqual([{ resultType: 'error', message: 'Field is required' }]); |
| 140 | + expect(result.warnings).toEqual([]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should return empty errors and warnings if field submission is unspecified', () => { |
| 144 | + mockField.meta.submission = { unspecified: true }; |
| 145 | + |
| 146 | + const result = validateFieldValue(mockField, '', mockValidators, mockContext); |
| 147 | + |
| 148 | + expect(result.errors).toEqual([]); |
| 149 | + expect(result.warnings).toEqual([]); |
| 150 | + }); |
| 151 | +}); |
0 commit comments