diff --git a/src/adapters/inline-date-adapter.test.ts b/src/adapters/inline-date-adapter.test.ts new file mode 100644 index 00000000..5175a9d4 --- /dev/null +++ b/src/adapters/inline-date-adapter.test.ts @@ -0,0 +1,99 @@ +import { InlineDateAdapter } from './inline-date-adapter'; +import { parseDate, toOmrsIsoString } from '@openmrs/esm-framework'; + +describe('InlineDateAdapter', () => { + let field; + let context; + let targetField; + + beforeEach(() => { + targetField = { + id: 'temperature', + questionOptions: { + concept: '2c43u05b-b6d8-4eju-8f37-0b14f5347560', + rendering: 'number' + }, + value: '36', + meta: {} + }; + + field = { + id: 'temperature_inline_date', + meta: { + targetField: 'temperature' + } + }; + + context = { + getFormField: jest.fn().mockReturnValue(targetField), + methods: { + getValues: jest.fn().mockReturnValue('36') + }, + formFields: [targetField] + }; + }); + + it('should overwrite the target field obs date prop value', () => { + const newDate = new Date('2024-04-01T19:50:00.000+0000'); + targetField.meta.submission = { + newValue: { value: '36' } + }; + + InlineDateAdapter.transformFieldValue(field, newDate, context); + + expect(targetField.meta.submission.newValue.obsDatetime).toBe(toOmrsIsoString(newDate)); + }); + + it('should edit a previously associated date while in edit mode', () => { + const newDate = new Date('2024-04-01T19:50:00.000+0000'); + targetField.meta.previousValue = { + obsDatetime: '2024-04-01T19:40:40.000+0000', + value: '36' + }; + + InlineDateAdapter.transformFieldValue(field, newDate, context); + + expect(targetField.meta.submission.newValue.obsDatetime).toBe(toOmrsIsoString(newDate)); + }); + + it('should only commit side-effects if the previous date value changes', () => { + const existingDate = new Date('2024-04-01T19:50:00.000+0000'); + targetField.meta.previousValue = { + obsDatetime: toOmrsIsoString(existingDate), + value: '36', + }; + InlineDateAdapter.transformFieldValue(field, existingDate, context); + + expect(targetField.meta.submission).toBeUndefined(); + }); + + it('should commit side-effects if the target field has a value or submission', () => { + const newDate = new Date('2024-04-01T19:50:00.000+0000'); + targetField.meta.previousValue = { + value: '36' + }; + + InlineDateAdapter.transformFieldValue(field, newDate, context); + + expect(targetField.meta.submission.newValue.obsDatetime).toBe(toOmrsIsoString(newDate)); + }); + + it('should extract initial value from the target field obs value', () => { + const encounter = { + uuid: '873455da-3ec4-453c-b565-7c1fe35426be', + obs: [{ + concept: { uuid: '2c43u05b-b6d8-4eju-8f37-0b14f5347560' }, + value: '36', + obsDatetime: '2024-04-01T19:50:00.000+0000' + }] + }; + + targetField.meta.previousValue = { + obsDatetime: '2024-04-01T19:50:00.000+0000' + }; + + const result = InlineDateAdapter.getInitialValue(field, encounter, context); + + expect(result).toEqual(parseDate('2024-04-01T19:50:00.000+0000')); + }); +}); diff --git a/src/adapters/inline-date-adapter.ts b/src/adapters/inline-date-adapter.ts index 1cc6d53b..5239e774 100644 --- a/src/adapters/inline-date-adapter.ts +++ b/src/adapters/inline-date-adapter.ts @@ -11,25 +11,29 @@ export const InlineDateAdapter: FormFieldValueAdapter = { const targetField = context.getFormField(field.meta.targetField); const targetFieldCurrentValue = context.methods.getValues(targetField.id); const dateString = value instanceof Date ? toOmrsIsoString(value) : value; + if (targetField.meta.submission?.newValue) { if (isEmpty(dateString) && !isNewSubmissionEffective(targetField, targetFieldCurrentValue)) { - // clear submission + // Clear submission if the new date is empty and no effective submission exists targetField.meta.submission.newValue = null; - } else { + } else if (targetField.meta.submission.newValue.obsDatetime !== dateString) { + // Only update obsDatetime if the new date differs from the current one targetField.meta.submission.newValue.obsDatetime = dateString; } } else if (!hasSubmission(targetField) && targetField.meta.previousValue) { if (isEmpty(value) && isEmpty(targetField.meta.previousValue.obsDatetime)) { - return null; + return null; // Avoid submission if both new and previous dates are empty + } + if (targetField.meta.previousValue.obsDatetime !== dateString) { + // Only create a new submission if the previous value is different from the new date + const newSubmission = editObs(targetField, targetFieldCurrentValue); + targetField.meta.submission = { + newValue: { + ...newSubmission, + obsDatetime: dateString, + }, + }; } - // generate submission - const newSubmission = editObs(targetField, targetFieldCurrentValue); - targetField.meta.submission = { - newValue: { - ...newSubmission, - obsDatetime: dateString, - }, - }; } }, getInitialValue: function (field: FormField, sourceObject: OpenmrsResource, context: FormProcessorContextProps) { @@ -38,7 +42,7 @@ export const InlineDateAdapter: FormFieldValueAdapter = { const targetFieldId = field.id.split('_inline_date')[0]; const targetField = context.formFields.find((field) => field.id === targetFieldId); if (targetField?.meta.previousValue?.obsDatetime) { - return parseDate(targetField?.meta.previousValue?.obsDatetime); + return parseDate(targetField.meta.previousValue.obsDatetime); } } return null;