Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tjclawson-stripe committed Feb 11, 2025
1 parent 37b8c99 commit 9262a81
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.stripe.android.paymentelement.embedded.form

import com.stripe.android.common.model.asCommonConfiguration
import com.stripe.android.core.strings.resolvableString
import com.stripe.android.model.PaymentIntentFixtures
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentelement.confirmation.toConfirmationOption
import com.stripe.android.paymentelement.embedded.content.EmbeddedConfirmationStateFixtures
import com.stripe.android.paymentsheet.model.PaymentSelection

@OptIn(ExperimentalEmbeddedPaymentElementApi::class)
internal fun confirmationStateConfirming(selection: PaymentSelection): ConfirmationHandler.State.Confirming {
val confirmationOption = selection.toConfirmationOption(
configuration = EmbeddedConfirmationStateFixtures.defaultState().configuration.asCommonConfiguration(),
linkConfiguration = null
)
return ConfirmationHandler.State.Confirming(requireNotNull(confirmationOption))
}

internal fun confirmationStateComplete(succeeded: Boolean): ConfirmationHandler.State.Complete {
val result = if (succeeded) {
ConfirmationHandler.Result.Succeeded(
PaymentIntentFixtures.PI_SUCCEEDED,
null
)
} else {
ConfirmationHandler.Result.Failed(
cause = Throwable(),
message = "Something went wrong".resolvableString,
type = ConfirmationHandler.Result.Failed.ErrorType.Internal
)
}
return ConfirmationHandler.State.Complete(result)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.TurbineTestContext
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import com.stripe.android.common.model.asCommonConfiguration
import com.stripe.android.core.strings.resolvableString
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadataFactory
import com.stripe.android.model.PaymentIntentFixtures
Expand All @@ -13,11 +12,8 @@ import com.stripe.android.model.SetupIntentFixtures
import com.stripe.android.model.StripeIntent
import com.stripe.android.paymentelement.EmbeddedPaymentElement
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentelement.confirmation.toConfirmationOption
import com.stripe.android.paymentelement.embedded.EmbeddedSelectionHolder
import com.stripe.android.paymentelement.embedded.content.EmbeddedConfirmationStateFixtures
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.ui.PrimaryButtonProcessingState
import com.stripe.android.ui.core.R
import kotlinx.coroutines.test.TestScope
Expand Down Expand Up @@ -139,7 +135,7 @@ class DefaultFormActivityStateHelperTest {

stateHolder.update(confirmationStateComplete(false))
val failedState = awaitItem()
assertThat(failedState.error).isEqualTo("Whoops".resolvableString)
assertThat(failedState.error).isEqualTo("Something went wrong".resolvableString)

stateHolder.update(confirmationStateConfirming(PaymentMethodFixtures.CARD_PAYMENT_SELECTION))
val confirmingState = awaitItem()
Expand Down Expand Up @@ -178,28 +174,4 @@ class DefaultFormActivityStateHelperTest {
assertThat(initialState.isEnabled).isFalse()
assertThat(initialState.isProcessing).isFalse()
}

private fun confirmationStateConfirming(selection: PaymentSelection): ConfirmationHandler.State.Confirming {
val confirmationOption = selection.toConfirmationOption(
configuration = EmbeddedConfirmationStateFixtures.defaultState().configuration.asCommonConfiguration(),
linkConfiguration = null
)
return ConfirmationHandler.State.Confirming(requireNotNull(confirmationOption))
}

private fun confirmationStateComplete(succeeded: Boolean): ConfirmationHandler.State.Complete {
val result = if (succeeded) {
ConfirmationHandler.Result.Succeeded(
PaymentIntentFixtures.PI_SUCCEEDED,
null
)
} else {
ConfirmationHandler.Result.Failed(
cause = Throwable(),
message = "Whoops".resolvableString,
type = ConfirmationHandler.Result.Failed.ErrorType.Internal
)
}
return ConfirmationHandler.State.Complete(result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.core.strings.resolvableString
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadataFactory
import com.stripe.android.model.PaymentMethodFixtures
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentelement.embedded.EmbeddedFormHelperFactory
import com.stripe.android.paymentelement.embedded.EmbeddedSelectionHolder
import com.stripe.android.paymentelement.embedded.content.EmbeddedConfirmationStateFixtures
import com.stripe.android.paymentsheet.analytics.FakeEventReporter
import com.stripe.android.paymentsheet.ui.PrimaryButtonProcessingState
import com.stripe.android.paymentsheet.utils.ViewModelStoreOwnerContext
import com.stripe.android.screenshottesting.PaparazziRule
import com.stripe.android.utils.FakeLinkConfigurationCoordinator
Expand All @@ -36,15 +36,18 @@ internal class FormActivityScreenShotTest {
@Test
fun testFormActivity_enabled() {
paparazziRule.snapshot {
TestFormActivityUi()
TestFormActivityUi(
confirmationState = ConfirmationHandler.State.Idle,
enabled = true
)
}
}

@Test
fun testFormActivity_disabled() {
paparazziRule.snapshot {
TestFormActivityUi(
isEnabled = false
confirmationState = ConfirmationHandler.State.Idle
)
}
}
Expand All @@ -53,9 +56,7 @@ internal class FormActivityScreenShotTest {
fun testFormActivity_processing() {
paparazziRule.snapshot {
TestFormActivityUi(
isEnabled = false,
isProcessing = true,
processingState = PrimaryButtonProcessingState.Processing
confirmationState = confirmationStateConfirming(PaymentMethodFixtures.CARD_PAYMENT_SELECTION)
)
}
}
Expand All @@ -64,8 +65,7 @@ internal class FormActivityScreenShotTest {
fun testFormActivity_complete() {
paparazziRule.snapshot {
TestFormActivityUi(
isEnabled = false,
processingState = PrimaryButtonProcessingState.Completed
confirmationState = confirmationStateComplete(true)
)
}
}
Expand All @@ -74,20 +74,16 @@ internal class FormActivityScreenShotTest {
fun testFormActivity_error() {
paparazziRule.snapshot {
TestFormActivityUi(
isEnabled = true,
processingState = PrimaryButtonProcessingState.Idle(null),
error = "Something went wrong"
confirmationState = confirmationStateComplete(false)
)
}
}

@OptIn(ExperimentalEmbeddedPaymentElementApi::class)
@Composable
private fun TestFormActivityUi(
isEnabled: Boolean = true,
processingState: PrimaryButtonProcessingState = PrimaryButtonProcessingState.Idle(null),
isProcessing: Boolean = false,
error: String? = null
confirmationState: ConfirmationHandler.State,
enabled: Boolean = false
) {
val paymentMethodMetadata = PaymentMethodMetadataFactory.create()
val selectionHolder = EmbeddedSelectionHolder(SavedStateHandle())
Expand All @@ -112,6 +108,7 @@ internal class FormActivityScreenShotTest {
formActivityStateHelper = stateHolder,
).create()

stateHolder.update(confirmationState)
val state by stateHolder.state.collectAsState()

ViewModelStoreOwnerContext {
Expand All @@ -120,12 +117,7 @@ internal class FormActivityScreenShotTest {
eventReporter = FakeEventReporter(),
onClick = {},
onProcessingCompleted = {},
state = state.copy(
isEnabled = isEnabled,
isProcessing = isProcessing,
error = error?.resolvableString,
processingState = processingState
),
state = state.copy(isEnabled = enabled),
onDismissed = {}
)
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9262a81

Please sign in to comment.