Skip to content

Commit

Permalink
Add test for selecting saved PM in CustomerSheet. (#8259)
Browse files Browse the repository at this point in the history
* Add test for selecting saved PM in `CustomerSheet`.

* Clear preferences before confirming selection in `CustomerSheetTest`
  • Loading branch information
samer-stripe committed Apr 12, 2024
1 parent a6f60e4 commit 2f39506
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.test.espresso.Espresso
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.stripe.android.customersheet.ui.CUSTOMER_SHEET_CONFIRM_BUTTON_TEST_TAG
import com.stripe.android.customersheet.ui.CUSTOMER_SHEET_SAVE_BUTTON_TEST_TAG
import com.stripe.android.paymentsheet.ui.SAVED_PAYMENT_OPTION_TEST_TAG
import com.stripe.android.uicore.elements.DROPDOWN_MENU_CLICKABLE_TEST_TAG

internal class CustomerSheetPage(
Expand Down Expand Up @@ -65,6 +66,14 @@ internal class CustomerSheetPage(
clickPrimaryButton(CUSTOMER_SHEET_CONFIRM_BUTTON_TEST_TAG)
}

fun clickSavedPaymentMethod(endsWith: String) {
val savedPaymentMethodMatcher = hasTestTag(SAVED_PAYMENT_OPTION_TEST_TAG)
.and(hasText(endsWith, substring = true))

waitUntil(savedPaymentMethodMatcher)
click(savedPaymentMethodMatcher)
}

private fun clickPrimaryButton(tag: String) {
waitUntil(hasTestTag(tag).and(isEnabled()))
click(hasTestTag(tag))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.stripe.android.core.utils.urlEncode
import com.stripe.android.customersheet.CustomerSheet
import com.stripe.android.customersheet.CustomerSheetResult
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi
import com.stripe.android.customersheet.PaymentOptionSelection
import com.stripe.android.model.CardBrand
import com.stripe.android.networktesting.NetworkRule
import com.stripe.android.networktesting.RequestMatcher
import com.stripe.android.networktesting.RequestMatchers
Expand All @@ -21,6 +23,7 @@ import com.stripe.android.paymentsheet.utils.CustomerSheetTestType
import com.stripe.android.paymentsheet.utils.CustomerSheetTestTypeProvider
import com.stripe.android.paymentsheet.utils.IntegrationType
import com.stripe.android.paymentsheet.utils.IntegrationTypeProvider
import com.stripe.android.paymentsheet.utils.PrefsTestStore
import com.stripe.android.paymentsheet.utils.runCustomerSheetTest
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -91,6 +94,55 @@ internal class CustomerSheetTest {
page.clickConfirmButton()
}

@Test
fun testSavedCardReturnedInResultCallback() = activityScenarioRule.runCustomerSheetTest(
integrationType = integrationType,
customerSheetTestType = CustomerSheetTestType.AttachToSetupIntent,
resultCallback = { result ->
assertThat(result).isInstanceOf(CustomerSheetResult.Selected::class.java)

val selected = result as CustomerSheetResult.Selected

assertThat(selected.selection).isInstanceOf(PaymentOptionSelection.PaymentMethod::class.java)

val paymentMethodSelection = selected.selection as PaymentOptionSelection.PaymentMethod

val card = paymentMethodSelection.paymentMethod.card

assertThat(card?.last4).isEqualTo("4242")
assertThat(card?.brand).isEqualTo(CardBrand.Visa)
}
) { context ->
context.scenario.onActivity {
PrefsTestStore(it).clear()
}

networkRule.enqueue(
retrieveElementsSessionRequest(),
) { response ->
response.testBodyFromFile("elements-sessions-requires_payment_method.json")
}

networkRule.enqueue(
retrievePaymentMethodsRequest(),
cardPaymentMethodsParams(),
) { response ->
response.testBodyFromFile("payment-methods-get-success.json")
}

networkRule.enqueue(
retrievePaymentMethodsRequest(),
usBankAccountPaymentMethodsParams(),
) { response ->
response.testBodyFromFile("payment-methods-get-success-empty.json")
}

context.presentCustomerSheet()

page.clickSavedPaymentMethod(endsWith = "4242")
page.clickConfirmButton()
}

@Test
fun testSuccessfulCardSaveWithFullBillingDetailsCollection(
@TestParameter(valuesProvider = CustomerSheetTestTypeProvider::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit

@OptIn(ExperimentalCustomerSheetApi::class)
internal class CustomerSheetTestRunnerContext(
private val scenario: ActivityScenario<MainActivity>,
internal val scenario: ActivityScenario<MainActivity>,
private val customerSheet: CustomerSheet,
private val countDownLatch: CountDownLatch,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.stripe.android.paymentsheet.utils

import android.content.Context
import android.content.SharedPreferences
import com.stripe.android.paymentsheet.DefaultPrefsRepository

class PrefsTestStore(
context: Context,
) {
private val prefs: SharedPreferences by lazy {
context.getSharedPreferences(DefaultPrefsRepository.PREF_FILE, Context.MODE_PRIVATE)
}

fun clear() {
prefs.edit().clear().apply()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"object": "list",
"data": [
{
"id": "pm_12345",
"object": "payment_method",
"billing_details": {
"address": {
"city": null,
"country": "US",
"line1": null,
"line2": null,
"postal_code": "12342",
"state": null
},
"email": null,
"name": null,
"phone": null
},
"card": {
"brand": "visa",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": null
},
"country": "US",
"display_brand": "visa",
"exp_month": 12,
"exp_year": 2035,
"fingerprint": "IMwEhnDXJXJa55oU",
"funding": "debit",
"generated_from": null,
"last4": "5556",
"networks": {
"available": [
"visa"
],
"preferred": null
},
"three_d_secure_usage": {
"supported": true
},
"wallet": null
},
"created": 1712746197,
"customer": "cus_1",
"livemode": false,
"type": "card"
},
{
"id": "pm_67890",
"object": "payment_method",
"billing_details": {
"address": {
"city": null,
"country": "AE",
"line1": null,
"line2": null,
"postal_code": null,
"state": null
},
"email": null,
"name": "CustomerSheet Testing",
"phone": null
},
"card": {
"brand": "visa",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": null
},
"country": "US",
"display_brand": "visa",
"exp_month": 12,
"exp_year": 2034,
"fingerprint": "LzD7yFi1Cp2DJQs7",
"funding": "credit",
"generated_from": null,
"last4": "4242",
"networks": {
"available": [
"visa"
],
"preferred": null
},
"three_d_secure_usage": {
"supported": true
},
"wallet": null
},
"created": 1712554485,
"customer": "cus_1",
"livemode": false,
"type": "card"
}
],
"has_more": false,
"url": "/v1/payment_methods"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.model.SavedSelection
import com.stripe.android.paymentsheet.model.toSavedSelection
import kotlinx.coroutines.withContext
import org.jetbrains.annotations.VisibleForTesting
import kotlin.coroutines.CoroutineContext

internal class DefaultPrefsRepository(
Expand Down Expand Up @@ -77,7 +78,8 @@ internal class DefaultPrefsRepository(
return customerId?.let { "customer[$it]" } ?: "guest"
}

private companion object {
private const val PREF_FILE = "DefaultPrefsRepository"
internal companion object {
@VisibleForTesting
internal const val PREF_FILE = "DefaultPrefsRepository"
}
}

0 comments on commit 2f39506

Please sign in to comment.