Skip to content

Commit

Permalink
Fix states when going back from autocomplete screen (#5324)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswoo-stripe authored Jul 25, 2022
1 parent 92ba1a4 commit 3b502cb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ internal fun AutocompleteScreenUI(viewModel: AutocompleteViewModel) {
modifier = Modifier.fillMaxWidth()
) {
AddressOptionsAppBar(false) {
viewModel.setResultAndGoBack()
viewModel.onBackPressed()
}
Box(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,39 @@ internal class AutocompleteViewModel @Inject constructor(
}
}

fun onBackPressed() {
val result = if (queryFlow.value.isNotBlank()) {
AddressDetails(
line1 = queryFlow.value
)
} else {
null
}
setResultAndGoBack(result)
}

fun onEnterAddressManually() {
setResultAndGoBack()
setResultAndGoBack(
AddressDetails(
line1 = queryFlow.value
)
)
}

fun setResultAndGoBack() {
addressResult.value?.fold(
onSuccess = {
navigator.setResult(AddressDetails.KEY, it)
},
onFailure = {
navigator.setResult(AddressDetails.KEY, null)
}
) ?: run {
navigator.setResult(AddressDetails.KEY, AddressDetails())
private fun setResultAndGoBack(addressDetails: AddressDetails? = null) {
if (addressDetails != null) {
navigator.setResult(AddressDetails.KEY, addressDetails)
} else {
addressResult.value?.fold(
onSuccess = {
navigator.setResult(AddressDetails.KEY, it)
},
onFailure = {
navigator.setResult(AddressDetails.KEY, null)
}
)
}

navigator.onBack()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.stub
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
Expand Down Expand Up @@ -133,16 +134,11 @@ class AutocompleteViewModelTest {
val viewModel = createViewModel()
val expectedResult = Result.success(
AddressDetails(
city = "city",
country = null,
line1 = "",
line2 = null,
postalCode = null,
state = null
line1 = "Some query"
)
)

viewModel.addressResult.value = expectedResult
viewModel.textFieldController.onRawValueChange("Some query")
viewModel.onEnterAddressManually()

verify(navigator).setResult(anyOrNull(), eq(expectedResult.getOrNull()))
Expand All @@ -153,7 +149,9 @@ class AutocompleteViewModelTest {
fun `onEnterAddressManually navigates back with empty address`() = runTest(UnconfinedTestDispatcher()) {
val viewModel = createViewModel()
val expectedResult = Result.success(
AddressDetails()
AddressDetails(
line1 = ""
)
)

viewModel.onEnterAddressManually()
Expand Down Expand Up @@ -223,4 +221,24 @@ class AutocompleteViewModelTest {

assertThat(viewModel.textFieldController.trailingIcon.stateIn(viewModel.viewModelScope).value).isNotNull()
}

@Test
fun `when query is not empty then return line1 on back`() = runTest(UnconfinedTestDispatcher()) {
val viewModel = createViewModel()

viewModel.textFieldController.onRawValueChange("a")
viewModel.onBackPressed()

verify(viewModel.navigator).setResult(eq(AddressDetails.KEY), eq(AddressDetails(line1 = "a")))
}

@Test
fun `when query is empty then do nothing on back`() = runTest(UnconfinedTestDispatcher()) {
val viewModel = createViewModel()

viewModel.textFieldController.onRawValueChange("")
viewModel.onBackPressed()

verify(viewModel.navigator, never()).setResult(any(), any())
}
}

0 comments on commit 3b502cb

Please sign in to comment.