From f0fc64086a98b89f259afc0f4e4007ad43554fe7 Mon Sep 17 00:00:00 2001 From: Annmarie Ziegler Date: Fri, 12 Jan 2024 15:52:04 -0500 Subject: [PATCH] Refactor existing unit tests and add new. Add DomainRegistrationTracker to resolve the dependency conflict" --- .../DomainRegistrationViewModelSlice.kt | 10 +++- .../DomainRegistrationViewModelSliceTest.kt | 56 ++++++++++++------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/domainregistration/DomainRegistrationViewModelSlice.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/domainregistration/DomainRegistrationViewModelSlice.kt index 7de703940fb4..c6927313ba17 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/domainregistration/DomainRegistrationViewModelSlice.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/domainregistration/DomainRegistrationViewModelSlice.kt @@ -36,7 +36,7 @@ class DomainRegistrationViewModelSlice @Inject constructor( private val selectedSiteRepository: SelectedSiteRepository, private val appLogWrapper: AppLogWrapper, private val siteUtils: SiteUtilsWrapper, - private val analyticsTrackerWrapper: AnalyticsTrackerWrapper + private val domainRegistrationTracker: DomainRegistrationTracker ) { private lateinit var scope: CoroutineScope @@ -125,7 +125,7 @@ class DomainRegistrationViewModelSlice @Inject constructor( private fun domainRegistrationClick() { val selectedSite = requireNotNull(selectedSiteRepository.getSelectedSite()) - analyticsTrackerWrapper.track(AnalyticsTracker.Stat.DOMAIN_CREDIT_REDEMPTION_TAPPED, selectedSite) + domainRegistrationTracker.track(AnalyticsTracker.Stat.DOMAIN_CREDIT_REDEMPTION_TAPPED, selectedSite) _onNavigation.value = Event(SiteNavigationAction.OpenDomainRegistration(selectedSite)) } @@ -139,3 +139,9 @@ class DomainRegistrationViewModelSlice @Inject constructor( continuations[event.site.id] = null } } + +/* This class is a helper to offset the AppLogWrapper dependency conflict (see AppLogWrapper itself for more info) */ +class DomainRegistrationTracker +@Inject constructor(private val analyticsTrackerWrapper: AnalyticsTrackerWrapper) { + fun track(stat: AnalyticsTracker.Stat, site: SiteModel) = analyticsTrackerWrapper.track(stat, site) +} diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/domainregistration/DomainRegistrationViewModelSliceTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/domainregistration/DomainRegistrationViewModelSliceTest.kt index c76a294fbdd3..34c089a253fc 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/domainregistration/DomainRegistrationViewModelSliceTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/domainregistration/DomainRegistrationViewModelSliceTest.kt @@ -3,9 +3,10 @@ package org.wordpress.android.ui.mysite.cards.domainregistration import kotlinx.coroutines.ExperimentalCoroutinesApi import org.assertj.core.api.Assertions.assertThat import org.junit.Before -import org.junit.Ignore import org.junit.Test +import org.junit.runner.RunWith import org.mockito.Mock +import org.mockito.junit.MockitoJUnitRunner import org.mockito.kotlin.any import org.mockito.kotlin.never import org.mockito.kotlin.times @@ -22,12 +23,12 @@ import org.wordpress.android.fluxc.store.SiteStore.PlansErrorType.GENERIC_ERROR import org.wordpress.android.fluxc.utils.AppLogWrapper import org.wordpress.android.ui.mysite.MySiteCardAndItem import org.wordpress.android.ui.mysite.SelectedSiteRepository +import org.wordpress.android.ui.mysite.SiteNavigationAction import org.wordpress.android.ui.plans.PlansConstants.PREMIUM_PLAN_ID import org.wordpress.android.util.SiteUtilsWrapper -import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper @ExperimentalCoroutinesApi -@Ignore("Update tests to work with new architecture") +@RunWith(MockitoJUnitRunner::class) class DomainRegistrationViewModelSliceTest : BaseUnitTest() { @Mock lateinit var dispatcher: Dispatcher @@ -42,15 +43,14 @@ class DomainRegistrationViewModelSliceTest : BaseUnitTest() { lateinit var siteUtils: SiteUtilsWrapper @Mock - lateinit var analyticsTrackerWrapper: AnalyticsTrackerWrapper + lateinit var domainRegistrationTracker: DomainRegistrationTracker private val siteLocalId = 1 private val site = SiteModel() private lateinit var result: MutableList - - private lateinit var viewModelSlice: DomainRegistrationViewModelSlice - private lateinit var isRefreshing: MutableList + private lateinit var navigationActions: MutableList + private lateinit var viewModelSlice: DomainRegistrationViewModelSlice @Before fun setUp() { @@ -62,20 +62,33 @@ class DomainRegistrationViewModelSliceTest : BaseUnitTest() { selectedSiteRepository, appLogWrapper, siteUtils, - analyticsTrackerWrapper + domainRegistrationTracker ) - result = mutableListOf() - viewModelSlice.uiModel.observeForever { result.add(it) } + viewModelSlice.initialize(testScope()) + result = mutableListOf() + navigationActions = mutableListOf() isRefreshing = mutableListOf() + + viewModelSlice.uiModel.observeForever { result.add(it) } viewModelSlice.isRefreshing.observeForever { isRefreshing.add(it) } + viewModelSlice.onNavigation.observeForever { event -> + event?.getContentIfNotHandled()?.let { navigationActions.add(it) } + } + } + + @Test + fun `when getData is invoked, then refresh is true`() = test { + viewModelSlice.getData(siteLocalId, site) + + assertThat(isRefreshing.last()).isTrue } @Test fun `when site is free, emit false and don't fetch`() = test { setupSite(site = site, isFree = true) - assertThat(result.last()).isNull() + assertThat(result).isEmpty() verify(dispatcher, never()).dispatch(any()) } @@ -93,7 +106,7 @@ class DomainRegistrationViewModelSliceTest : BaseUnitTest() { fun `when fetched site doesn't have a plan with credits, start to fetch and emit false`() = test { setupSite(site = site, currentPlan = buildPlan(hasDomainCredit = false)) - assertThat(result.last()).isNull() + assertThat(result).isEmpty() } @Test @@ -108,22 +121,17 @@ class DomainRegistrationViewModelSliceTest : BaseUnitTest() { viewModelSlice.onPlansFetched(event) } - assertThat(result.last()).isNull() + assertThat(result).isEmpty() } @Test fun `when fetch fails, emit false value`() = test { setupSite(site = site, error = GENERIC_ERROR) - assertThat(result.last()).isNull() + assertThat(result).isEmpty() } - @Test - fun `when build is invoked, then refresh is true`() = test { - viewModelSlice.getData(siteLocalId, site) - assertThat(isRefreshing.last()).isTrue - } @Test fun `when data has been refreshed, then refresh is set to false`() = test { @@ -132,6 +140,16 @@ class DomainRegistrationViewModelSliceTest : BaseUnitTest() { assertThat(isRefreshing.last()).isFalse } + @Test + fun `given card is built, when domain registration click, then navigate to domain registration`() = test { + setupSite(site = site, currentPlan = buildPlan(hasDomainCredit = true)) + + assertThat(result.last()).isNotNull + + result.last().onClick.click() + + assertThat(navigationActions.last()).isEqualTo(SiteNavigationAction.OpenDomainRegistration(site)) + } private fun setupSite( site: SiteModel, isFree: Boolean = false,