diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/WooPosHomeViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/WooPosHomeViewModel.kt index 23e7eabbba2..947b3200ae1 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/WooPosHomeViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/WooPosHomeViewModel.kt @@ -8,6 +8,7 @@ import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent.OrderSuccess import com.woocommerce.android.ui.woopos.home.WooPosHomeState.ExitConfirmationDialog import com.woocommerce.android.ui.woopos.home.WooPosHomeState.ProductsInfoDialog import com.woocommerce.android.ui.woopos.home.WooPosHomeState.ScreenPositionState +import com.woocommerce.android.ui.woopos.home.items.navigation.WooPosItemsNavigator import com.woocommerce.android.viewmodel.getStateFlow import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableSharedFlow @@ -20,6 +21,7 @@ import javax.inject.Inject class WooPosHomeViewModel @Inject constructor( private val childrenToParentEventReceiver: WooPosChildrenToParentEventReceiver, private val parentToChildrenEventSender: WooPosParentToChildrenEventSender, + private val wooPosItemsNavigator: WooPosItemsNavigator, savedStateHandle: SavedStateHandle, ) : ViewModel() { private val _state = savedStateHandle.getStateFlow( @@ -200,6 +202,11 @@ class WooPosHomeViewModel @Inject constructor( } private fun onOrderSuccessfullyPaid(paymentMethod: PaymentMethod) { + viewModelScope.launch { + wooPosItemsNavigator.sendNavigationEvent( + WooPosItemsNavigator.WooPosItemsScreenNavigationEvent.NavigateBackToItemListScreen + ) + } _state.value = _state.value.copy( screenPositionState = ScreenPositionState.Checkout.FullScreenTotals ) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt index 5b607f56b42..40da8f0109e 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt @@ -28,7 +28,6 @@ import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent.OrderSuccess import com.woocommerce.android.ui.woopos.home.WooPosChildrenToParentEventSender import com.woocommerce.android.ui.woopos.home.WooPosParentToChildrenEventReceiver import com.woocommerce.android.ui.woopos.home.items.WooPosItemsViewModel -import com.woocommerce.android.ui.woopos.home.items.navigation.WooPosItemsNavigator import com.woocommerce.android.ui.woopos.home.totals.WooPosTotalsViewState.PaymentFailed import com.woocommerce.android.ui.woopos.home.totals.WooPosTotalsViewState.PaymentInProgress import com.woocommerce.android.ui.woopos.util.WooPosNetworkStatus @@ -62,7 +61,6 @@ class WooPosTotalsViewModel @Inject constructor( private val priceFormat: WooPosFormatPrice, private val analyticsTracker: WooPosAnalyticsTracker, private val networkStatus: WooPosNetworkStatus, - private val wooPosItemsNavigator: WooPosItemsNavigator, private val isReceiptSendingSupported: WooPosEmailReceiptIsSendingSupported, private val cardReaderPaymentControllerFactory: CardReaderPaymentControllerFactory, private val uiStringParser: UiStringParser, @@ -305,9 +303,6 @@ class WooPosTotalsViewModel @Inject constructor( } is CardReaderPaymentState.PaymentSuccessful -> { - wooPosItemsNavigator.sendNavigationEvent( - WooPosItemsNavigator.WooPosItemsScreenNavigationEvent.NavigateBackToItemListScreen - ) childrenToParentEventSender.sendToParent(OrderSuccessfullyPaidByCard) } diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/WooPosHomeViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/WooPosHomeViewModelTest.kt index da236266e64..2d27b82a186 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/WooPosHomeViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/WooPosHomeViewModelTest.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.SavedStateHandle import com.woocommerce.android.R import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent.OrderSuccessfullyPaid.PaymentMethod import com.woocommerce.android.ui.woopos.home.items.WooPosItemsViewModel.ItemClickedData +import com.woocommerce.android.ui.woopos.home.items.navigation.WooPosItemsNavigator import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableSharedFlow @@ -30,6 +31,7 @@ class WooPosHomeViewModelTest { private val childrenToParentEventReceiver: WooPosChildrenToParentEventReceiver = mock() private val parentToChildrenEventSender: WooPosParentToChildrenEventSender = mock() + private val wooPosItemsNavigator: WooPosItemsNavigator = mock() @Test fun `given state checkout, when SystemBackClicked passed, then BackFromCheckoutToCartClicked event should be sent`() = @@ -398,9 +400,44 @@ class WooPosHomeViewModelTest { .isEqualTo(WooPosHomeState.ScreenPositionState.Checkout.FullScreenTotals) } + @Test + fun `given OrderSuccessfullyPaid by card, then redirect back to items screen`() = + runTest { + // GIVEN + whenever(childrenToParentEventReceiver.events).thenReturn( + flowOf(ChildToParentEvent.OrderSuccessfullyPaidByCard) + ) + + // WHEN + createViewModel() + + // THEN + verify(wooPosItemsNavigator).sendNavigationEvent( + WooPosItemsNavigator.WooPosItemsScreenNavigationEvent.NavigateBackToItemListScreen + ) + } + + @Test + fun `given OrderSuccessfullyPaid by cash, then redirect back to items screen`() = + runTest { + // GIVEN + val events = MutableSharedFlow() + whenever(childrenToParentEventReceiver.events).thenReturn(events) + val viewModel = createViewModel() + + // WHEN + viewModel.onUIEvent(WooPosHomeUIEvent.OnPaymentCompletedViaCash) + + // THEN + verify(wooPosItemsNavigator).sendNavigationEvent( + WooPosItemsNavigator.WooPosItemsScreenNavigationEvent.NavigateBackToItemListScreen + ) + } + private fun createViewModel() = WooPosHomeViewModel( childrenToParentEventReceiver, parentToChildrenEventSender, + wooPosItemsNavigator, SavedStateHandle() ) } diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt index 08eca2b3a60..5a2071ed68d 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt @@ -39,7 +39,6 @@ import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent.OrderSuccess import com.woocommerce.android.ui.woopos.home.WooPosChildrenToParentEventSender import com.woocommerce.android.ui.woopos.home.WooPosParentToChildrenEventReceiver import com.woocommerce.android.ui.woopos.home.items.WooPosItemsViewModel -import com.woocommerce.android.ui.woopos.home.items.navigation.WooPosItemsNavigator import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule import com.woocommerce.android.ui.woopos.util.WooPosNetworkStatus import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent @@ -81,8 +80,6 @@ class WooPosTotalsViewModelTest { private val networkStatus: WooPosNetworkStatus = mock() - private val wooPosItemsNavigator: WooPosItemsNavigator = mock() - private val childrenToParentEventSender: WooPosChildrenToParentEventSender = mock() private val resourceProvider: ResourceProvider = mock() private val cardReaderManager: CardReaderManager = mock() @@ -1251,7 +1248,6 @@ class WooPosTotalsViewModelTest { analyticsTracker = analyticsTracker, networkStatus = networkStatus, cardReaderPaymentControllerFactory = cardReaderPaymentControllerFactory, - wooPosItemsNavigator = wooPosItemsNavigator, isReceiptSendingSupported = isReceiptSendingSupported, uiStringParser = uiStringParser, savedState = savedState,