checkout / frames-android

Frames Android: making native card payments simple
https://www.checkout.com/docs/integrate/sdks/android-sdk
MIT License
47 stars 32 forks source link

PIMOB: 2109: Allow injection of cardholder name field in paymentformConfig #221

Closed chintan-soni-cko closed 10 months ago

chintan-soni-cko commented 10 months ago

Issue

PIMOB-2109

Proposed changes

1 Add cardHolderName in PaymentFormConfig 2 Load the cardholdername field in the Payment form from PaymentFormConfig

  1. Refactored existing unit test for paymentformstatemanager field

Test Steps

  1. add cardHolderName in PaymentFormConfig
  2. Verify the paymentform details' cardholder name is updated Screen_recording_20230816_114904.webm

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you choose the solution you did and what alternatives you considered, etc...

chintan-soni-cko commented 10 months ago

One thing good to have is that now we know some files have no test at all, we could start adding them, such as PaymentFormViewModel, PaymentFormScreen, PaymentFormConfig, etc.

We can just do a bare minimum test against the values we added when there's no existing tests file.

For instance,

  • for PaymentFormViewModel test we can cover the injector provides the correct card holder name, and cardholder name default value is an empty string.
  • for PaymentFormScreen test we can cover the same for the viewModel.

It will be a long way to go at the beginning as we lack quite some tests, but over the time it will get things easier. Of course if there's difficult writing tests or tight deadline we can drop it

@jheng-hao-lin-cko Thanks for the suggestion. I have tried to add test cases for new classes which are apparently not easy to testable due to their design and capabilities of injection in constructors.

chintan-soni-cko commented 10 months ago

Just experiment writing a test against PaymentFormScreen which works fine checking whether the screen has a field contains the prefilled data, thou it might not be taken by the test coverage analysis because of the setup.

will leave the test code here, up to you whether we like to include it in this PR.

package com.checkout.frames.screen.paymentform

import android.content.Context
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.checkout.base.model.CardScheme
import com.checkout.base.model.Country
import com.checkout.base.model.Environment
import com.checkout.frames.api.PaymentFlowHandler
import com.checkout.frames.screen.paymentform.model.BillingFormAddress
import com.checkout.frames.screen.paymentform.model.PaymentFormConfig
import com.checkout.frames.screen.paymentform.model.PrefillData
import com.checkout.frames.style.screen.PaymentFormStyle
import com.checkout.tokenization.model.Address
import com.checkout.tokenization.model.Phone
import com.checkout.tokenization.model.TokenDetails
import io.mockk.junit5.MockKExtension
import io.mockk.mockk
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
internal class PaymentFormScreenTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    private val expectedMockContext: Context = mockk(relaxed = true)

    private val paymentFormConfig = PaymentFormConfig(
        context = expectedMockContext,
        environment = Environment.PRODUCTION,
        publicKey = "Test key",
        style = PaymentFormStyle(),
        supportedCardSchemeList = listOf(CardScheme.VISA, CardScheme.MAESTRO),
        paymentFlowHandler = object : PaymentFlowHandler {
            override fun onSubmit() {}
            override fun onSuccess(tokenDetails: TokenDetails) {}
            override fun onFailure(errorMessage: String) {}
            override fun onBackPressed() {}
        },
        prefillData = PrefillData(
            cardHolderName = "Test Name",
            billingFormAddress = BillingFormAddress(
                name = "Test Billing Address name",
                address = Address(
                    addressLine1 = "Checkout.com",
                    addressLine2 = "90 Tottenham Court Road",
                    city = "London",
                    state = "London",
                    zip = "W1T 4TJ",
                    country = Country.from(iso3166Alpha2 = "GB")
                ),
                phone = Phone(
                    number = "4155552671", country = Country.from(iso3166Alpha2 = "GB")
                )
            )
        )
    )

    @Test
    fun paymentFormScreenShouldBeCreatedWithTheCorrectTestNameFilled() {
        composeTestRule.setContent {
            MaterialTheme { PaymentFormScreen(paymentFormConfig) }
        }

        composeTestRule.onNodeWithText("Test Name").assertIsDisplayed()
    }
}

Thanks, @jheng-hao-lin-cko will include in the next PR. So, it will include for billing form address as well.