diff --git a/card/src/main/java/com/adyen/checkout/card/internal/ui/StoredCardDelegate.kt b/card/src/main/java/com/adyen/checkout/card/internal/ui/StoredCardDelegate.kt index b8d89601de..3e2f07677b 100644 --- a/card/src/main/java/com/adyen/checkout/card/internal/ui/StoredCardDelegate.kt +++ b/card/src/main/java/com/adyen/checkout/card/internal/ui/StoredCardDelegate.kt @@ -122,7 +122,7 @@ internal class StoredCardDelegate( private val _viewFlow: MutableStateFlow = MutableStateFlow(null) override val viewFlow: Flow = _viewFlow - override val submitFlow: Flow = submitHandler.submitFlow + override val submitFlow: Flow = getTrackedSubmitFlow() override val uiStateFlow: Flow = submitHandler.uiStateFlow override val uiEventFlow: Flow = submitHandler.uiEventFlow @@ -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) } diff --git a/card/src/test/java/com/adyen/checkout/card/internal/ui/StoredCardDelegateTest.kt b/card/src/test/java/com/adyen/checkout/card/internal/ui/StoredCardDelegateTest.kt index 46f13ffb7f..e1b32b3719 100644 --- a/card/src/test/java/com/adyen/checkout/card/internal/ui/StoredCardDelegateTest.kt +++ b/card/src/test/java/com/adyen/checkout/card/internal/ui/StoredCardDelegateTest.kt @@ -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 @@ -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 @@ -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 { @@ -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 { 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 @@ -515,7 +524,7 @@ internal class StoredCardDelegateTest( submitHandler = submitHandler, order = order, cardConfigDataGenerator = cardConfigDataGenerator, - cardValidationMapper = CardValidationMapper() + cardValidationMapper = CardValidationMapper(), ) }