Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Dec 18, 2024
1 parent a181ebb commit 8b305cd
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ internal fun createLinkActivityResult(resultCode: Int, intent: Intent?): LinkAct
}

LinkActivity.RESULT_COMPLETE -> {
intent?.extras?.let {
BundleCompat.getParcelable(it, LinkActivityContract.EXTRA_RESULT, LinkActivityResult::class.java)
} ?: LinkActivityResult.Canceled()
handleNativeLinkResult(intent)
}

else -> {
Expand All @@ -101,6 +99,13 @@ internal fun createLinkActivityResult(resultCode: Int, intent: Intent?): LinkAct
}
}

private fun handleNativeLinkResult(intent: Intent?): LinkActivityResult {
val result = intent?.extras?.let {
BundleCompat.getParcelable(it, LinkActivityContract.EXTRA_RESULT, LinkActivityResult::class.java)
}
return result ?: LinkActivityResult.Canceled()
}

private fun String.parsePaymentMethod(): PaymentMethod? = try {
val decodedPaymentMethod = String(Base64.decode(this, 0), Charsets.UTF_8)
val paymentMethod = PaymentMethodJsonParser()
Expand Down
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 @@ -15,6 +17,8 @@ import androidx.savedstate.SavedStateRegistryOwner
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.stripe.android.link.account.FakeLinkAccountManager
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.resetMain
Expand All @@ -35,7 +39,11 @@ import kotlin.test.assertFailsWith
internal class LinkActivityViewModelTest {
private val dispatcher = UnconfinedTestDispatcher()
private val linkAccountManager = FakeLinkAccountManager()
private val vm = LinkActivityViewModel(mock(), mock(), linkAccountManager)
val confirmationHandler = FakeConfirmationHandler()
private val confirmationHandlerFactory: ConfirmationHandler.Factory = ConfirmationHandler.Factory {
confirmationHandler
}
private val vm = LinkActivityViewModel(mock(), confirmationHandlerFactory, linkAccountManager)
private val navController: NavHostController = mock()
private val dismissWithResult: (LinkActivityResult) -> Unit = mock()

Expand Down Expand Up @@ -119,6 +127,19 @@ internal class LinkActivityViewModelTest {
assertThat(vm.linkAccount).isEqualTo(TestFactory.LINK_ACCOUNT)
}

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

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

val registerCall = confirmationHandler.registerTurbine.awaitItem()

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

private fun creationExtras(): CreationExtras {
val mockOwner = mock<SavedStateRegistryOwner>()
val mockViewModelStoreOwner = mock<ViewModelStoreOwner>()
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
Loading

0 comments on commit 8b305cd

Please sign in to comment.