From 66aca316c52924d99c8f6fc00a608d2c119a8d88 Mon Sep 17 00:00:00 2001 From: Samuel Male Date: Tue, 3 Dec 2024 14:51:14 +0300 Subject: [PATCH] (fix) Synchronize obs group state with group member updates (#437) --- .../field/form-field-renderer.component.tsx | 13 ++++++ src/form-engine.test.tsx | 43 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/components/renderer/field/form-field-renderer.component.tsx b/src/components/renderer/field/form-field-renderer.component.tsx index cb1fe180..95d9a9dd 100644 --- a/src/components/renderer/field/form-field-renderer.component.tsx +++ b/src/components/renderer/field/form-field-renderer.component.tsx @@ -46,6 +46,7 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form formFieldValidators, addInvalidField, removeInvalidField, + updateFormField, } = context; const fieldValue = useWatch({ control, name: fieldId, exact: true }); @@ -132,6 +133,18 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form } setWarnings(validationWarnings); handleFieldLogic(field, context); + if (field.meta.groupId) { + const group = formFields.find((f) => f.id === field.meta.groupId); + if (group) { + group.questions = group.questions.map((child) => { + if (child.id === field.id) { + return field; + } + return child; + }); + updateFormField(group); + } + } }; if (!inputComponentWrapper) { diff --git a/src/form-engine.test.tsx b/src/form-engine.test.tsx index 4e60fc2d..daec5eee 100644 --- a/src/form-engine.test.tsx +++ b/src/form-engine.test.tsx @@ -877,6 +877,49 @@ describe('Form engine component', () => { }); describe('Obs group', () => { + it('should save obs group on form submission', async () => { + const saveEncounterMock = jest.spyOn(api, 'saveEncounter'); + await act(async () => { + renderForm(null, obsGroupTestForm); + }); + + // Fill out the obs group fields + const dateOfBirth = screen.getByRole('textbox', { name: /date of birth/i }); + const maleRadio = screen.getByRole('radio', { name: /^male$/i }); + + await user.click(dateOfBirth); + await user.paste('2020-09-09T00:00:00.000Z'); + await user.click(maleRadio); + + // Submit the form + await user.click(screen.getByRole('button', { name: /save/i })); + + // Verify the encounter was saved with the correct structure + expect(saveEncounterMock).toHaveBeenCalledTimes(1); + + const [_, encounter] = saveEncounterMock.mock.calls[0]; + expect(encounter.obs.length).toBe(1); + expect(encounter.obs[0]).toEqual({ + groupMembers: [ + { + value: '1534AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + concept: '1587AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + formFieldNamespace: 'rfe-forms', + formFieldPath: 'rfe-forms-childSex', + }, + { + value: '2020-09-09', + concept: '164802AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + formFieldNamespace: 'rfe-forms', + formFieldPath: 'rfe-forms-birthDate', + }, + ], + concept: '1c70c490-cafa-4c95-9fdd-a30b62bb78b8', + formFieldNamespace: 'rfe-forms', + formFieldPath: 'rfe-forms-myGroup', + }); + }); + it('should test addition of a repeating group', async () => { await act(async () => { renderForm(null, obsGroupTestForm);