Skip to content

Instantly share code, notes, and snippets.

@waliahimanshu

waliahimanshu/kt Secret

Created June 8, 2022 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waliahimanshu/d918a2ca42e210da4ed2c7b130766417 to your computer and use it in GitHub Desktop.
Save waliahimanshu/d918a2ca42e210da4ed2c7b130766417 to your computer and use it in GitHub Desktop.
detket function signature error
package com.example.yelpedapp
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.core.content.res.ResourcesCompat
import com.example.yelpedapp.FeatureToggle.*
import com.example.yelpedapp.PremiumFeature.*
import com.example.yelpedapp.SubscriptionSource.*
import com.flextrade.kfixture.KFixture
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
@RunWith(Parameterized::class)
class WelcomeNewPsUserViewModelViewStatesTest(
private val subscriptionSource: SubscriptionSource,
private val enabledFeatureToggles: List<FeatureToggle>,
private val expectedHighlightFeature: PremiumFeature,
private val expectedShowcaseFeatureList: List<PremiumFeature>,
) {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
@get:Rule
val coroutineRule = TestCoroutineRule()
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
private val mockCurrentUserRepository: CurrentUserRepository = mock()
private val mockFeatureToggleRepository: FeatureTogglesRepository = mock()
private val kFixture = KFixture()
private val fakeFeaturesViewStates = hashMapOf(
PREMIUM_SEARCH to (kFixture<PremiumFeatureViewState>().copy(premiumFeature = PREMIUM_SEARCH)),
HALL_OF_FAME to (kFixture<PremiumFeatureViewState>().copy(premiumFeature = HALL_OF_FAME)),
PREMIUM_FILTERS to (kFixture<PremiumFeatureViewState>().copy(premiumFeature = PREMIUM_FILTERS)),
UNLIMITED_SAVED_RECIPES to (kFixture<PremiumFeatureViewState>().copy(premiumFeature = UNLIMITED_SAVED_RECIPES)),
AD_FREE to (kFixture<PremiumFeatureViewState>().copy(premiumFeature = AD_FREE)),
RELATED_RECIPES to (kFixture<PremiumFeatureViewState>().copy(premiumFeature = RELATED_RECIPES)),
)
@Before
fun setup() {
reset(mockCurrentUserRepository, mockFeatureToggleRepository)
}
@Test
fun `Given a SubscriptionSource and a set of Feature Toggles When vm initialised Then correct view state for reason is set`() {
givenEnabledFeatureToggles()
val expectedViewState = givenSubscriptionSource()
createViewModel(subscriptionSource).viewState.test().assertMostRecentEvent(expectedViewState)
}
private fun givenSubscriptionSource(): UserSuccessfullySubscribeViewState {
val fakeUser = kFixture<CurrentUser>()
val fakeHighlightFeature = getFakeFeatureViewState(expectedHighlightFeature)
val fakeShowcaseFeatureList = expectedShowcaseFeatureList.map(::getFakeFeatureViewState)
coroutineRule.runBlockingTest {
whenever(mockCurrentUserRepository.requireUser()).thenReturn(fakeUser)
}
return UserSuccessfullySubscribeViewState(
userName = fakeUser.name.orEmpty(),
highlightFeature = fakeHighlightFeature,
featuresList = fakeShowcaseFeatureList
)
}
private fun givenEnabledFeatureToggles() {
FeatureToggle.values()
.filter { !enabledFeatureToggles.contains(it) }
.forEach { whenever(mockFeatureToggleRepository.isFeatureEnabled(it)).thenReturn(false) }
enabledFeatureToggles.forEach {
whenever(mockFeatureToggleRepository.isFeatureEnabled(it)).thenReturn(true)
}
}
private fun createViewModel(subscriptionSource: SubscriptionSource, query: String = "") = WelcomeNewPsUserViewModel(
welcomeScreenArgs = WelcomeNewPsUserFragmentArgs(subscriptionSource, query),
currentUserRepository = mockCurrentUserRepository,
logger = mock(),
analytics = mock(),
psFeatureViewStateFactory = ::getFakeFeatureViewState,
featureTogglesRepository = mockFeatureToggleRepository,
)
private fun getFakeFeatureViewState(
feature: PremiumFeature,
isSearchActive: Boolean = false,
): PremiumFeatureViewState {
return fakeFeaturesViewStates[feature]
?: throw RuntimeException("Fake Features List Must Contain all Features for Testing")
}
companion object {
@JvmStatic
@Parameterized.Parameters(
name = "{index}-Case-subscriptionSource-{0}-featureToggles-{1}-expectedHighlightFeature-{2}-expectedShowcaseFeatureList-{3}"
)
fun data(): List<Array<out Any?>> =
listOf(
arrayOf(CTA_PREMIUM_SEARCH, emptyList<FeatureToggle>(),
PREMIUM_SEARCH, listOf(AD_FREE)),
arrayOf(
CTA_PREMIUM_SEARCH,
listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
PREMIUM_SEARCH,
listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_PREMIUM_SEARCH,
listOf(SAVES_LIMIT_TEST),
PREMIUM_SEARCH,
listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_PREMIUM_SEARCH,
listOf(SAVES_LIMIT_PROMOTION),
PREMIUM_SEARCH,
listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_PREMIUM_SEARCH,
listOf(HALL_OF_FAME),
PREMIUM_SEARCH,
listOf(HALL_OF_FAME, AD_FREE)
),
arrayOf(
CTA_PREMIUM_SEARCH,
listOf(PREMIUM_SEARCH_FILTERS),
PREMIUM_SEARCH,
listOf(PREMIUM_FILTERS, AD_FREE)
),
arrayOf(
CTA_PREMIUM_SEARCH,
listOf(HALL_OF_FAME, SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION, PREMIUM_SEARCH_FILTERS),
PREMIUM_SEARCH,
listOf(UNLIMITED_SAVED_RECIPES, PREMIUM_FILTERS, HALL_OF_FAME, AD_FREE)
),
arrayOf(
CTA_PREMIUM_FILTERS,
emptyList<FeatureToggle>(),
PREMIUM_FILTERS,
listOf(PREMIUM_SEARCH, AD_FREE)
),
arrayOf(
CTA_PREMIUM_FILTERS,
listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
PREMIUM_FILTERS,
listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_PREMIUM_FILTERS,
listOf(HALL_OF_FAME),
PREMIUM_FILTERS,
listOf(PREMIUM_SEARCH, HALL_OF_FAME, AD_FREE)
),
arrayOf(
CTA_PREMIUM_FILTERS,
listOf(HALL_OF_FAME, SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
PREMIUM_FILTERS,
listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, HALL_OF_FAME, AD_FREE)
),
arrayOf(CTA_AGNOSTIC, emptyList<FeatureToggle>(), PREMIUM_SEARCH, listOf(AD_FREE)),
arrayOf(
CTA_AGNOSTIC,
listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
PREMIUM_SEARCH,
listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_AGNOSTIC,
listOf(SAVES_LIMIT_TEST),
PREMIUM_SEARCH,
listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_AGNOSTIC,
listOf(SAVES_LIMIT_PROMOTION),
PREMIUM_SEARCH,
listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_AGNOSTIC,
listOf(HALL_OF_FAME),
PREMIUM_SEARCH,
listOf(HALL_OF_FAME, AD_FREE)
),
arrayOf(
CTA_AGNOSTIC,
listOf(PREMIUM_SEARCH_FILTERS),
PREMIUM_SEARCH,
listOf(PREMIUM_FILTERS, AD_FREE)
),
arrayOf(
CTA_AGNOSTIC,
listOf(HALL_OF_FAME, PREMIUM_SEARCH_FILTERS, SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
PREMIUM_SEARCH,
listOf(UNLIMITED_SAVED_RECIPES, PREMIUM_FILTERS, HALL_OF_FAME, AD_FREE)
),
arrayOf(CTA_HALL_OF_FAME, emptyList<FeatureToggle>(), HALL_OF_FAME, listOf(PREMIUM_SEARCH, AD_FREE)),
arrayOf(
CTA_HALL_OF_FAME,
listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
HALL_OF_FAME,
listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_HALL_OF_FAME,
listOf(SAVES_LIMIT_TEST),
HALL_OF_FAME,
listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_HALL_OF_FAME,
listOf(SAVES_LIMIT_PROMOTION),
HALL_OF_FAME,
listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, AD_FREE)
),
arrayOf(
CTA_HALL_OF_FAME,
listOf(PREMIUM_SEARCH_FILTERS),
HALL_OF_FAME,
listOf(PREMIUM_SEARCH, PREMIUM_FILTERS, AD_FREE)
),
arrayOf(
CTA_UNLIMITED_SAVED_RECIPES,
emptyList<FeatureToggle>(),
UNLIMITED_SAVED_RECIPES,
listOf(PREMIUM_SEARCH, AD_FREE)
),
arrayOf(
CTA_UNLIMITED_SAVED_RECIPES,
listOf(HALL_OF_FAME),
UNLIMITED_SAVED_RECIPES,
listOf(PREMIUM_SEARCH, HALL_OF_FAME, AD_FREE)
),
arrayOf(
CTA_UNLIMITED_SAVED_RECIPES,
listOf(PREMIUM_SEARCH_FILTERS),
UNLIMITED_SAVED_RECIPES,
listOf(PREMIUM_SEARCH, PREMIUM_FILTERS, AD_FREE)
),
arrayOf(
CTA_RELATED_RECIPES,
emptyList<FeatureToggle>(),
RELATED_RECIPES,
listOf(PREMIUM_SEARCH, AD_FREE)
),
arrayOf(
CTA_RELATED_RECIPES,
listOf(RECIPE_PS_RECOMMENDATIONS),
RELATED_RECIPES,
listOf(PREMIUM_SEARCH, AD_FREE)
),
)
}
}
enum class FeatureToggle {
SAVES_LIMIT_PROMOTION, SAVES_LIMIT_TEST, RECIPE_PS_RECOMMENDATIONS, PREMIUM_SEARCH_FILTERS
}
enum class PremiumFeature {
HALL_OF_FAME,
UNLIMITED_SAVED_RECIPES,
PREMIUM_SEARCH,
PREMIUM_FILTERS,
AD_FREE,
RELATED_RECIPES,
}
enum class SubscriptionSource {
CTA_UNLIMITED_SAVED_RECIPES,
CTA_PREMIUM_SEARCH,
CTA_PREMIUM_FILTERS,
CTA_HALL_OF_FAME,
CTA_AGNOSTIC,
CTA_SEASONAL_PREMIUM_CAMPAIGN,
CTA_RELATED_RECIPES,
NONE,
}
data class PremiumFeatureViewState(
val premiumFeature: PremiumFeature,
@DrawableRes val icon: Int,
@StringRes val featureName: Int,
@StringRes val message: Int,
@StringRes val ctaText: Int,
@ColorRes val iconTint: Int = ResourcesCompat.ID_NULL,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment