Skip to content

Commit

Permalink
Refactor existing unit tests and add new. Add DomainRegistrationTrack…
Browse files Browse the repository at this point in the history
…er to resolve the dependency conflict"
  • Loading branch information
zwarm committed Jan 12, 2024
1 parent bdcb769 commit f0fc640
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
}

Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<MySiteCardAndItem.Card.DomainRegistrationCard>

private lateinit var viewModelSlice: DomainRegistrationViewModelSlice

private lateinit var isRefreshing: MutableList<Boolean>
private lateinit var navigationActions: MutableList<SiteNavigationAction>
private lateinit var viewModelSlice: DomainRegistrationViewModelSlice

@Before
fun setUp() {
Expand All @@ -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())
}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit f0fc640

Please sign in to comment.