Skip to content

Commit

Permalink
Add detaching payment method test in CustomerSheetTest
Browse files Browse the repository at this point in the history
  • Loading branch information
samer-stripe committed Apr 16, 2024
1 parent 9ad0c3e commit e4979af
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.paymentsheet

import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isEnabled
Expand Down Expand Up @@ -66,6 +67,28 @@ internal class CustomerSheetPage(
clickPrimaryButton(CUSTOMER_SHEET_CONFIRM_BUTTON_TEST_TAG)
}

fun clickEditButton() {
val editButtonMatcher = hasText("EDIT")

waitUntil(editButtonMatcher)
click(editButtonMatcher, canScroll = false)
}

fun clickDeleteButton(forEndsWith: String) {
val deleteBadgeForSavedPmMatcher = hasTestTag(PAYMENT_OPTION_DELETE_BADGE_TEST_TAG)
.and(hasContentDescription(forEndsWith, substring = true))

waitUntil(deleteBadgeForSavedPmMatcher)
click(deleteBadgeForSavedPmMatcher)
}

fun clickDialogRemoveButton() {
val dialogRemoveButtonMatcher = hasText("Remove")

waitUntil(dialogRemoveButtonMatcher)
click(dialogRemoveButtonMatcher, canScroll = false)
}

fun clickSavedPaymentMethod(endsWith: String) {
val savedPaymentMethodMatcher = hasTestTag(SAVED_PAYMENT_OPTION_TEST_TAG)
.and(hasText(endsWith, substring = true))
Expand Down Expand Up @@ -95,10 +118,14 @@ internal class CustomerSheetPage(
}
}

private fun click(matcher: SemanticsMatcher) {
composeTestRule.onNode(matcher)
.performScrollTo()
.performClick()
private fun click(matcher: SemanticsMatcher, canScroll: Boolean = true) {
val clickableNode = composeTestRule.onNode(matcher)

if (canScroll) {
clickableNode.performScrollTo()
}

clickableNode.performClick()
}

private fun replaceText(label: String, text: String, isLabelSubstring: Boolean = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,64 @@ internal class CustomerSheetTest {
page.clickConfirmButton()
}

@Test
fun testDeleteSavedCard() = 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()

networkRule.enqueue(
detachRequest(),
) { response ->
response.testBodyFromFile("payment-methods-detach.json")
}

page.clickEditButton()
page.clickDeleteButton(forEndsWith = "4242")
page.clickDialogRemoveButton()

context.markTestSucceeded()
}

@Test
fun testSuccessfulCardSaveWithFullBillingDetailsCollection(
@TestParameter(valuesProvider = CustomerSheetTestTypeProvider::class)
Expand Down Expand Up @@ -400,6 +458,14 @@ internal class CustomerSheetTest {
)
}

private fun detachRequest(): RequestMatcher {
return RequestMatchers.composite(
host("api.stripe.com"),
method("POST"),
path("/v1/payment_methods/pm_67890/detach")
)
}

private fun retrieveSetupIntentParams(): RequestMatcher {
return RequestMatchers.composite(
query("client_secret", "seti_12345_secret_12345"),
Expand Down
47 changes: 47 additions & 0 deletions paymentsheet/src/androidTest/resources/payment-methods-detach.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"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": null,
"livemode": false,
"type": "card"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.paymentsheet

import androidx.annotation.DrawableRes
import androidx.annotation.RestrictTo
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
Expand Down Expand Up @@ -165,7 +166,9 @@ private fun PaymentOptionBadge(
PaymentOptionEditState.Removable -> RemoveBadge(
onRemoveAccessibilityDescription = onRemoveAccessibilityDescription,
onPressed = { openRemoveDialog.value = true },
modifier = Modifier.offset(x = (-14).dp, y = 1.dp),
modifier = Modifier
.offset(x = (-14).dp, y = 1.dp)
.testTag(PAYMENT_OPTION_DELETE_BADGE_TEST_TAG),
)
PaymentOptionEditState.None -> Unit
}
Expand Down Expand Up @@ -354,3 +357,6 @@ private fun PaymentOptionUiModifiable() {
)
}
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
const val PAYMENT_OPTION_DELETE_BADGE_TEST_TAG = "PaymentOptionDeleteBadge"

0 comments on commit e4979af

Please sign in to comment.