Skip to content
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

(test) O3-3926 Add test coverage for the InlineDateAdapter #422

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/adapters/inline-date-adapter.test.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
});
28 changes: 16 additions & 12 deletions src/adapters/inline-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
Loading