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

don't select a payment method if the user it has selected nothing (#… #1792

Merged
merged 5 commits into from
Nov 7, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ internal class PaymentMethodsAdapter @JvmOverloads internal constructor(
internal var selectedPaymentMethodId: String? = initiallySelectedPaymentMethodId
internal val selectedPaymentMethod: PaymentMethod?
get() {
// return the selected Payment Method, if it exists;
// otherwise, return the most recently created Payment Method
return selectedPaymentMethodId?.let { selectedPaymentMethodId ->
paymentMethods.firstOrNull { it.id == selectedPaymentMethodId }
} ?: (paymentMethods.maxBy { it.created ?: 0 })
}
}

internal var listener: Listener? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ class PaymentMethodsActivityTest : BaseViewTest<PaymentMethodsActivity>(PaymentM

val paymentMethodsAdapter =
recyclerView.adapter as PaymentMethodsAdapter

paymentMethodsAdapter.selectedPaymentMethodId = paymentMethod.id
assertEquals(paymentMethod.id, paymentMethodsAdapter.selectedPaymentMethod?.id)
Comment on lines 170 to 174
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can delete these lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Michael. If I delete the assignment the test fails because the selected payment method is null

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.times
Expand Down Expand Up @@ -38,6 +39,7 @@ class PaymentMethodsAdapterTest {
assertEquals(4, paymentMethodsAdapter.itemCount)
verify<RecyclerView.AdapterDataObserver>(adapterDataObserver).onChanged()

paymentMethodsAdapter.selectedPaymentMethodId = paymentMethodsAdapter.paymentMethods[2].id
assertEquals(
PaymentMethodFixtures.CARD_PAYMENT_METHODS[2].id,
requireNotNull(paymentMethodsAdapter.selectedPaymentMethod).id
Expand All @@ -57,6 +59,7 @@ class PaymentMethodsAdapterTest {
listOf(PaymentMethodFixtures.CARD_PAYMENT_METHODS[0])

paymentMethodsAdapter.setPaymentMethods(singlePaymentMethod)
paymentMethodsAdapter.selectedPaymentMethodId = paymentMethodsAdapter.paymentMethods[0].id
assertEquals(2, paymentMethodsAdapter.itemCount)
assertNotNull(paymentMethodsAdapter.selectedPaymentMethod)

Expand All @@ -66,6 +69,7 @@ class PaymentMethodsAdapterTest {
)

paymentMethodsAdapter.setPaymentMethods(PaymentMethodFixtures.CARD_PAYMENT_METHODS)
paymentMethodsAdapter.selectedPaymentMethodId = paymentMethodsAdapter.paymentMethods[2].id
assertEquals(4, paymentMethodsAdapter.itemCount)
assertEquals(
PaymentMethodFixtures.CARD_PAYMENT_METHODS[2].id,
Expand All @@ -91,12 +95,12 @@ class PaymentMethodsAdapterTest {
}

@Test
fun setPaymentMethods_whenNoInitialSpecified_selectsMostRecentlyCreated() {
fun setPaymentMethods_whenNoInitialSpecified_returnsNull() {
val adapter = PaymentMethodsAdapter(null,
PaymentMethodsActivityStarter.Args.Builder()
.build())
adapter.setPaymentMethods(PaymentMethodFixtures.CARD_PAYMENT_METHODS)
assertEquals("pm_3000", adapter.selectedPaymentMethod?.id)
assertNull(adapter.selectedPaymentMethod)
}

@Test
Expand Down