diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountEmitters.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountEmitters.kt index 6eb5a1ebcb9..5873d7140ab 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountEmitters.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountEmitters.kt @@ -21,7 +21,9 @@ internal fun USBankAccountEmitters( LaunchedEffect(Unit) { viewModel.result.collect { result -> - usBankAccountFormArgs.onHandleUSBankAccount(result) + result?.let { + usBankAccountFormArgs.onHandleUSBankAccount(result) + } } } diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModel.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModel.kt index d6cc6097919..ad312b8fc05 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModel.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModel.kt @@ -188,8 +188,8 @@ internal class USBankAccountFormViewModel @Inject internal constructor( flowOf(null) } - private val _result = MutableSharedFlow(replay = 1) - val result: Flow = _result + private val _result = MutableSharedFlow(replay = 1) + val result: Flow = _result private val defaultSaveForFutureUse: Boolean = args.savedPaymentMethod?.input?.saveForFutureUse ?: false @@ -348,8 +348,10 @@ internal class USBankAccountFormViewModel @Inject internal constructor( } fun onDestroy() { + _result.tryEmit(null) collectBankAccountLauncher?.unregister() collectBankAccountLauncher = null + reset() } fun formattedMerchantName(): String { diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModelTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModelTest.kt index 7c90b7dff5d..4e960a93849 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModelTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModelTest.kt @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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 {