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

[Multiple choice questions] Make "other" text required #2870

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -49,7 +49,7 @@ class MultipleChoiceTaskViewModel @Inject constructor() : AbstractTaskViewModel(
?.let {
val selected =
if (task.isRequired) {
otherText != ""
otherText.trim() != ""
shobhitagarwal1612 marked this conversation as resolved.
Show resolved Hide resolved
} else {
true
}
Expand Down Expand Up @@ -92,6 +92,12 @@ class MultipleChoiceTaskViewModel @Inject constructor() : AbstractTaskViewModel(
}

fun updateResponse() {
// Check if "other" text is missing or not.
if (task.isRequired && selectedIds.contains(OTHER_ID) && otherText.trim().isEmpty()) {
shobhitagarwal1612 marked this conversation as resolved.
Show resolved Hide resolved
clearResponse()
return
}

setValue(
fromList(
task.multipleChoice,
Expand All @@ -103,7 +109,9 @@ class MultipleChoiceTaskViewModel @Inject constructor() : AbstractTaskViewModel(
)
}

/* Reads the list of options in a multiple choice object and converts them to MultipleChoiceItems*/
/**
* Reads the list of options in a multiple choice object and converts them to MultipleChoiceItems.
*/
private fun updateMultipleChoiceItems() {
val multipleChoice = checkNotNull(task.multipleChoice)
val itemsFromOptions: MutableList<MultipleChoiceItem> = mutableListOf()
Expand All @@ -130,7 +138,7 @@ class MultipleChoiceTaskViewModel @Inject constructor() : AbstractTaskViewModel(
this._items.value = itemsFromOptions
}

/* Reads the saved task value and adds selected items to the selected list*/
/** Reads the saved task value and adds selected items to the selected list. */
private fun loadPendingSelections() {
val selectedOptionIds = (taskTaskData.value as? MultipleChoiceTaskData)?.selectedOptionIds
val multipleChoice = checkNotNull(task.multipleChoice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,21 @@ class MultipleChoiceTaskFragmentTest :
}

@Test
fun `saves other text when selected`() = runWithTestDispatcher {
fun `saves other text`() = runWithTestDispatcher {
val multipleChoice = MultipleChoice(options, MultipleChoice.Cardinality.SELECT_MULTIPLE, true)
setupTaskFragment<MultipleChoiceTaskFragment>(job, task.copy(multipleChoice = multipleChoice))
setupTaskFragment<MultipleChoiceTaskFragment>(
job,
task.copy(multipleChoice = multipleChoice, isRequired = true),
)
val userInput = "User inputted text"

onView(withText("Other")).perform(click())
onView(allOf(isDisplayed(), withId(R.id.user_response_text)))
.perform(CustomViewActions.forceTypeText(userInput))

hasValue(MultipleChoiceTaskData(multipleChoice, listOf("[ $userInput ]")))
runner().assertButtonIsEnabled("Next")
}

@Test
fun `selects other option on text input`() = runWithTestDispatcher {
val multipleChoice = MultipleChoice(options, MultipleChoice.Cardinality.SELECT_MULTIPLE, true)
setupTaskFragment<MultipleChoiceTaskFragment>(job, task.copy(multipleChoice = multipleChoice))
val userInput = "A"
onView(withText("Other")).perform(click())
onView(allOf(isDisplayed(), withId(R.id.user_response_text)))
.perform(CustomViewActions.forceTypeText(userInput))
onView(withText("Other")).check(matches(isChecked()))
hasValue(MultipleChoiceTaskData(multipleChoice, listOf("[ $userInput ]")))
runner().assertButtonIsEnabled("Next")
}

@Test
Expand Down Expand Up @@ -315,4 +307,40 @@ class MultipleChoiceTaskFragmentTest :
.assertButtonIsDisabled("Next")
.assertButtonIsEnabled("Skip")
}

@Test
fun `doesn't save response when other text is missing and task is required`() =
runWithTestDispatcher {
val multipleChoice = MultipleChoice(options, MultipleChoice.Cardinality.SELECT_ONE, true)
setupTaskFragment<MultipleChoiceTaskFragment>(
job,
task.copy(multipleChoice = multipleChoice, isRequired = true),
)

onView(withText("Other")).perform(click())
onView(allOf(isDisplayed(), withId(R.id.user_response_text)))
.perform(CustomViewActions.forceTypeText(""))

hasValue(null)
runner().assertButtonIsDisabled("Next")
}

@Test
fun `doesn't save response when multiple options selected but other text is missing and task is required`() =
runWithTestDispatcher {
val multipleChoice = MultipleChoice(options, MultipleChoice.Cardinality.SELECT_MULTIPLE, true)
setupTaskFragment<MultipleChoiceTaskFragment>(
job,
task.copy(multipleChoice = multipleChoice, isRequired = true),
)

onView(withText("Option 1")).perform(click())
onView(withText("Option 2")).perform(click())
onView(withText("Other")).perform(click())
onView(allOf(isDisplayed(), withId(R.id.user_response_text)))
.perform(CustomViewActions.forceTypeText(""))

hasValue(null)
runner().assertButtonIsDisabled("Next")
}
}