Skip to content

Commit

Permalink
Check if previous/next page is hidden to show previous/next buttons (#…
Browse files Browse the repository at this point in the history
…2569)

* Only select QuestionnaireItem with 'page' extension as page

* Fix hasNextPage and hasPreviousPage to check if page is hidden

* Resolve NIT, renaming test linkId to page4-noExtension-hidden
  • Loading branch information
LZRS authored Dec 20, 2024
1 parent 736578f commit 69b8ebf
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ internal data class QuestionnairePage(
)

internal val QuestionnairePagination.hasPreviousPage: Boolean
get() = pages.any { it.index < currentPageIndex && it.enabled }
get() = pages.any { it.index < currentPageIndex && it.enabled && !it.hidden }

internal val QuestionnairePagination.hasNextPage: Boolean
get() = pages.any { it.index > currentPageIndex && it.enabled }
get() = pages.any { it.index > currentPageIndex && it.enabled && !it.hidden }
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,102 @@ class QuestionnaireViewModelTest {
// Pagination //
// //
// ==================================================================== //
@Test
fun `should include all top level items as pages when any item has page extension`() = runTest {
val questionnaire =
Questionnaire().apply {
id = "a-questionnaire"
addItem(
QuestionnaireItemComponent().apply {
linkId = "page1-noExtension"
type = Questionnaire.QuestionnaireItemType.GROUP
addItem(
QuestionnaireItemComponent().apply {
linkId = "page1-1"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
text = "Question on page 1"
},
)
},
)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page2"
type = Questionnaire.QuestionnaireItemType.GROUP
addExtension(paginationExtension)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page2-1"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
text = "Question on page 2"
},
)
},
)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page3-noExtension"
type = Questionnaire.QuestionnaireItemType.GROUP
addItem(
QuestionnaireItemComponent().apply {
linkId = "page3-1"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
text = "Question on page 3"
},
)
},
)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page4-noExtension-hidden"
addExtension(hiddenExtension)
type = Questionnaire.QuestionnaireItemType.GROUP
addItem(
QuestionnaireItemComponent().apply {
linkId = "page4-1"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
text = "Question on page 4"
},
)
},
)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page5"
type = Questionnaire.QuestionnaireItemType.GROUP
addExtension(paginationExtension)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page5-1"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
text = "Question on page 5"
},
)
},
)
}

val viewModel = createQuestionnaireViewModel(questionnaire)
viewModel.runViewModelBlocking {
assertThat(
(viewModel.questionnaireStateFlow.value.displayMode as DisplayMode.EditMode).pagination,
)
.isEqualTo(
QuestionnairePagination(
isPaginated = true,
pages =
listOf(
QuestionnairePage(0, enabled = true, hidden = false),
QuestionnairePage(1, enabled = true, hidden = false),
QuestionnairePage(2, enabled = true, hidden = false),
QuestionnairePage(3, enabled = true, hidden = true),
QuestionnairePage(4, enabled = true, hidden = false),
),
currentPageIndex = 0,
),
)
}
}

@Test
fun `should show current page`() = runTest {
Expand Down Expand Up @@ -1830,8 +1926,11 @@ class QuestionnaireViewModelTest {
}
val viewModel = createQuestionnaireViewModel(questionnaire)
viewModel.runViewModelBlocking {
val questionnaireStatePagination =
(viewModel.questionnaireStateFlow.value.displayMode as DisplayMode.EditMode).pagination

assertThat(
(viewModel.questionnaireStateFlow.value.displayMode as DisplayMode.EditMode).pagination,
questionnaireStatePagination,
)
.isEqualTo(
QuestionnairePagination(
Expand All @@ -1845,6 +1944,83 @@ class QuestionnaireViewModelTest {
currentPageIndex = 1,
),
)

assertThat(questionnaireStatePagination.hasPreviousPage).isFalse()
}
}

@Test
fun `should skip last page if it is hidden`() = runTest {
val questionnaire =
Questionnaire().apply {
id = "a-questionnaire"
addItem(
QuestionnaireItemComponent().apply {
linkId = "page1"
type = Questionnaire.QuestionnaireItemType.GROUP
addExtension(paginationExtension)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page1-1"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
text = "Question on page 1"
},
)
},
)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page2"
type = Questionnaire.QuestionnaireItemType.GROUP
addExtension(paginationExtension)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page2-1"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
text = "Question on page 2"
},
)
},
)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page3"
type = Questionnaire.QuestionnaireItemType.GROUP
addExtension(paginationExtension)
addExtension(hiddenExtension)
addItem(
QuestionnaireItemComponent().apply {
linkId = "page3-1"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
text = "Question on page 3"
},
)
},
)
}
val viewModel = createQuestionnaireViewModel(questionnaire)
viewModel.runViewModelBlocking {
viewModel.goToNextPage()
val questionnaireStatePagination =
(viewModel.questionnaireStateFlow.value.displayMode as DisplayMode.EditMode).pagination

assertThat(
questionnaireStatePagination,
)
.isEqualTo(
QuestionnairePagination(
isPaginated = true,
pages =
listOf(
QuestionnairePage(0, enabled = true, hidden = false),
QuestionnairePage(1, enabled = true, hidden = false),
QuestionnairePage(2, enabled = true, hidden = true),
),
currentPageIndex = 1,
),
)

assertThat(questionnaireStatePagination.hasNextPage).isFalse()
}
}

Expand Down

0 comments on commit 69b8ebf

Please sign in to comment.