Skip to content

Commit

Permalink
(fix) Synchronize obs group state with group member updates (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmale authored Dec 3, 2024
1 parent 19af5e5 commit 66aca31
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/components/renderer/field/form-field-renderer.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
formFieldValidators,
addInvalidField,
removeInvalidField,
updateFormField,
} = context;

const fieldValue = useWatch({ control, name: fieldId, exact: true });
Expand Down Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions src/form-engine.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 66aca31

Please sign in to comment.