Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2655-add-missing-item-for-repeat…
Browse files Browse the repository at this point in the history
…ed-group-has-child-items' into master-release
  • Loading branch information
ndegwamartin committed Aug 19, 2024
2 parents 20912f5 + bbb7051 commit db494e6
Show file tree
Hide file tree
Showing 15 changed files with 339 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
* might not contain answers to unanswered or disabled questions. Note : this only applies to
* [QuestionnaireItemComponent]s nested under a group.
*/
private fun addMissingResponseItems(
@VisibleForTesting
internal fun addMissingResponseItems(
questionnaireItems: List<QuestionnaireItemComponent>,
responseItems: MutableList<QuestionnaireResponseItemComponent>,
) {
Expand All @@ -446,6 +447,14 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
responseItems = responseItemMap[it.linkId]!!.single().item,
)
}
if (it.type == Questionnaire.QuestionnaireItemType.GROUP && it.repeats) {
responseItemMap[it.linkId]!!.forEach { rItem ->
addMissingResponseItems(
questionnaireItems = it.item,
responseItems = rItem.item,
)
}
}
responseItems.addAll(responseItemMap[it.linkId]!!)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,141 @@ class QuestionnaireViewModelTest {
}
}

@Test
fun `should add missing response item inside a repeated group`() {
val questionnaireString =
"""
{
"resourceType": "Questionnaire",
"item": [
{
"linkId": "1",
"type": "group",
"text": "Repeated Group",
"repeats": true,
"item": [
{
"linkId": "1-1",
"type": "date",
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/entryFormat",
"valueString": "yyyy-mm-dd"
}
]
},
{
"linkId": "1-2",
"type": "boolean"
}
]
}
]
}
"""
.trimIndent()

val questionnaireResponseString =
"""
{
"resourceType": "QuestionnaireResponse",
"item": [
{
"linkId": "1",
"text": "Repeated Group",
"item": [
{
"linkId": "1-1",
"answer": [
{
"valueDate": "2023-06-14"
}
]
}
]
},
{
"linkId": "1",
"text": "Repeated Group",
"item": [
{
"linkId": "1-1",
"answer": [
{
"valueDate": "2023-06-13"
}
]
}
]
}
]
}
"""
.trimIndent()

val expectedQuestionnaireResponseString =
"""
{
"resourceType": "QuestionnaireResponse",
"item": [
{
"linkId": "1",
"text": "Repeated Group",
"item": [
{
"linkId": "1-1",
"answer": [
{
"valueDate": "2023-06-14"
}
]
},
{
"linkId": "1-2"
}
]
},
{
"linkId": "1",
"text": "Repeated Group",
"item": [
{
"linkId": "1-1",
"answer": [
{
"valueDate": "2023-06-13"
}
]
},
{
"linkId": "1-2"
}
]
}
]
}
"""
.trimIndent()

val questionnaire =
printer.parseResource(Questionnaire::class.java, questionnaireString) as Questionnaire

val response =
printer.parseResource(QuestionnaireResponse::class.java, questionnaireResponseString)
as QuestionnaireResponse

val expectedResponse =
printer.parseResource(QuestionnaireResponse::class.java, expectedQuestionnaireResponseString)
as QuestionnaireResponse

val viewModel = createQuestionnaireViewModel(questionnaire, response)

runTest {
viewModel.addMissingResponseItems(questionnaire.item, response.item)
assertResourceEquals(response, expectedResponse)
}
}

// ==================================================================== //
// //
// Questionnaire State Flow //
Expand Down
Loading

0 comments on commit db494e6

Please sign in to comment.