Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add detaching payment method test in CustomerSheetTest #8279

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fun SimpleDialogElementUI(
},
confirmButton = {
TextButton(
modifier = Modifier.testTag(TEST_TAG_DIALOG_CONFIRM_BUTTON),
onClick = {
onConfirmListener()
}
Expand All @@ -52,8 +53,6 @@ fun SimpleDialogElementUI(
} else {
Color.Unspecified
},
modifier = Modifier
.testTag(TEST_TAG_DIALOG_CONFIRM_BUTTON),
)
}
},
Expand Down
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 All @@ -11,7 +12,11 @@ import androidx.compose.ui.test.performTextReplacement
import androidx.test.espresso.Espresso
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.PAYMENT_SHEET_EDIT_BUTTON_TEST_TAG
import com.stripe.android.paymentsheet.ui.SAVED_PAYMENT_OPTION_TEST_TAG
import com.stripe.android.paymentsheet.ui.TEST_TAG_REMOVE_BADGE
import com.stripe.android.paymentsheet.utils.isPlaced
import com.stripe.android.ui.core.elements.TEST_TAG_DIALOG_CONFIRM_BUTTON
import com.stripe.android.uicore.elements.DROPDOWN_MENU_CLICKABLE_TEST_TAG

internal class CustomerSheetPage(
Expand All @@ -21,6 +26,16 @@ internal class CustomerSheetPage(
waitUntil(hasText(text, substring = substring))
}

fun waitUntilRemoved(text: String, substring: Boolean = false) {
waitForIdle()

composeTestRule.waitUntil(5_000) {
composeTestRule
.onAllNodes(hasText(text, substring).and(isPlaced()))
.fetchSemanticsNodes().isEmpty()
}
}

fun fillOutFullBillingAddress() {
replaceText("Address line 1", ADDRESS_LINE_ONE)
replaceText("Address line 2 (optional)", ADDRESS_LINE_TWO)
Expand Down Expand Up @@ -65,6 +80,28 @@ internal class CustomerSheetPage(
clickPrimaryButton(CUSTOMER_SHEET_CONFIRM_BUTTON_TEST_TAG)
}

fun clickEditButton() {
val editButtonMatcher = hasTestTag(PAYMENT_SHEET_EDIT_BUTTON_TEST_TAG)

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

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

waitUntil(deleteBadgeForSavedPmMatcher)
click(deleteBadgeForSavedPmMatcher)
}

fun clickDialogRemoveButton() {
val dialogRemoveButtonMatcher = hasTestTag(TEST_TAG_DIALOG_CONFIRM_BUTTON)

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 @@ -94,10 +131,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,67 @@ internal class CustomerSheetTest {
page.clickConfirmButton()
}

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

page.waitUntilRemoved(text = "4242", substring = true)

context.markTestSucceeded()
samer-stripe marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
fun testSuccessfulCardSaveWithFullBillingDetailsCollection(
@TestParameter(valuesProvider = CustomerSheetTestTypeProvider::class)
Expand Down Expand Up @@ -403,6 +464,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.stripe.android.paymentsheet.utils

import androidx.compose.ui.test.SemanticsMatcher

private const val IS_PLACED = "is_placed_in_layout"

/**
* This matcher checks if a given composable node is placed on the screen.
*
* Composable nodes may be removed but cached for reuse. This happens when using
* LazyColumn/LazyRow. The Compose testing framework will still be able to find
* these nodes even if they are not displayed. This matcher checks ensures that found
* node is placed on the laid out screen and not cached by lazy lists or another
* recycling composable.
*/
internal fun isPlaced() = SemanticsMatcher(IS_PLACED) { node ->
node.layoutInfo.isPlaced
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up finding the issue for why the composable node was still found. Added a new SemanticsMatcher to fix the test. @amk-stripe

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!! (not blocking) What do you think of naming the utils function something like "isVisible" or "isDisplayed"? I think those are more semantically meaningful than "isPlaced"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, can rename in a separate PR!

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.ui

import android.graphics.Typeface
import androidx.annotation.RestrictTo
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -125,6 +126,7 @@ private fun EditButton(
}

IconButton(
modifier = Modifier.testTag(PAYMENT_SHEET_EDIT_BUTTON_TEST_TAG),
enabled = isEnabled,
onClick = onClick,
) {
Expand Down Expand Up @@ -185,3 +187,6 @@ internal fun TestModeBadge_Preview() {
TestModeBadge()
}
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
const val PAYMENT_SHEET_EDIT_BUTTON_TEST_TAG = "PaymentSheetEditButton"