-
Notifications
You must be signed in to change notification settings - Fork 103
feat(form-engine): add dependency tracking for form field expressions and improve calculate expression evaluation order #654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
delcroip
wants to merge
7
commits into
openmrs:main
Choose a base branch
from
delcroip:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e5ef8be
feat(form-engine): add dependency tracking for form field expressions…
delcroip e5b4212
fix: update recusrsive
delcroip 059328a
fix: hideExpression based on calculateExpression
delcroip d65c375
Merge remote-tracking branch 'upstream'
delcroip 51fd306
Merge branch 'main' into main
delcroip 5c05013
Merge branch 'main' into main
delcroip 1da8a94
Merge branch 'main' into main
delcroip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import { codedTypes } from '../../../constants'; | |
| import { type FormContextProps } from '../../../provider/form-provider'; | ||
| import { type FormFieldValidator, type SessionMode, type ValidationResult, type FormField } from '../../../types'; | ||
| import { hasRendering } from '../../../utils/common-utils'; | ||
| import { evaluateAsyncExpression, evaluateExpression } from '../../../utils/expression-runner'; | ||
| import { evaluateAsyncExpression, evaluateExpression, trackFieldDependenciesFromString } from '../../../utils/expression-runner'; | ||
| import { evalConditionalRequired, evaluateDisabled, evaluateHide, findFieldSection } from '../../../utils/form-helper'; | ||
| import { isEmpty } from '../../../validators/form-validator'; | ||
| import { reportError } from '../../../utils/error-utils'; | ||
|
|
@@ -83,6 +83,8 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon | |
| context.formFieldAdapters[dependent.type].transformFieldValue(dependent, result, context); | ||
| } | ||
| updateFormField(dependent); | ||
| // Recursively evaluate dependents of calculate fields | ||
| evaluateFieldDependents(dependent, values, context); | ||
| }) | ||
| .catch((error) => { | ||
| reportError(error, 'Error evaluating calculate expression'); | ||
|
|
@@ -171,6 +173,12 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon | |
| patient, | ||
| }, | ||
| ); | ||
| // Track dependencies for answer hide expressions | ||
| trackFieldDependenciesFromString( | ||
| answer.hide?.hideWhenExpression, | ||
| { value: dependent, type: 'field' }, | ||
| formFields, | ||
| ); | ||
| }); | ||
| // evaluate disabled | ||
| dependent?.questionOptions.answers | ||
|
|
@@ -186,6 +194,12 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon | |
| patient, | ||
| }, | ||
| ); | ||
| // Track dependencies for answer disable expressions | ||
| trackFieldDependenciesFromString( | ||
| answer.disable?.disableWhenExpression, | ||
| { value: dependent, type: 'field' }, | ||
| formFields, | ||
| ); | ||
|
||
| }); | ||
| // evaluate readonly | ||
| if (!dependent.isHidden && dependent.meta.readonlyExpression) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
| import { useEvaluateFormFieldExpressions } from './useEvaluateFormFieldExpressions'; | ||
| import { type FormProcessorContextProps, type FormField, type RenderType } from '../types'; | ||
|
|
||
| // Mock form JSON | ||
| const mockFormJson = { | ||
| name: 'test-form', | ||
| uuid: 'test-uuid', | ||
| pages: [], | ||
| referencedForms: [], | ||
| processor: 'EncounterFormProcessor', | ||
| encounterType: 'test-encounter-type-uuid', | ||
| }; | ||
|
|
||
| // Mock the form processor context | ||
| const createMockContext = (formFields: FormField[] = []): FormProcessorContextProps => ({ | ||
| formJson: mockFormJson, | ||
| formFields, | ||
| patient: { | ||
| id: 'test-patient-id', | ||
| resourceType: 'Patient', | ||
| } as any, | ||
| sessionMode: 'enter', | ||
| visit: {} as any, | ||
| sessionDate: new Date(), | ||
| location: {} as any, | ||
| currentProvider: {} as any, | ||
| layoutType: 'desktop' as any, | ||
| processor: { | ||
| getHistoricalValue: jest.fn(), | ||
| } as any, | ||
| formFieldAdapters: {}, | ||
| formFieldValidators: {}, | ||
| }); | ||
|
|
||
| describe('useEvaluateFormFieldExpressions', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should evaluate field expressions and track dependencies', () => { | ||
| const formValues = { field1: 'value1', field2: 'value2' }; | ||
|
|
||
| // Mock form fields with expressions that reference other fields | ||
| const mockFields: FormField[] = [ | ||
| { | ||
| id: 'field1', | ||
| type: 'obs', | ||
| questionOptions: { | ||
| rendering: 'text' as any, | ||
| answers: [], | ||
| }, | ||
| hide: { | ||
| hideWhenExpression: 'field2 === "hide"', | ||
| }, | ||
| disabled: { | ||
| disableWhenExpression: 'field1 === "disabled"', | ||
| }, | ||
| readonly: 'field2 === "readonly"', | ||
| isHidden: false, | ||
| isDisabled: false, | ||
| meta: {}, | ||
| }, | ||
| { | ||
| id: 'field2', | ||
| type: 'obs', | ||
| questionOptions: { | ||
| rendering: 'text' as any, | ||
| answers: [ | ||
| { | ||
| label: 'Answer 1', | ||
| concept: 'answer1', | ||
| hide: { | ||
| hideWhenExpression: 'field1 === "hide_answer"', | ||
| }, | ||
| }, | ||
| { | ||
| label: 'Answer 2', | ||
| concept: 'answer2', | ||
| disable: { | ||
| disableWhenExpression: 'field1 === "disable_answer"', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| isHidden: false, | ||
| isDisabled: false, | ||
| meta: {}, | ||
| }, | ||
| ]; | ||
|
|
||
| const contextWithFields = createMockContext(mockFields); | ||
|
|
||
| const { result } = renderHook(() => | ||
| useEvaluateFormFieldExpressions(formValues, contextWithFields) | ||
| ); | ||
|
|
||
| // Check that fields are evaluated | ||
| expect(result.current.evaluatedFields).toBeDefined(); | ||
| expect(result.current.evaluatedFields.length).toBe(2); | ||
|
|
||
| // Check that expressions are evaluated | ||
| const field1 = result.current.evaluatedFields.find(f => f.id === 'field1'); | ||
| const field2 = result.current.evaluatedFields.find(f => f.id === 'field2'); | ||
|
|
||
| expect(field1).toBeDefined(); | ||
| expect(field2).toBeDefined(); | ||
|
|
||
| // Field1 should not be hidden (field2 !== "hide") | ||
| expect(field1.isHidden).toBe(false); | ||
|
|
||
| // Field2 answers should be evaluated | ||
| expect(field2.questionOptions.answers[0].isHidden).toBe(false); // field1 !== "hide_answer" | ||
| expect(field2.questionOptions.answers[1].disable.isDisabled).toBe(false); // field1 !== "disable_answer" | ||
| }); | ||
|
|
||
| it('should handle empty expressions gracefully', () => { | ||
| const formValues = {}; | ||
|
|
||
| const mockFields: FormField[] = [ | ||
| { | ||
| id: 'field1', | ||
| type: 'obs', | ||
| questionOptions: { | ||
| rendering: 'text' as any, | ||
| answers: [], | ||
| }, | ||
| isHidden: false, | ||
| isDisabled: false, | ||
| meta: {}, | ||
| }, | ||
| ]; | ||
|
|
||
| const contextWithFields = createMockContext(mockFields); | ||
|
|
||
| const { result } = renderHook(() => | ||
| useEvaluateFormFieldExpressions(formValues, contextWithFields) | ||
| ); | ||
|
|
||
| expect(result.current.evaluatedFields).toBeDefined(); | ||
| expect(result.current.evaluatedFields.length).toBe(1); | ||
| }); | ||
|
|
||
| it('should evaluate page and section visibility', () => { | ||
| const formValues = { field1: 'value1' }; | ||
|
|
||
| const mockFields: FormField[] = [ | ||
| { | ||
| id: 'field1', | ||
| type: 'obs', | ||
| questionOptions: { | ||
| rendering: 'text' as any, | ||
| answers: [], | ||
| }, | ||
| isHidden: false, | ||
| isDisabled: false, | ||
| meta: {}, | ||
| }, | ||
| ]; | ||
|
|
||
| const formJsonWithPages = { | ||
| ...mockFormJson, | ||
| pages: [ | ||
| { | ||
| label: 'page1', | ||
| sections: [ | ||
| { | ||
| label: 'section1', | ||
| questions: [mockFields[0]], | ||
| isHidden: false, | ||
| isExpanded: 'true', | ||
| }, | ||
| ], | ||
| isHidden: false, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const contextWithFields = createMockContext(mockFields); | ||
| contextWithFields.formJson = formJsonWithPages; | ||
|
|
||
| const { result } = renderHook(() => | ||
| useEvaluateFormFieldExpressions(formValues, contextWithFields) | ||
| ); | ||
|
|
||
| expect(result.current.evaluatedFormJson).toBeDefined(); | ||
| expect(result.current.evaluatedPagesVisibility).toBe(true); | ||
| }); | ||
|
|
||
| it('should handle fields with calculate expressions', () => { | ||
| const formValues = { field1: 5, field2: 10 }; | ||
|
|
||
| const mockFields: FormField[] = [ | ||
| { | ||
| id: 'field1', | ||
| type: 'obs', | ||
| questionOptions: { | ||
| rendering: 'number' as any, | ||
| answers: [], | ||
| calculate: { | ||
| calculateExpression: 'field2 * 2', | ||
| }, | ||
| }, | ||
| isHidden: false, | ||
| isDisabled: false, | ||
| meta: {}, | ||
| }, | ||
| { | ||
| id: 'field2', | ||
| type: 'obs', | ||
| questionOptions: { | ||
| rendering: 'number' as any, | ||
| answers: [], | ||
| }, | ||
| isHidden: false, | ||
| isDisabled: false, | ||
| meta: {}, | ||
| }, | ||
| ]; | ||
|
|
||
| const contextWithFields = createMockContext(mockFields); | ||
|
|
||
| const { result } = renderHook(() => | ||
| useEvaluateFormFieldExpressions(formValues, contextWithFields) | ||
| ); | ||
|
|
||
| expect(result.current.evaluatedFields).toBeDefined(); | ||
| expect(result.current.evaluatedFields.length).toBe(2); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.