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

Check if previous/next page is hidden to show previous/next buttons #2569

Merged
merged 4 commits into from
Dec 20, 2024
Merged
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
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
Loading