Skip to content

Commit 46ba615

Browse files
committed
cleanup WalletScreen and add UI tests
1 parent de9383b commit 46ba615

File tree

4 files changed

+83
-24
lines changed

4 files changed

+83
-24
lines changed

paymentsheet/detekt-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<ID>LongMethod:USBankAccountFormViewModel.kt$USBankAccountFormViewModel$private fun createNewPaymentSelection( resultIdentifier: ResultIdentifier, last4: String?, bankName: String?, billingDetails: PaymentMethod.BillingDetails, ): PaymentSelection.New.USBankAccount</ID>
5757
<ID>LongMethod:UpdatePaymentMethodUI.kt$@Composable internal fun UpdatePaymentMethodUI(interactor: UpdatePaymentMethodInteractor, modifier: Modifier)</ID>
5858
<ID>LongMethod:VerificationBody.kt$@Composable internal fun VerificationBody( state: VerificationViewState, otpElement: OTPElement, onBack: () -> Unit, onFocusRequested: () -> Unit, didShowCodeSentNotification: () -> Unit, onChangeEmailClick: () -> Unit, onResendCodeClick: () -> Unit, )</ID>
59+
<ID>LongMethod:WalletScreen.kt$@Composable internal fun WalletBody( state: WalletUiState, expiryDateController: TextFieldController, cvcController: CvcController, onItemSelected: (ConsumerPaymentDetails.PaymentDetails) -> Unit, onExpandedChanged: (Boolean) -> Unit, onAddPaymentMethodOptionClicked: (AddPaymentMethodOption) -> Unit, onPrimaryButtonClick: () -> Unit, onPayAnotherWayClicked: () -> Unit, onDismissAlert: () -> Unit, onSetDefaultClicked: (ConsumerPaymentDetails.PaymentDetails) -> Unit, onRemoveClicked: (ConsumerPaymentDetails.PaymentDetails) -> Unit, onUpdateClicked: (ConsumerPaymentDetails.PaymentDetails) -> Unit, onLogoutClicked: () -> Unit, showBottomSheetContent: (BottomSheetContent) -> Unit, hideBottomSheetContent: suspend () -> Unit, )</ID>
5960
<ID>LongMethod:WalletScreen.kt$@Composable private fun PaymentMethodSection( state: WalletUiState, isExpanded: Boolean, onItemSelected: (ConsumerPaymentDetails.PaymentDetails) -> Unit, onExpandedChanged: (Boolean) -> Unit, onAddNewPaymentMethodClicked: () -> Unit, onSetDefaultClicked: (ConsumerPaymentDetails.PaymentDetails) -> Unit, onRemoveClicked: (ConsumerPaymentDetails.PaymentDetails) -> Unit, onUpdateClicked: (ConsumerPaymentDetails.PaymentDetails) -> Unit, onLogoutClicked: () -> Unit, showBottomSheetContent: (BottomSheetContent) -> Unit, hideBottomSheetContent: suspend () -> Unit )</ID>
6061
<ID>MagicNumber:NewPaymentMethodTabLayoutUI.kt$.3f</ID>
6162
<ID>MagicNumber:NewPaymentMethodTabLayoutUI.kt$.4f</ID>

