Skip to content

Commit

Permalink
Add missing response items to repeated group child items
Browse files Browse the repository at this point in the history
  • Loading branch information
FikriMilano committed Aug 16, 2024
1 parent 603792f commit bbb7051
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
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

0 comments on commit bbb7051

Please sign in to comment.