Skip to content
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
4 changes: 4 additions & 0 deletions packages/smart-forms-renderer/src/test/data/removeIdSample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ export const qrRemoveIdResult: QuestionnaireResponse = {
item: [
{
linkId: 'myPatient1.name',
definition: 'http://hl7.org/fhir/StructureDefinition/Patient#Patient.name',
text: 'name *',
item: [
{
linkId: 'myPatient1.name.first',
definition: 'http://hl7.org/fhir/StructureDefinition/Patient#Patient.name.given',
text: 'firstName *',
answer: [
{
Expand All @@ -96,10 +98,12 @@ export const qrRemoveIdResult: QuestionnaireResponse = {
},
{
linkId: 'myPatient1.name',
definition: 'http://hl7.org/fhir/StructureDefinition/Patient#Patient.name',
text: 'name *',
item: [
{
linkId: 'myPatient1.name.first',
definition: 'http://hl7.org/fhir/StructureDefinition/Patient#Patient.name.given',
text: 'firstName *',
answer: [
{
Expand Down
46 changes: 46 additions & 0 deletions packages/smart-forms-renderer/src/test/removeEmptyAnswers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,52 @@ describe('removeEmptyAnswersFromItemRecursive', () => {
expect(result).toEqual(qrItem);
});

it('should preserve definition from qItem when present', () => {
const qItem: QuestionnaireItem = {
linkId: 'test-item',
type: 'boolean',
text: 'Has triple negative breast cancer',
definition: 'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]'
};

const qrItem: QuestionnaireResponseItem = {
linkId: 'test-item',
definition: 'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]',
text: 'Has triple negative breast cancer',
answer: [{ valueBoolean: true }]
};

mockQrItemHasItemsOrAnswer.mockReturnValue(true);
mockIsHiddenByEnableWhen.mockReturnValue(false);

const result = removeEmptyAnswersFromItemRecursive(qItem, qrItem, enableWhenContext);

expect((result as QuestionnaireResponseItem).definition).toBe(
'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]'
);
});

it('should not include definition property when qItem has none', () => {
const qItem: QuestionnaireItem = {
linkId: 'test-item',
type: 'string',
text: 'No definition here'
};

const qrItem: QuestionnaireResponseItem = {
linkId: 'test-item',
text: 'No definition here',
answer: [{ valueString: 'answer' }]
};

mockQrItemHasItemsOrAnswer.mockReturnValue(true);
mockIsHiddenByEnableWhen.mockReturnValue(false);

const result = removeEmptyAnswersFromItemRecursive(qItem, qrItem, enableWhenContext);

expect(result).not.toHaveProperty('definition');
});

it('should return null when item is hidden by enableWhen', () => {
const qItem: QuestionnaireItem = {
linkId: 'test-item',
Expand Down
80 changes: 80 additions & 0 deletions packages/smart-forms-renderer/src/test/removeRepeatId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,86 @@ describe('removeRepeatId', () => {
expect(result.text).toBe('Test item text');
});

it('should preserve definition from qItem when present', () => {
const qItem: QuestionnaireItem = {
linkId: 'test-item',
type: 'boolean',
text: 'Has triple negative breast cancer',
definition: 'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]'
};

const qrItem: QuestionnaireResponseItem = {
linkId: 'test-item',
definition:
'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]',
text: 'Has triple negative breast cancer',
answer: [{ valueBoolean: true }]
};

const result = removeInternalRepeatIdsRecursive(qItem, qrItem) as QuestionnaireResponseItem;

expect(result.definition).toBe(
'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]'
);
});

it('should preserve definition on nested group and child items', () => {
const qItem: QuestionnaireItem = {
linkId: '1',
type: 'group',
text: 'IsTNBCFeature',
definition: 'http://example.org/fhir/StructureDefinition/IsTNBCFeature',
item: [
{
linkId: '1.1',
type: 'boolean',
text: 'Has triple negative breast cancer',
definition:
'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]'
}
]
};

const qrItem: QuestionnaireResponseItem = {
linkId: '1',
definition: 'http://example.org/fhir/StructureDefinition/IsTNBCFeature',
text: 'IsTNBCFeature',
item: [
{
linkId: '1.1',
definition:
'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]',
text: 'Has triple negative breast cancer',
answer: [{ valueBoolean: true }]
}
]
};

const result = removeInternalRepeatIdsRecursive(qItem, qrItem) as QuestionnaireResponseItem;

expect(result.definition).toBe('http://example.org/fhir/StructureDefinition/IsTNBCFeature');
expect(result.item![0].definition).toBe(
'http://example.org/fhir/StructureDefinition/IsTNBCFeature#Observation.value[x]'
);
});

it('should not include definition property when qItem has none', () => {
const qItem: QuestionnaireItem = {
linkId: 'test-item',
type: 'string',
text: 'No definition here'
};

const qrItem: QuestionnaireResponseItem = {
linkId: 'test-item',
answer: [{ valueString: 'answer' }]
};

const result = removeInternalRepeatIdsRecursive(qItem, qrItem) as QuestionnaireResponseItem;

expect(result).not.toHaveProperty('definition');
});

it('should handle empty answers correctly', () => {
const qItem: QuestionnaireItem = {
linkId: 'test-item',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function removeEmptyAnswersFromItem(

return {
linkId: qItem.linkId,
...(qItem.definition && { definition: qItem.definition }),
...(qItem.text && { text: qItem.text }),
...(childQrItems.length > 0 && { item: childQrItems }),
...(updatedAnswers.length > 0 && { answer: updatedAnswers })
Expand Down
1 change: 1 addition & 0 deletions packages/smart-forms-renderer/src/utils/removeRepeatId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function removeInternalRepeatIdsFromItem(

return {
linkId: qItem.linkId,
...(qItem.definition && { definition: qItem.definition }),
...(qItem.text && { text: qItem.text }),
...(childQrItems.length > 0 && { item: childQrItems }),
...(updatedAnswers.length > 0 && { answer: updatedAnswers })
Expand Down
Loading