paymentsheet/src/main/java/com/stripe/android/link/ui/wallet/WalletScreen.kt

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ internal fun WalletScreen(
9292
hideBottomSheetContent: suspend () -> Unit,
9393
onLogoutClicked: () -> Unit,
9494
) {
95-
val coroutineScope = rememberCoroutineScope()
9695
val state by viewModel.uiState.collectAsState()
9796

9897
val financialConnectionsSheetLauncher =
@@ -129,23 +128,7 @@ internal fun WalletScreen(
129128
onSetDefaultClicked = viewModel::onSetDefaultClicked,
130129
showBottomSheetContent = showBottomSheetContent,
131130
hideBottomSheetContent = hideBottomSheetContent,
132-
onAddNewPaymentMethodClicked = {
133-
if (state.addPaymentMethodOptions.size == 1) {
134-
viewModel.onAddPaymentMethodOptionClicked(state.addPaymentMethodOptions[0])
135-
} else {
136-
showBottomSheetContent {
137-
AddPaymentMethodMenu(
138-
options = state.addPaymentMethodOptions,
139-
onOptionClick = { option ->
140-
viewModel.onAddPaymentMethodOptionClicked(option)
141-
coroutineScope.launch {
142-
hideBottomSheetContent()
143-
}
144-
},
145-
)
146-
}
147-
}
148-
},
131+
onAddPaymentMethodOptionClicked = viewModel::onAddPaymentMethodOptionClicked,
149132
onDismissAlert = viewModel::onDismissAlert
150133
)
151134
}
@@ -157,7 +140,7 @@ internal fun WalletBody(
157140
cvcController: CvcController,
158141
onItemSelected: (ConsumerPaymentDetails.PaymentDetails) -> Unit,
159142
onExpandedChanged: (Boolean) -> Unit,
160-
onAddNewPaymentMethodClicked: () -> Unit,
143+
onAddPaymentMethodOptionClicked: (AddPaymentMethodOption) -> Unit,
161144
onPrimaryButtonClick: () -> Unit,
162145
onPayAnotherWayClicked: () -> Unit,
163146
onDismissAlert: () -> Unit,
@@ -168,6 +151,7 @@ internal fun WalletBody(
168151
showBottomSheetContent: (BottomSheetContent) -> Unit,
169152
hideBottomSheetContent: suspend () -> Unit,
170153
) {
154+
val coroutineScope = rememberCoroutineScope()
171155
AnimatedContent(
172156
targetState = state.paymentDetailsList.isEmpty(),
173157
transitionSpec = { LinkScreenTransition },
@@ -204,7 +188,24 @@ internal fun WalletBody(
204188
onUpdateClicked = onUpdateClicked,
205189
onLogoutClicked = onLogoutClicked,
206190
onSetDefaultClicked = onSetDefaultClicked,
207-
onAddNewPaymentMethodClicked = onAddNewPaymentMethodClicked,
191+
onAddNewPaymentMethodClicked = {
192+
if (state.addPaymentMethodOptions.size == 1) {
193+
onAddPaymentMethodOptionClicked(state.addPaymentMethodOptions[0])
194+
} else {
195+
showBottomSheetContent {
196+
AddPaymentMethodMenu(
197+
modifier = Modifier.testTag(WALLET_SCREEN_ADD_PAYMENT_METHOD_MENU),
198+
options = state.addPaymentMethodOptions,
199+
onOptionClick = { option ->
200+
onAddPaymentMethodOptionClicked(option)
201+
coroutineScope.launch {
202+
hideBottomSheetContent()
203+
}
204+
},
205+
)
206+
}
207+
}
208+
},
208209
hideBottomSheetContent = hideBottomSheetContent
209210
)
210211

@@ -813,6 +814,7 @@ internal const val COLLAPSED_WALLET_PAYMENT_DETAILS_TAG = "collapsed_wallet_paym
813814
internal const val COLLAPSED_WALLET_ROW = "collapsed_wallet_row_tag"
814815
internal const val WALLET_SCREEN_EXPANDED_ROW_HEADER = "wallet_screen_expanded_row_header"
815816
internal const val WALLET_ADD_PAYMENT_METHOD_ROW = "wallet_add_payment_method_row"
817+
internal const val WALLET_SCREEN_ADD_PAYMENT_METHOD_MENU = "wallet_screen_add_payment_method_sheet"
816818
internal const val WALLET_SCREEN_PAYMENT_METHODS_LIST = "wallet_screen_payment_methods_list"
817819
internal const val WALLET_SCREEN_PAY_BUTTON = "wallet_screen_pay_button"
818820
internal const val WALLET_SCREEN_PAY_ANOTHER_WAY_BUTTON = "wallet_screen_pay_another_way_button"

paymentsheet/src/test/java/com/stripe/android/link/ui/wallet/WalletScreenScreenshotTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ internal class WalletScreenScreenshotTest {
229229
onSetDefaultClicked = {},
230230
showBottomSheetContent = {},
231231
hideBottomSheetContent = {},
232-
onAddNewPaymentMethodClicked = {},
232+
onAddPaymentMethodOptionClicked = {},
233233
onDismissAlert = {},
234234
onLogoutClicked = {},
235235
expiryDateController = SimpleTextFieldController(DateConfig()),

paymentsheet/src/test/java/com/stripe/android/link/ui/wallet/WalletScreenTest.kt

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package com.stripe.android.link.ui.wallet
33
import androidx.activity.ComponentActivity
44
import androidx.compose.foundation.layout.Box
55
import androidx.compose.foundation.layout.Column
6+
import androidx.compose.foundation.layout.fillMaxSize
67
import androidx.compose.runtime.Composable
78
import androidx.compose.runtime.getValue
89
import androidx.compose.runtime.mutableStateOf
910
import androidx.compose.runtime.remember
1011
import androidx.compose.runtime.setValue
12+
import androidx.compose.ui.Modifier
1113
import androidx.compose.ui.test.SemanticsNodeInteraction
1214
import androidx.compose.ui.test.assertCountEquals
1315
import androidx.compose.ui.test.assertHasClickAction
@@ -42,6 +44,7 @@ import com.stripe.android.model.CardBrand
4244
import com.stripe.android.model.ConsumerPaymentDetails
4345
import com.stripe.android.model.ConsumerPaymentDetailsUpdateParams
4446
import com.stripe.android.model.CvcCheck
47+
import com.stripe.android.payments.financialconnections.FinancialConnectionsAvailability
4548
import com.stripe.android.testing.CoroutineTestRule
4649
import com.stripe.android.testing.FakeLogger
4750
import com.stripe.android.ui.core.elements.CvcController
@@ -189,6 +192,54 @@ internal class WalletScreenTest {
189192
.assertHasClickAction()
190193
}
191194

195+
@Test
196+
fun `add payment method menu is shown when multiple options are available`() = runTest(dispatcher) {
197+
composeTestRule.setContent {
198+
DefaultLinkTheme {
199+
var bottomSheetContent: BottomSheetContent? by remember { mutableStateOf(null) }
200+
Box(modifier = Modifier.fillMaxSize()) {
201+
if (bottomSheetContent == null) {
202+
TestWalletBody(
203+
addPaymentMethodOptions = listOf(
204+
AddPaymentMethodOption.Card,
205+
AddPaymentMethodOption.Bank(FinancialConnectionsAvailability.Full),
206+
),
207+
showBottomSheetContent = { bottomSheetContent = it },
208+
hideBottomSheetContent = { bottomSheetContent = null },
209+
)
210+
} else {
211+
Column {
212+
bottomSheetContent?.invoke(this)
213+
}
214+
}
215+
}
216+
}
217+
}
218+
onWalletAddPaymentMethodRow().performClick()
219+
composeTestRule.waitForIdle()
220+
onWalletAddPaymentMethodMenu().assertIsDisplayed()
221+
}
222+
223+
@Test
224+
fun `single payment method option is selected when only one available`() = runTest(dispatcher) {
225+
var selectedAddPaymentMethodOptionClicked: AddPaymentMethodOption? = null
226+
composeTestRule.setContent {
227+
DefaultLinkTheme {
228+
Box(modifier = Modifier.fillMaxSize()) {
229+
TestWalletBody(
230+
addPaymentMethodOptions = listOf(AddPaymentMethodOption.Card),
231+
onAddPaymentMethodOptionClicked = { selectedAddPaymentMethodOptionClicked = it },
232+
showBottomSheetContent = {},
233+
hideBottomSheetContent = {},
234+
)
235+
}
236+
}
237+
}
238+
onWalletAddPaymentMethodRow().performClick()
239+
composeTestRule.waitForIdle()
240+
assertThat(selectedAddPaymentMethodOptionClicked).isEqualTo(AddPaymentMethodOption.Card)
241+
}
242+
192243
@Test
193244
fun `wallet list is expanded and pay button should be disabled for expired card`() = runTest(dispatcher) {
194245
val linkAccountManager = FakeLinkAccountManager()
@@ -686,10 +737,12 @@ internal class WalletScreenTest {
686737

687738
@Composable
688739
private fun TestWalletBody(
740+
addPaymentMethodOptions: List<AddPaymentMethodOption> = listOf(AddPaymentMethodOption.Card),
689741
onRemoveClicked: (ConsumerPaymentDetails.PaymentDetails) -> Unit = {},
690742
onSetDefaultClicked: (ConsumerPaymentDetails.PaymentDetails) -> Unit = {},
743+
onAddPaymentMethodOptionClicked: (AddPaymentMethodOption) -> Unit = {},
691744
showBottomSheetContent: (BottomSheetContent?) -> Unit,
692-
hideBottomSheetContent: () -> Unit
745+
hideBottomSheetContent: () -> Unit,
693746
) {
694747
val paymentDetails = TestFactory.CONSUMER_PAYMENT_DETAILS.paymentDetails
695748
.filterIsInstance<ConsumerPaymentDetails.Card>()
@@ -704,7 +757,7 @@ internal class WalletScreenTest {
704757
hasCompleted = false,
705758
primaryButtonLabel = "Buy".resolvableString,
706759
secondaryButtonLabel = "Pay another way".resolvableString,
707-
addPaymentMethodOptions = listOf(AddPaymentMethodOption.Card),
760+
addPaymentMethodOptions = addPaymentMethodOptions,
708761
userSetIsExpanded = true,
709762
isSettingUp = false,
710763
merchantName = "Example Inc.",
@@ -717,7 +770,7 @@ internal class WalletScreenTest {
717770
onSetDefaultClicked = onSetDefaultClicked,
718771
showBottomSheetContent = showBottomSheetContent,
719772
hideBottomSheetContent = hideBottomSheetContent,
720-
onAddNewPaymentMethodClicked = {},
773+
onAddPaymentMethodOptionClicked = onAddPaymentMethodOptionClicked,
721774
onDismissAlert = {},
722775
onUpdateClicked = {},
723776
onLogoutClicked = {},
@@ -761,6 +814,9 @@ internal class WalletScreenTest {
761814
private fun onWalletAddPaymentMethodRow() =
762815
composeTestRule.onNodeWithTag(WALLET_ADD_PAYMENT_METHOD_ROW, useUnmergedTree = true)
763816

817+
private fun onWalletAddPaymentMethodMenu() =
818+
composeTestRule.onNodeWithTag(WALLET_SCREEN_ADD_PAYMENT_METHOD_MENU, useUnmergedTree = true)
819+
764820
private fun onPaymentMethodList() = composeTestRule.onAllNodes(hasTestTag(WALLET_SCREEN_PAYMENT_METHODS_LIST))
765821

766822
private fun onExpandedWalletHeader() =

0 commit comments

Comments
 (0)