Skip to content

Commit

Permalink
Track submit event when a value is emitted in the stored components
Browse files Browse the repository at this point in the history
COAND-832
  • Loading branch information
araratthehero committed Nov 27, 2024
1 parent ed36fe1 commit 2ae379a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ internal class StoredCardDelegate(
private val _viewFlow: MutableStateFlow<ComponentViewType?> = MutableStateFlow(null)
override val viewFlow: Flow<ComponentViewType?> = _viewFlow

override val submitFlow: Flow<CardComponentState> = submitHandler.submitFlow
override val submitFlow: Flow<CardComponentState> = getTrackedSubmitFlow()
override val uiStateFlow: Flow<PaymentComponentUIState> = submitHandler.uiStateFlow
override val uiEventFlow: Flow<PaymentComponentUIEvent> = submitHandler.uiEventFlow

Expand Down Expand Up @@ -316,10 +316,12 @@ internal class StoredCardDelegate(
)
}

override fun onSubmit() {
private fun getTrackedSubmitFlow() = submitHandler.submitFlow.onEach {
val event = GenericEvents.submit(storedPaymentMethod.type.orEmpty())
analyticsManager.trackEvent(event)
}

override fun onSubmit() {
val state = _componentStateFlow.value
submitHandler.onSubmit(state)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import com.adyen.checkout.ui.core.internal.util.AddressValidationUtils
import com.adyen.threeds2.ThreeDS2Service
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
Expand All @@ -75,6 +77,7 @@ import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import java.util.Locale
Expand Down Expand Up @@ -266,24 +269,26 @@ internal class StoredCardDelegateTest(
}

@Test
fun `encryption fails, then component state should be invalid and analytics error event is tracked `() = runTest {
cardEncryptor.shouldThrowException = true
fun `encryption fails, then component state should be invalid and analytics error event is tracked `() =
runTest {
cardEncryptor.shouldThrowException = true

delegate.initialize(CoroutineScope(UnconfinedTestDispatcher()))
delegate.initialize(CoroutineScope(UnconfinedTestDispatcher()))

delegate.componentStateFlow.test {
delegate.updateComponentState(createOutputData())
delegate.componentStateFlow.test {
delegate.updateComponentState(createOutputData())

val componentState = expectMostRecentItem()
val componentState = expectMostRecentItem()

val expectedEvent = GenericEvents.error(CardPaymentMethod.PAYMENT_METHOD_TYPE, ErrorEvent.ENCRYPTION)
analyticsManager.assertLastEventEquals(expectedEvent)
val expectedEvent =
GenericEvents.error(CardPaymentMethod.PAYMENT_METHOD_TYPE, ErrorEvent.ENCRYPTION)
analyticsManager.assertLastEventEquals(expectedEvent)

assertTrue(componentState.isReady)
assertFalse(componentState.isInputValid)
assertNull(componentState.lastFourDigits)
assertTrue(componentState.isReady)
assertFalse(componentState.isInputValid)
assertNull(componentState.lastFourDigits)
}
}
}

@Test
fun `security code in output data is invalid, then component state should be invalid`() = runTest {
Expand Down Expand Up @@ -454,11 +459,15 @@ internal class StoredCardDelegateTest(
}

@Test
fun `when onSubmit is called, then submit event is tracked`() {
delegate.onSubmit()
fun `when submitFlow emits an event, then submit event is tracked`() = runTest {
val submitFlow = flow<CardComponentState> { emit(mock()) }
whenever(submitHandler.submitFlow) doReturn submitFlow
val delegate = createCardDelegate()

val expectedEvent = GenericEvents.submit(PaymentMethodTypes.SCHEME)
analyticsManager.assertLastEventEquals(expectedEvent)
delegate.submitFlow.collectLatest {
val expectedEvent = GenericEvents.submit(PaymentMethodTypes.SCHEME)
analyticsManager.assertLastEventEquals(expectedEvent)
}
}

@Test
Expand Down Expand Up @@ -515,7 +524,7 @@ internal class StoredCardDelegateTest(
submitHandler = submitHandler,
order = order,
cardConfigDataGenerator = cardConfigDataGenerator,
cardValidationMapper = CardValidationMapper()
cardValidationMapper = CardValidationMapper(),
)
}

Expand Down

0 comments on commit 2ae379a

Please sign in to comment.