Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Jan 7, 2025
1 parent b9eff64 commit 701086a
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentsheet.R
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -34,7 +33,7 @@ internal class LinkActivityViewModel @Inject constructor(
confirmationHandlerFactory: ConfirmationHandler.Factory,
private val linkAccountManager: LinkAccountManager,
) : ViewModel(), DefaultLifecycleObserver {
val confirmationHandler: ConfirmationHandler
val confirmationHandler: ConfirmationHandler = confirmationHandlerFactory.create(viewModelScope)

private val _linkState = MutableStateFlow(
value = LinkAppBarState(
Expand All @@ -52,22 +51,6 @@ internal class LinkActivityViewModel @Inject constructor(
var navController: NavHostController? = null
var dismissWithResult: ((LinkActivityResult) -> Unit)? = null

init {
confirmationHandler = confirmationHandlerFactory.create(viewModelScope)
viewModelScope.launch {

listenToConfirmationState()
}
}

private suspend fun listenToConfirmationState() {
confirmationHandler.state
.filterIsInstance<ConfirmationHandler.State.Complete>()
.collect {
dismissWithResult?.invoke(LinkActivityResult.Completed)
}
}

fun registerFromActivity(
activityResultCaller: ActivityResultCaller,
lifecycleOwner: LifecycleOwner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal interface LinkConfirmationHandler {
}
}

sealed interface Result {
internal sealed interface Result {
data object Succeeded : Result
data object Canceled : Result
data class Failed(val message: ResolvableString) : Result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ internal interface NativeLinkModule {
@NativeLinkScope
fun stripeRepository(stripeRepository: StripeApiRepository): StripeRepository

@SuppressWarnings("TooManyFunctions")
companion object {
@Provides
@NativeLinkScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,12 @@ internal fun WalletBody(
onPrimaryButtonClick: () -> Unit,
onPayAnotherWayClick: () -> Unit,
) {
val context = LocalContext.current
if (state.paymentDetailsList.isEmpty()) {
Box(
modifier = Modifier
.fillMaxSize()
.testTag(WALLET_LOADER_TAG),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
Loader()
return
}

val context = LocalContext.current
val focusManager = LocalFocusManager.current

LaunchedEffect(state.isProcessing) {
Expand Down Expand Up @@ -144,6 +137,18 @@ internal fun WalletBody(
}
}

@Composable
private fun Loader() {
Box(
modifier = Modifier
.fillMaxSize()
.testTag(WALLET_LOADER_TAG),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}

@Composable
private fun PaymentMethodSection(
state: WalletUiState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal data class WalletUiState(
val isProcessing: Boolean,
val primaryButtonLabel: ResolvableString,
val hasCompleted: Boolean,
val errorMessage: ResolvableString? = null
val errorMessage: ResolvableString?
) {

val showBankAccountTerms = selectedItem is ConsumerPaymentDetails.BankAccount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ internal class WalletViewModel @Inject constructor(
selectedItem = null,
isProcessing = false,
hasCompleted = false,
primaryButtonLabel = completePaymentButtonLabel(configuration.stripeIntent)
primaryButtonLabel = completePaymentButtonLabel(configuration.stripeIntent),
errorMessage = null
)
)

Expand Down Expand Up @@ -95,22 +96,26 @@ internal class WalletViewModel @Inject constructor(
fun onPrimaryButtonClicked() {
val selectedItem = uiState.value.selectedItem ?: return
viewModelScope.launch {
_uiState.update {
it.copy(isProcessing = true)
}
val result = linkConfirmationHandler.confirm(
paymentDetails = selectedItem,
linkAccount = linkAccount
)
when (result) {
Result.Canceled -> {
dismissWithResult(LinkActivityResult.Canceled(LinkActivityResult.Canceled.Reason.BackPressed))
}
Result.Canceled -> Unit
is Result.Failed -> {
_uiState.update {
it.copy(
errorMessage = result.message
errorMessage = result.message,
isProcessing = false
)
}
}
Result.Succeeded -> Unit
Result.Succeeded -> {
dismissWithResult(LinkActivityResult.Completed)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.stripe.android.link

import android.app.Application
import androidx.activity.result.ActivityResultCaller
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.SAVED_STATE_REGISTRY_OWNER_KEY
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.VIEW_MODEL_STORE_OWNER_KEY
Expand All @@ -20,6 +22,8 @@ import com.google.common.truth.Truth.assertThat
import com.stripe.android.link.account.FakeLinkAccountManager
import com.stripe.android.link.account.LinkAccountManager
import com.stripe.android.link.model.AccountStatus
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentelement.confirmation.FakeConfirmationHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
Expand Down Expand Up @@ -319,14 +323,34 @@ internal class LinkActivityViewModelTest {
}
}

fun `vm register starts confirmation handler registration`() = runTest(dispatcher) {
val activityResultCaller: ActivityResultCaller = mock()
val lifecycleOwner: LifecycleOwner = mock()
val confirmationHandler = FakeConfirmationHandler()
val vm = createViewModel(
confirmationHandler = confirmationHandler
)

confirmationHandler.registerTurbine.ensureAllEventsConsumed()
vm.registerFromActivity(activityResultCaller, lifecycleOwner)

val registerCall = confirmationHandler.registerTurbine.awaitItem()

assertThat(registerCall).isEqualTo(FakeConfirmationHandler.RegisterCall(activityResultCaller, lifecycleOwner))
}

private fun createViewModel(
linkAccountManager: LinkAccountManager = FakeLinkAccountManager(),
confirmationHandler: ConfirmationHandler = FakeConfirmationHandler(),
navController: NavHostController = navController(),
dismissWithResult: (LinkActivityResult) -> Unit = {}
): LinkActivityViewModel {
return LinkActivityViewModel(
linkAccountManager = linkAccountManager,
activityRetainedComponent = mock(),
confirmationHandlerFactory = {
confirmationHandler
}
).apply {
this.navController = navController
this.dismissWithResult = dismissWithResult
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.stripe.android.link.confirmation

import com.stripe.android.link.model.LinkAccount
import com.stripe.android.model.ConsumerPaymentDetails

internal open class FakeLinkConfirmationHandler : LinkConfirmationHandler {
var result: Result = Result.Succeeded
override suspend fun confirm(
paymentDetails: ConsumerPaymentDetails.PaymentDetails,
linkAccount: LinkAccount
) = result
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ internal class WalletScreenScreenshotTest {
selectedItem = null,
isProcessing = false,
hasCompleted = false,
primaryButtonLabel = primaryButtonLabel
primaryButtonLabel = primaryButtonLabel,
errorMessage = null
)
)
}
Expand All @@ -37,7 +38,8 @@ internal class WalletScreenScreenshotTest {
selectedItem = TestFactory.CONSUMER_PAYMENT_DETAILS.paymentDetails.firstOrNull(),
isProcessing = false,
hasCompleted = false,
primaryButtonLabel = primaryButtonLabel
primaryButtonLabel = primaryButtonLabel,
errorMessage = null
)
)
}
Expand All @@ -50,7 +52,8 @@ internal class WalletScreenScreenshotTest {
selectedItem = TestFactory.CONSUMER_PAYMENT_DETAILS.paymentDetails.firstOrNull(),
isProcessing = false,
hasCompleted = false,
primaryButtonLabel = primaryButtonLabel
primaryButtonLabel = primaryButtonLabel,
errorMessage = null
),
isExpanded = true
)
Expand All @@ -64,7 +67,8 @@ internal class WalletScreenScreenshotTest {
selectedItem = TestFactory.CONSUMER_PAYMENT_DETAILS.paymentDetails.firstOrNull(),
isProcessing = false,
hasCompleted = false,
primaryButtonLabel = primaryButtonLabel
primaryButtonLabel = primaryButtonLabel,
errorMessage = null
),
isExpanded = true
)
Expand All @@ -78,7 +82,8 @@ internal class WalletScreenScreenshotTest {
selectedItem = TestFactory.CONSUMER_PAYMENT_DETAILS.paymentDetails.firstOrNull(),
isProcessing = true,
hasCompleted = false,
primaryButtonLabel = primaryButtonLabel
primaryButtonLabel = primaryButtonLabel,
errorMessage = null
),
isExpanded = true
)
Expand All @@ -99,7 +104,8 @@ internal class WalletScreenScreenshotTest {
selectedItem = paymentDetailsList.firstOrNull(),
isProcessing = false,
hasCompleted = false,
primaryButtonLabel = primaryButtonLabel
primaryButtonLabel = primaryButtonLabel,
errorMessage = null
),
isExpanded = true
)
Expand All @@ -120,7 +126,8 @@ internal class WalletScreenScreenshotTest {
selectedItem = paymentDetailsList.firstOrNull(),
isProcessing = false,
hasCompleted = false,
primaryButtonLabel = primaryButtonLabel
primaryButtonLabel = primaryButtonLabel,
errorMessage = null
),
isExpanded = true
)
Expand All @@ -136,7 +143,8 @@ internal class WalletScreenScreenshotTest {
},
isProcessing = false,
hasCompleted = false,
primaryButtonLabel = primaryButtonLabel
primaryButtonLabel = primaryButtonLabel,
errorMessage = null
),
isExpanded = true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.stripe.android.link.TestFactory
import com.stripe.android.link.account.FakeLinkAccountManager
import com.stripe.android.link.account.LinkAccountManager
import com.stripe.android.link.confirmation.FakeLinkConfirmationHandler
import com.stripe.android.link.ui.PrimaryButtonTag
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.testing.FakeLogger
Expand All @@ -25,7 +26,6 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock

@RunWith(AndroidJUnit4::class)
internal class WalletScreenTest {
Expand Down Expand Up @@ -175,7 +175,7 @@ internal class WalletScreenTest {
linkAccount = TestFactory.LINK_ACCOUNT,
linkAccountManager = linkAccountManager,
logger = FakeLogger(),
confirmationHandler = mock(),
linkConfirmationHandler = FakeLinkConfirmationHandler(),
navigateAndClearStack = {},
dismissWithResult = {}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class WalletUiStateTest {
selectedItem = TestFactory.CONSUMER_PAYMENT_DETAILS.paymentDetails.firstOrNull(),
hasCompleted = true,
isProcessing = false,
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL,
errorMessage = null
)

assertThat(state.primaryButtonState).isEqualTo(PrimaryButtonState.Completed)
Expand All @@ -29,7 +30,8 @@ class WalletUiStateTest {
selectedItem = TestFactory.CONSUMER_PAYMENT_DETAILS.paymentDetails.firstOrNull(),
hasCompleted = false,
isProcessing = true,
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL,
errorMessage = null
)

assertThat(state.primaryButtonState).isEqualTo(PrimaryButtonState.Processing)
Expand All @@ -45,7 +47,8 @@ class WalletUiStateTest {
),
hasCompleted = false,
isProcessing = false,
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL,
errorMessage = null
)

assertThat(state.primaryButtonState).isEqualTo(PrimaryButtonState.Disabled)
Expand All @@ -61,7 +64,8 @@ class WalletUiStateTest {
),
hasCompleted = false,
isProcessing = false,
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL,
errorMessage = null
)

assertThat(state.primaryButtonState).isEqualTo(PrimaryButtonState.Disabled)
Expand All @@ -75,7 +79,8 @@ class WalletUiStateTest {
.copy(expiryYear = 2099),
hasCompleted = false,
isProcessing = false,
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL,
errorMessage = null
)

assertThat(state.primaryButtonState).isEqualTo(PrimaryButtonState.Enabled)
Expand All @@ -88,7 +93,8 @@ class WalletUiStateTest {
selectedItem = TestFactory.CONSUMER_PAYMENT_DETAILS_BANK_ACCOUNT,
hasCompleted = false,
isProcessing = false,
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL,
errorMessage = null
)

assertThat(state.showBankAccountTerms).isTrue()
Expand All @@ -101,7 +107,8 @@ class WalletUiStateTest {
selectedItem = TestFactory.CONSUMER_PAYMENT_DETAILS_CARD,
hasCompleted = false,
isProcessing = false,
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL
primaryButtonLabel = LINK_WALLET_PRIMARY_BUTTON_LABEL,
errorMessage = null
)

assertThat(state.showBankAccountTerms).isFalse()
Expand Down
Loading

0 comments on commit 701086a

Please sign in to comment.