Skip to content

Commit

Permalink
Make readiness depending on the availability check
Browse files Browse the repository at this point in the history
Before it was based on if the button was shown or not, which can be hidden even if the component state is fully valid and ready.

COAND-941
  • Loading branch information
OscarSpruit committed Dec 9, 2024
1 parent fe45972 commit ad988b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ internal class DefaultGooglePayDelegate(
private val payEventChannel: Channel<Task<PaymentData>> = bufferedChannel()
override val payEventFlow: Flow<Task<PaymentData>> = payEventChannel.receiveAsFlow()

private var isAvailable = false

override fun initialize(coroutineScope: CoroutineScope) {
_coroutineScope = coroutineScope
submitHandler.initialize(coroutineScope, componentStateFlow)
Expand All @@ -107,6 +109,7 @@ internal class DefaultGooglePayDelegate(
paymentMethod,
componentParams,
) { isAvailable, _ ->
this.isAvailable = isAvailable
updateOutputData(isButtonVisible = isAvailable)

if (!isAvailable) {
Expand Down Expand Up @@ -182,11 +185,7 @@ internal class DefaultGooglePayDelegate(
amount = componentParams.amount,
)

val isReady = if (shouldShowSubmitButton()) {
outputData.isButtonVisible
} else {
true
}
val isReady = isAvailable

return GooglePayComponentState(
data = paymentComponentData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal class DefaultGooglePayDelegateTest(
with(awaitItem()) {
assertNull(data.paymentMethod)
assertFalse(isInputValid)
assertTrue(isReady)
assertFalse(isReady)
assertNull(paymentData)
}

Expand All @@ -101,6 +101,9 @@ internal class DefaultGooglePayDelegateTest(

@Test
fun `when payment data is null, then state is not valid`() = runTest {
withAvailabilityCheck(true)
delegate.initialize(CoroutineScope(UnconfinedTestDispatcher()))

delegate.componentStateFlow.test {
delegate.updateComponentState(createOutputData(paymentData = null))

Expand All @@ -117,6 +120,9 @@ internal class DefaultGooglePayDelegateTest(

@Test
fun `when creating component state with valid payment data, then the state is propagated`() = runTest {
withAvailabilityCheck(true)
delegate.initialize(CoroutineScope(UnconfinedTestDispatcher()))

delegate.componentStateFlow.test {
skipItems(1)

Expand Down Expand Up @@ -304,10 +310,7 @@ internal class DefaultGooglePayDelegateTest(
expectedIsReady: Boolean,
expectedException: CheckoutException?,
) = runTest {
whenever(googlePayAvailabilityCheck.isAvailable(any(), any(), any())) doAnswer { invocation ->
(invocation.getArgument(2, ComponentAvailableCallback::class.java))
.onAvailabilityResult(isAvailable, PaymentMethod())
}
withAvailabilityCheck(isAvailable)

val config = createCheckoutConfiguration {
setSubmitButtonVisible(isSubmitButtonVisible)
Expand Down Expand Up @@ -382,6 +385,13 @@ internal class DefaultGooglePayDelegateTest(
paymentData: PaymentData? = null,
) = GooglePayOutputData(isButtonVisible, isLoading, paymentData)

private fun withAvailabilityCheck(isAvailable: Boolean) {
whenever(googlePayAvailabilityCheck.isAvailable(any(), any(), any())) doAnswer { invocation ->
(invocation.getArgument(2, ComponentAvailableCallback::class.java))
.onAvailabilityResult(isAvailable, PaymentMethod())
}
}

companion object {
private val TEST_ORDER = OrderRequest("PSP", "ORDER_DATA")
private const val TEST_CHECKOUT_ATTEMPT_ID = "TEST_CHECKOUT_ATTEMPT_ID"
Expand Down Expand Up @@ -411,7 +421,7 @@ internal class DefaultGooglePayDelegateTest(
@JvmStatic
fun googlePayAvailableSource() = listOf(
// isSubmitButtonVisible, isAvailable, expectedIsReady, expectedException
arguments(false, false, true, GooglePayUnavailableException()),
arguments(false, false, false, GooglePayUnavailableException()),
arguments(false, true, true, null),
arguments(true, false, false, GooglePayUnavailableException()),
arguments(true, true, true, null),
Expand Down

0 comments on commit ad988b9

Please sign in to comment.