Skip to content

Commit

Permalink
In vertical mode, only open card brand choice screen if the saved PM …
Browse files Browse the repository at this point in the history
…is modifiable (#9124)
  • Loading branch information
amk-stripe committed Aug 26, 2024
1 parent fb2eb23 commit a871e94
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ internal class DefaultPaymentMethodVerticalLayoutInteractor(
private val mostRecentlySelectedSavedPaymentMethod: StateFlow<PaymentMethod?>,
private val providePaymentMethodName: (PaymentMethodCode?) -> ResolvableString,
private val canRemove: StateFlow<Boolean>,
private val canEdit: StateFlow<Boolean>,
private val onEditPaymentMethod: (DisplayableSavedPaymentMethod) -> Unit,
private val onSelectSavedPaymentMethod: (PaymentMethod) -> Unit,
private val walletsState: StateFlow<WalletsState?>,
Expand Down Expand Up @@ -133,7 +132,6 @@ internal class DefaultPaymentMethodVerticalLayoutInteractor(
paymentMethods = customerStateHolder.paymentMethods,
mostRecentlySelectedSavedPaymentMethod = customerStateHolder.mostRecentlySelectedSavedPaymentMethod,
providePaymentMethodName = savedPaymentMethodMutator.providePaymentMethodName,
canEdit = viewModel.savedPaymentMethodMutator.canEdit,
canRemove = viewModel.savedPaymentMethodMutator.canRemove,
onEditPaymentMethod = { savedPaymentMethodMutator.modifyPaymentMethod(it.paymentMethod) },
onSelectSavedPaymentMethod = {
Expand Down Expand Up @@ -171,13 +169,11 @@ internal class DefaultPaymentMethodVerticalLayoutInteractor(
private val availableSavedPaymentMethodAction = combineAsStateFlow(
paymentMethods,
displayedSavedPaymentMethod,
canEdit,
canRemove,
) { paymentMethods, displayedSavedPaymentMethod, canEdit, canRemove ->
) { paymentMethods, displayedSavedPaymentMethod, canRemove ->
getAvailableSavedPaymentMethodAction(
paymentMethods = paymentMethods,
savedPaymentMethod = displayedSavedPaymentMethod,
canEdit = canEdit,
canRemove = canRemove
)
}
Expand Down Expand Up @@ -284,7 +280,6 @@ internal class DefaultPaymentMethodVerticalLayoutInteractor(
private fun getAvailableSavedPaymentMethodAction(
paymentMethods: List<PaymentMethod>?,
savedPaymentMethod: DisplayableSavedPaymentMethod?,
canEdit: Boolean,
canRemove: Boolean,
): PaymentMethodVerticalLayoutInteractor.SavedPaymentMethodAction {
if (paymentMethods == null || savedPaymentMethod == null) {
Expand All @@ -296,7 +291,7 @@ internal class DefaultPaymentMethodVerticalLayoutInteractor(
1 -> {
getSavedPaymentMethodActionForOnePaymentMethod(
canRemove = canRemove,
canEdit = canEdit,
savedPaymentMethod = savedPaymentMethod,
)
}
else ->
Expand All @@ -306,9 +301,9 @@ internal class DefaultPaymentMethodVerticalLayoutInteractor(

private fun getSavedPaymentMethodActionForOnePaymentMethod(
canRemove: Boolean,
canEdit: Boolean,
savedPaymentMethod: DisplayableSavedPaymentMethod?,
): PaymentMethodVerticalLayoutInteractor.SavedPaymentMethodAction {
return if (canEdit) {
return if (savedPaymentMethod?.isModifiable() == true) {
// Edit screen handles both canRemove variants.
PaymentMethodVerticalLayoutInteractor.SavedPaymentMethodAction.EDIT_CARD_BRAND
} else if (canRemove) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
initialPaymentMethods = PaymentMethodFixtures.createCards(3),
) {
canRemove.value = true
canEdit.value = true

interactor.state.test {
awaitItem().run {
Expand All @@ -129,7 +128,6 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
initialPaymentMethods = PaymentMethodFixtures.createCards(3),
) {
canRemove.value = false
canEdit.value = false

interactor.state.test {
awaitItem().run {
Expand All @@ -144,10 +142,9 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
@Test
fun `state has edit_card_brand saved PM action when one saved PM, can edit, and can remove`() {
runScenario(
initialPaymentMethods = PaymentMethodFactory.cards(1),
initialPaymentMethods = listOf(PaymentMethodFixtures.CARD_WITH_NETWORKS_PAYMENT_METHOD),
) {
canRemove.value = true
canEdit.value = true

interactor.state.test {
awaitItem().run {
Expand All @@ -162,10 +159,9 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
@Test
fun `state has edit card brand saved payment method action when one saved PM, can edit, and cannot remove`() {
runScenario(
initialPaymentMethods = PaymentMethodFactory.cards(1),
initialPaymentMethods = listOf(PaymentMethodFixtures.CARD_WITH_NETWORKS_PAYMENT_METHOD),
) {
canRemove.value = false
canEdit.value = true

interactor.state.test {
awaitItem().run {
Expand All @@ -177,13 +173,29 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
}
}

@Test
fun `state has manage one saved payment method action when one saved PM, cannot edit, and can remove`() {
runScenario(
initialPaymentMethods = PaymentMethodFactory.cards(1), // Creates a non-modifiable card
) {
canRemove.value = true

interactor.state.test {
awaitItem().run {
assertThat(availableSavedPaymentMethodAction).isEqualTo(
PaymentMethodVerticalLayoutInteractor.SavedPaymentMethodAction.MANAGE_ONE
)
}
}
}
}

@Test
fun `state has no saved payment method action when one saved PM, cannot edit, and cannot remove`() {
runScenario(
initialPaymentMethods = PaymentMethodFactory.cards(1),
initialPaymentMethods = PaymentMethodFactory.cards(1), // Creates a non-modifiable card
) {
canRemove.value = false
canEdit.value = false

interactor.state.test {
awaitItem().run {
Expand All @@ -196,12 +208,11 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
}

@Test
fun `state has no saved payment method action when multiple saved PMs, cannot edit, and cannot remove`() {
fun `state has no saved payment method action when multiple saved PMs and cannot remove`() {
runScenario(
initialPaymentMethods = PaymentMethodFactory.cards(1),
) {
canRemove.value = false
canEdit.value = false

interactor.state.test {
awaitItem().run {
Expand Down Expand Up @@ -972,7 +983,6 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
val mostRecentlySelectedSavedPaymentMethod: MutableStateFlow<PaymentMethod?> =
MutableStateFlow(initialMostRecentlySelectedSavedPaymentMethod)
val walletsState = MutableStateFlow<WalletsState?>(null)
val canEdit = MutableStateFlow(true)
val canRemove = MutableStateFlow(true)
val isCurrentScreen: MutableStateFlow<Boolean> = MutableStateFlow(initialIsCurrentScreen)
val dispatcher = StandardTestDispatcher()
Expand All @@ -998,7 +1008,6 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
updateSelection = updateSelection,
isCurrentScreen = isCurrentScreen,
dispatcher = dispatcher,
canEdit = canEdit,
canRemove = canRemove,
isLiveMode = true,
)
Expand All @@ -1012,7 +1021,6 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
walletsState = walletsState,
interactor = interactor,
canRemove = canRemove,
canEdit = canEdit,
dispatcher = dispatcher,
).apply {
runTest {
Expand All @@ -1029,7 +1037,6 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
val paymentMethodsSource: MutableStateFlow<List<PaymentMethod>>,
val walletsState: MutableStateFlow<WalletsState?>,
val canRemove: MutableStateFlow<Boolean>,
val canEdit: MutableStateFlow<Boolean>,
val interactor: PaymentMethodVerticalLayoutInteractor,
val dispatcher: TestDispatcher,
)
Expand Down

0 comments on commit a871e94

Please sign in to comment.