Skip to content

Commit

Permalink
Fix issue with emitting result after destroying form in USBankAccount…
Browse files Browse the repository at this point in the history
…Form (#7521)

* Fix issue with emitting result after destroying form
  • Loading branch information
jameswoo-stripe authored Oct 27, 2023
1 parent 66851a9 commit 95501ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ internal fun USBankAccountEmitters(

LaunchedEffect(Unit) {
viewModel.result.collect { result ->
usBankAccountFormArgs.onHandleUSBankAccount(result)
result?.let {
usBankAccountFormArgs.onHandleUSBankAccount(result)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
flowOf(null)
}

private val _result = MutableSharedFlow<PaymentSelection.New.USBankAccount>(replay = 1)
val result: Flow<PaymentSelection.New.USBankAccount> = _result
private val _result = MutableSharedFlow<PaymentSelection.New.USBankAccount?>(replay = 1)
val result: Flow<PaymentSelection.New.USBankAccount?> = _result

private val defaultSaveForFutureUse: Boolean =
args.savedPaymentMethod?.input?.saveForFutureUse ?: false
Expand Down Expand Up @@ -348,8 +348,10 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
}

fun onDestroy() {
_result.tryEmit(null)
collectBankAccountLauncher?.unregister()
collectBankAccountLauncher = null
reset()
}

fun formattedMerchantName(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class USBankAccountFormViewModelTest {
val currentScreenState = viewModel.currentScreenState.stateIn(viewModel.viewModelScope).value
viewModel.handlePrimaryButtonClick(currentScreenState as USBankAccountFormScreenState.VerifyWithMicrodeposits)

assertThat(awaitItem().screenState).isEqualTo(currentScreenState)
assertThat(awaitItem()?.screenState).isEqualTo(currentScreenState)
}
}

Expand All @@ -163,7 +163,7 @@ class USBankAccountFormViewModelTest {
val currentScreenState = viewModel.currentScreenState.stateIn(viewModel.viewModelScope).value
viewModel.handlePrimaryButtonClick(currentScreenState as USBankAccountFormScreenState.MandateCollection)

assertThat(awaitItem().screenState).isEqualTo(currentScreenState)
assertThat(awaitItem()?.screenState).isEqualTo(currentScreenState)
}
}

Expand All @@ -178,7 +178,7 @@ class USBankAccountFormViewModelTest {
val currentScreenState = viewModel.currentScreenState.stateIn(viewModel.viewModelScope).value
viewModel.handlePrimaryButtonClick(currentScreenState as USBankAccountFormScreenState.VerifyWithMicrodeposits)

assertThat(awaitItem().screenState).isEqualTo(currentScreenState)
assertThat(awaitItem()?.screenState).isEqualTo(currentScreenState)
}
}

Expand All @@ -193,7 +193,7 @@ class USBankAccountFormViewModelTest {
val currentScreenState = viewModel.currentScreenState.stateIn(viewModel.viewModelScope).value
viewModel.handlePrimaryButtonClick(currentScreenState as USBankAccountFormScreenState.MandateCollection)

assertThat(awaitItem().screenState).isEqualTo(currentScreenState)
assertThat(awaitItem()?.screenState).isEqualTo(currentScreenState)
}
}

Expand Down Expand Up @@ -813,6 +813,28 @@ class USBankAccountFormViewModelTest {
)
}

@Test
fun `When form destroyed, collect bank account result is null and reset to billing collection screen`() = runTest {
val viewModel = createViewModel(
defaultArgs.copy(
clientSecret = null,
isPaymentFlow = false
)
)

viewModel.result.test {
viewModel.onDestroy()

assertThat(awaitItem()).isNull()

val currentScreenState =
viewModel.currentScreenState.stateIn(viewModel.viewModelScope).value

assertThat(currentScreenState)
.isInstanceOf(USBankAccountFormScreenState.BillingDetailsCollection::class.java)
}
}

private fun createViewModel(
args: USBankAccountFormViewModel.Args = defaultArgs
): USBankAccountFormViewModel {
Expand Down

0 comments on commit 95501ae

Please sign in to comment.