-
Notifications
You must be signed in to change notification settings - Fork 48
fix(surveys): honor choice option shuffling #772
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
Open
lucasheriques
wants to merge
1
commit into
main
Choose a base branch
from
lucas/surveys-shuffle-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog-android-surveys-compose": patch | ||
| --- | ||
|
|
||
| Honor survey shuffleOptions in the built-in choice UI, keeping Other last and the display order stable while answering. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...se/src/test/java/com/posthog/android/surveys/compose/internal/ui/SurveyChoiceOrderTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.posthog.android.surveys.compose.internal.ui | ||
|
|
||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.Parameterized | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNotEquals | ||
|
|
||
| @RunWith(Parameterized::class) | ||
| internal class SurveyChoiceOrderTest(private val hasOpenChoice: Boolean) { | ||
| @Test | ||
| fun `disabled shuffle preserves configured order`() { | ||
| assertEquals(listOf(0, 1, 2), surveyChoiceOrder(listOf("A", "B", "Other"), hasOpenChoice, false)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `shuffle preserves every choice identity and pins Other`() { | ||
| val choices = listOf("A", "B", "C", "Other") | ||
| val order = surveyChoiceOrder(choices, hasOpenChoice, true) | ||
| assertEquals(choices.indices.toList(), order.sorted()) | ||
| assertNotEquals(choices.indices.toList(), order) | ||
| if (hasOpenChoice) assertEquals(choices.lastIndex, order.last()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `two regular choices always swap matching web fallback`() { | ||
| val choices = if (hasOpenChoice) listOf("A", "B", "Other") else listOf("A", "B") | ||
| assertEquals(if (hasOpenChoice) listOf(1, 0, 2) else listOf(1, 0), surveyChoiceOrder(choices, hasOpenChoice, true)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `small and duplicate lists retain every original index`() { | ||
| for (choices in listOf(emptyList(), listOf("Other"), listOf("A", "Other"), listOf("A", "A", "Other"))) { | ||
| val order = surveyChoiceOrder(choices, hasOpenChoice, true) | ||
| assertEquals(choices.indices.toList(), order.sorted()) | ||
| if (hasOpenChoice && choices.isNotEmpty()) assertEquals(choices.lastIndex, order.last()) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| @JvmStatic | ||
| @Parameterized.Parameters(name = "open={0}") | ||
| fun cases(): List<Array<Boolean>> = listOf(arrayOf(false), arrayOf(true)) | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
...ebug/java/com/posthog/android/surveys/compose/internal/ui/SurveyShuffleInteractionTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package com.posthog.android.surveys.compose.internal.ui | ||
|
|
||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.ui.test.assertIsDisplayed | ||
| import androidx.compose.ui.test.hasSetTextAction | ||
| import androidx.compose.ui.test.junit4.createComposeRule | ||
| import androidx.compose.ui.test.onNodeWithText | ||
| import androidx.compose.ui.test.performClick | ||
| import androidx.compose.ui.test.performTextInput | ||
| import com.posthog.surveys.PostHogDisplayChoiceQuestion | ||
| import com.posthog.surveys.PostHogDisplaySurvey | ||
| import com.posthog.surveys.PostHogDisplaySurveyAppearance | ||
| import com.posthog.surveys.PostHogDisplaySurveyTextContentType | ||
| import com.posthog.surveys.PostHogNextSurveyQuestion | ||
| import com.posthog.surveys.PostHogSurveyResponse | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.ParameterizedRobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNotEquals | ||
|
|
||
| @RunWith(ParameterizedRobolectricTestRunner::class) | ||
| @Config(sdk = [28]) | ||
| internal class SurveyShuffleInteractionTest( | ||
| private val multiple: Boolean, | ||
| private val open: Boolean, | ||
| private val shuffle: Boolean, | ||
| ) { | ||
| @get:Rule val compose = createComposeRule() | ||
|
|
||
| @Test | ||
| fun `display order stays stable through selection and submits the displayed answer`() { | ||
| val choices = listOf("A", "B", "C") + if (open) listOf("Other") else emptyList() | ||
| val labels = choices.map { if (open && it == "Other") "Other:" else it } | ||
| val responses = mutableListOf<PostHogSurveyResponse>() | ||
| val survey = | ||
| PostHogDisplaySurvey( | ||
| id = "shuffle", | ||
| name = "Shuffle", | ||
| questions = listOf(question("First", choices), question("Last", listOf("Done"))), | ||
| appearance = PostHogDisplaySurveyAppearance(displayThankYouMessage = false), | ||
| ) | ||
| compose.setContent { | ||
| MaterialTheme { | ||
| SurveySheet(survey, onSurveyShown = {}, onSubmit = { _, response -> | ||
| responses.add(response) | ||
| PostHogNextSurveyQuestion(1, false) | ||
| }, onClose = {}) | ||
| } | ||
| } | ||
|
|
||
| fun visibleOrder() = labels.sortedBy { compose.onNodeWithText(it).fetchSemanticsNode().boundsInRoot.top } | ||
| val order = visibleOrder() | ||
| if (shuffle) assertNotEquals(labels, order) else assertEquals(labels, order) | ||
| if (open) assertEquals("Other:", order.last()) | ||
|
|
||
| compose.onNodeWithText("B").performClick() | ||
| assertEquals(order, visibleOrder()) | ||
| if (open) { | ||
| compose.onNodeWithText("Other:").performClick() | ||
| compose.onNode(hasSetTextAction()).performTextInput("Custom answer") | ||
| assertEquals(order, visibleOrder()) | ||
| } | ||
| compose.onNodeWithText("Submit").performClick() | ||
| compose.onNodeWithText("Last").assertIsDisplayed() | ||
| compose.onNodeWithText("Done").assertIsDisplayed() | ||
| compose.runOnIdle { | ||
| assertEquals(listOf(expectedResponse()), responses) | ||
| } | ||
| } | ||
|
|
||
| private fun expectedResponse(): PostHogSurveyResponse = | ||
| if (multiple) { | ||
| PostHogSurveyResponse.MultipleChoice(if (open) listOf("B", "Custom answer") else listOf("B")) | ||
| } else { | ||
| PostHogSurveyResponse.SingleChoice(if (open) "Custom answer" else "B") | ||
| } | ||
|
|
||
| private fun question( | ||
| id: String, | ||
| choices: List<String>, | ||
| ) = PostHogDisplayChoiceQuestion( | ||
| id = id, question = id, questionDescription = null, | ||
| questionDescriptionContentType = PostHogDisplaySurveyTextContentType.TEXT, | ||
| isOptional = false, buttonText = "Submit", choices = choices, | ||
| hasOpenChoice = open && id == "First", shuffleOptions = shuffle, isMultipleChoice = multiple, | ||
| ) | ||
|
|
||
| companion object { | ||
| @JvmStatic | ||
| @ParameterizedRobolectricTestRunner.Parameters(name = "multiple={0}, open={1}, shuffle={2}") | ||
| fun cases(): List<Array<Boolean>> = | ||
| listOf(false, true).flatMap { multiple -> | ||
| listOf(false, true).flatMap { open -> listOf(false, true).map { shuffle -> arrayOf(multiple, open, shuffle) } } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameters
multiple,open, andshuffledo not clearly communicate that they control multiple-choice mode, open-choice support, and option shuffling. This violates the repository directive to use variable names that clearly indicate their purpose. Rename them to descriptive names such asisMultipleChoice,hasOpenChoice, andshouldShuffleOptions; this repository requirement must be satisfied before merging.Rule Used: Use descriptive variable names that clearly indica... (source)
Learned From
PostHog/posthog#32928
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!