Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nalcalag committed Dec 19, 2024
1 parent 78411ea commit 1e619b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import com.duckduckgo.subscriptions.api.SubscriptionStatus.NOT_AUTO_RENEWABLE
import com.duckduckgo.subscriptions.api.SubscriptionStatus.UNKNOWN
import com.duckduckgo.subscriptions.api.SubscriptionStatus.WAITING
import com.duckduckgo.subscriptions.impl.RealSubscriptionsManager.RecoverSubscriptionResult
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.LEGACY_FE_ITR
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.LEGACY_FE_NETP
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.LEGACY_FE_PIR
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.MONTHLY_PLAN_ROW
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.MONTHLY_PLAN_US
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.NETP
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.ROW_ITR
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.YEARLY_PLAN_ROW
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.YEARLY_PLAN_US
import com.duckduckgo.subscriptions.impl.auth2.AccessTokenClaims
Expand Down Expand Up @@ -106,7 +110,10 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {

@SuppressLint("DenyListedApi")
private val privacyProFeature: PrivacyProFeature = FakeFeatureToggleFactory.create(PrivacyProFeature::class.java)
.apply { authApiV2().setRawStoredState(State(authApiV2Enabled)) }
.apply {
authApiV2().setRawStoredState(State(authApiV2Enabled))
featuresApi().setRawStoredState(State(true))
}
private val authClient: AuthClient = mock()
private val pkceGenerator: PkceGenerator = PkceGeneratorImpl()
private val authJwtValidator: AuthJwtValidator = mock()
Expand Down Expand Up @@ -1132,7 +1139,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {

@Test
fun whenGetSubscriptionOfferThenReturnValue() = runTest {
authRepository.setFeatures(MONTHLY_PLAN_US, setOf(NETP))
privacyProFeature.featuresApi().setRawStoredState(State(false))
givenPlansAvailable(MONTHLY_PLAN_US, YEARLY_PLAN_US)

val subscriptionOffers = subscriptionsManager.getSubscriptionOffer()
Expand All @@ -1142,7 +1149,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
assertEquals("1$", find { it.planId == MONTHLY_PLAN_US }?.pricingPhases?.first()?.formattedPrice)
assertTrue(any { it.planId == YEARLY_PLAN_US })
assertEquals("1$", find { it.planId == YEARLY_PLAN_US }?.pricingPhases?.first()?.formattedPrice)
assertEquals(setOf(NETP), first().features)
assertEquals(setOf(LEGACY_FE_NETP, LEGACY_FE_PIR, LEGACY_FE_ITR), first().features)
}
}

Expand All @@ -1151,12 +1158,12 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
authRepository.setFeatures(MONTHLY_PLAN_US, emptySet())
givenPlansAvailable(MONTHLY_PLAN_US, YEARLY_PLAN_US)

assertNull(subscriptionsManager.getSubscriptionOffer())
assertTrue(subscriptionsManager.getSubscriptionOffer().isEmpty())
}

@Test
fun whenGetSubscriptionOfferAndRowPlansAvailableThenReturnValue() = runTest {
authRepository.setFeatures(MONTHLY_PLAN_ROW, setOf(NETP))
privacyProFeature.featuresApi().setRawStoredState(State(false))
givenPlansAvailable(MONTHLY_PLAN_ROW, YEARLY_PLAN_ROW)
givenIsLaunchedRow(true)

Expand All @@ -1167,7 +1174,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
assertEquals("1$", find { it.planId == MONTHLY_PLAN_ROW }?.pricingPhases?.first()?.formattedPrice)
assertTrue(any { it.planId == YEARLY_PLAN_ROW })
assertEquals("1$", find { it.planId == YEARLY_PLAN_ROW }?.pricingPhases?.first()?.formattedPrice)
assertEquals(setOf(NETP), first().features)
assertEquals(setOf(NETP, ROW_ITR), first().features)
}
}

Expand All @@ -1177,7 +1184,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenPlansAvailable(MONTHLY_PLAN_ROW, YEARLY_PLAN_ROW)
givenIsLaunchedRow(false)

assertNull(subscriptionsManager.getSubscriptionOffer())
assertTrue(subscriptionsManager.getSubscriptionOffer().isEmpty())
}

@Test
Expand Down Expand Up @@ -1533,6 +1540,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
val offers = basePlanIds.map { basePlanId ->
mock<SubscriptionOfferDetails> { offer ->
whenever(offer.basePlanId).thenReturn(basePlanId)
whenever(offer.offerId).thenReturn(null)
whenever(offer.pricingPhases).thenReturn(pricingPhases)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.duckduckgo.subscriptions.impl.PrivacyProFeature
import com.duckduckgo.subscriptions.impl.SubscriptionOfferDetails
import com.duckduckgo.subscriptions.impl.SubscriptionsChecker
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.MONTHLY_PLAN_US
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.YEARLY_PLAN_US
import com.duckduckgo.subscriptions.impl.SubscriptionsManager
import com.duckduckgo.subscriptions.impl.pixels.SubscriptionPixelSender
import com.duckduckgo.subscriptions.impl.ui.SubscriptionWebViewViewModel.Command
Expand Down Expand Up @@ -199,20 +201,20 @@ class SubscriptionWebViewViewModelTest {
fun whenGetSubscriptionOptionsThenSendCommand() = runTest {
val testSubscriptionOfferList = listOf(
SubscriptionOfferDetails(
planId = "monthly",
planId = MONTHLY_PLAN_US,
offerId = null,
pricingPhases = listOf(PricingPhase(formattedPrice = "$1")),
features = setOf(SubscriptionsConstants.NETP),
),
SubscriptionOfferDetails(
planId = "yearly",
planId = YEARLY_PLAN_US,
offerId = null,
pricingPhases = listOf(PricingPhase(formattedPrice = "$10")),
features = setOf(SubscriptionsConstants.NETP),
),
)
privacyProFeature.allowPurchase().setRawStoredState(Toggle.State(enable = true))
whenever(subscriptionsManager.getSubscriptionOffer()).thenReturn(testSubscriptionOfferList)
privacyProFeature.allowPurchase().setRawStoredState(Toggle.State(enable = true))

viewModel.commands().test {
viewModel.processJsCallbackMessage("test", "getSubscriptionOptions", "id", JSONObject("{}"))
Expand All @@ -224,8 +226,8 @@ class SubscriptionWebViewViewModelTest {
assertEquals("id", response.id)
assertEquals("test", response.featureName)
assertEquals("getSubscriptionOptions", response.method)
assertEquals("yearly", params?.options?.first()?.id)
assertEquals("monthly", params?.options?.last()?.id)
assertEquals(YEARLY_PLAN_US, params?.options?.first()?.id)
assertEquals(MONTHLY_PLAN_US, params?.options?.last()?.id)
}
}

Expand Down Expand Up @@ -254,13 +256,13 @@ class SubscriptionWebViewViewModelTest {
fun whenGetSubscriptionsAndToggleOffThenSendCommandWithEmptyData() = runTest {
val testSubscriptionOfferList = listOf(
SubscriptionOfferDetails(
planId = "monthly",
planId = MONTHLY_PLAN_US,
offerId = null,
pricingPhases = listOf(PricingPhase(formattedPrice = "$1")),
features = setOf(SubscriptionsConstants.NETP),
),
SubscriptionOfferDetails(
planId = "yearly",
planId = YEARLY_PLAN_US,
offerId = null,
pricingPhases = listOf(PricingPhase(formattedPrice = "$10")),
features = setOf(SubscriptionsConstants.NETP),
Expand Down

0 comments on commit 1e619b1

Please sign in to comment.