Skip to content

Commit

Permalink
feat(health-sdk): Added unit tests for PaymentComponentView
Browse files Browse the repository at this point in the history
IPC-186
  • Loading branch information
danicretu committed Mar 15, 2024
1 parent 62a153e commit 03fab61
Showing 1 changed file with 168 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package net.gini.android.health.sdk.paymentComponent

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.SpannableString
import android.text.style.ClickableSpan
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import net.gini.android.health.sdk.R
import net.gini.android.health.sdk.paymentcomponent.PaymentComponent
import net.gini.android.health.sdk.paymentcomponent.PaymentComponentView
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class PaymentComponentViewTest {
private var activity : Activity? = null
private var context: Context? = null
private var scenario: ActivityScenario<Activity>? = null
private lateinit var paymentComponent: PaymentComponent
private lateinit var paymentComponentListener: PaymentComponent.Listener

@Before
fun setUp() {
activity = Robolectric.buildActivity(Activity::class.java).create().get()
context = ApplicationProvider.getApplicationContext()
context!!.setTheme(R.style.GiniHealthTheme)
paymentComponent = PaymentComponent(context!!, mockk())
paymentComponentListener = mockk(relaxed = true)
paymentComponent.listener = paymentComponentListener

scenario = ActivityScenario.launch(
Intent(context, Activity::class.java)
)
scenario?.moveToState(Lifecycle.State.CREATED)
}

@After
fun tearDown() {
activity = null
context = null
scenario!!.close()
}

@Test
fun `calls onMoreInformation method of listener when clicking on info button`() = runTest {
// Given
scenario?.onActivity { activity ->
val paymentComponentView = PaymentComponentView(activity, null)
paymentComponentView.paymentComponent = paymentComponent

// When
(paymentComponentView.findViewById(R.id.ghs_info_circle_icon) as Button).performClick()
// Then
verify {
paymentComponentListener.onMoreInformationClicked()
}
}
}

@Test
fun `calls onMoreInformation method of listener when clicking on more information label`() = runTest {
// Given
scenario?.onActivity { activity ->
val paymentComponentView = PaymentComponentView(activity, null)
paymentComponentView.paymentComponent = paymentComponent

val moreInformationLabel = paymentComponentView.findViewById(R.id.ghs_more_information_label) as TextView

// When
val spannableString = moreInformationLabel.text as SpannableString
val spans = spannableString.getSpans(0, spannableString.length, ClickableSpan::class.java)

Truth.assertThat(spans.size).isEqualTo(1)
spans[0].onClick(moreInformationLabel)

// Then
verify {
paymentComponentListener.onMoreInformationClicked()
}
}
}

@Test
fun `calls onBankPickerClicked method of listener when clicking on select bank button`() = runTest {
// Given
scenario?.onActivity { activity ->
val paymentComponentView = PaymentComponentView(activity, null)
paymentComponentView.paymentComponent = paymentComponent

// When
(paymentComponentView.findViewById(R.id.ghs_select_bank_button) as Button).performClick()
// Then
verify {
paymentComponentListener.onBankPickerClicked()
}
}
}

@Test
fun `does not call onPayInvoiceClicked method of listener if no document id is set`() = runTest {
// Given
scenario?.onActivity { activity ->
val paymentComponentView = PaymentComponentView(activity, null)
paymentComponentView.paymentComponent = paymentComponent

// When
(paymentComponentView.findViewById(R.id.ghs_pay_invoice_button) as Button).performClick()

// Then
verify(exactly = 0) { paymentComponentListener.onPayInvoiceClicked("") }
}
}

@Test
fun `calls onPayInvoiceClicked method of listener when clicking on pay invoice button and document id`() = runTest {
// Given
scenario?.onActivity { activity ->
val paymentComponentView = PaymentComponentView(activity, null)
paymentComponentView.paymentComponent = paymentComponent
paymentComponentView.documentId = "123"

// When
(paymentComponentView.findViewById(R.id.ghs_pay_invoice_button) as Button).performClick()
// Then
verify {
paymentComponentListener.onPayInvoiceClicked("123")
}
}
}

@Test
fun `disables buttons and deletes document id to reuse`() = runTest {
// Given
scenario?.onActivity { activity ->
val paymentComponentView = PaymentComponentView(activity, null)
paymentComponentView.paymentComponent = paymentComponent
paymentComponentView.documentId = "123"
paymentComponentView.isPayable = true

Truth.assertThat(paymentComponentView.documentId).isEqualTo("123")
Truth.assertThat(paymentComponentView.isPayable).isEqualTo(true)
Truth.assertThat((paymentComponentView.findViewById(R.id.ghs_pay_invoice_button) as Button).isEnabled).isEqualTo(true)
Truth.assertThat((paymentComponentView.findViewById(R.id.ghs_select_bank_button) as Button).isEnabled).isEqualTo(true)

// When
paymentComponentView.prepareForReuse()

// Then
Truth.assertThat(paymentComponentView.documentId).isNull()
Truth.assertThat(paymentComponentView.isPayable).isEqualTo(false)
Truth.assertThat((paymentComponentView.findViewById(R.id.ghs_pay_invoice_button) as Button).isEnabled).isEqualTo(false)
Truth.assertThat((paymentComponentView.findViewById(R.id.ghs_select_bank_button) as Button).isEnabled).isEqualTo(false)
}
}
}

0 comments on commit 03fab61

Please sign in to comment.