diff --git a/app/src/main/graphql/fragments.graphql b/app/src/main/graphql/fragments.graphql index 0b240ef644..6c4e036d14 100644 --- a/app/src/main/graphql/fragments.graphql +++ b/app/src/main/graphql/fragments.graphql @@ -104,27 +104,9 @@ fragment fullProject on Project { prelaunchActivated ...tagsCreative ...tagsDiscovery - rewards { + rewards { # query within project the bare minimum for rewards nodes { ... reward - allowedAddons { - pageInfo { - startCursor - } - } - items { - ... rewardItems - } - ... on Reward { - simpleShippingRulesExpanded { - cost - country - estimatedMax - estimatedMin - locationId - locationName - } - } } } risks diff --git a/app/src/main/graphql/project.graphql b/app/src/main/graphql/project.graphql index 0058a3fd4c..c8f94145be 100644 --- a/app/src/main/graphql/project.graphql +++ b/app/src/main/graphql/project.graphql @@ -19,6 +19,33 @@ query FetchProject($slug: String!) { } } +query FetchProjectRewards($slug: String!) { + project(slug: $slug) { + minPledge + rewards { + nodes { + ... reward + allowedAddons { + pageInfo { + startCursor + } + } + items { + ... rewardItems + } + simpleShippingRulesExpanded { + cost + country + estimatedMax + estimatedMin + locationId + locationName + } + } + } + } +} + query ProjectCreatorDetails($slug: String!) { project(slug: $slug) { creator { diff --git a/app/src/main/java/com/kickstarter/libs/utils/extensions/PledgeDataExt.kt b/app/src/main/java/com/kickstarter/libs/utils/extensions/PledgeDataExt.kt index 26c39f67ef..d54766c7e7 100644 --- a/app/src/main/java/com/kickstarter/libs/utils/extensions/PledgeDataExt.kt +++ b/app/src/main/java/com/kickstarter/libs/utils/extensions/PledgeDataExt.kt @@ -28,7 +28,7 @@ fun PledgeData.shippingCostIfShipping(): Double { var addOnsShippingCost = 0.0 this.addOns()?.map { if (RewardUtils.shipsWorldwide(it) || RewardUtils.shipsToRestrictedLocations(it)) { - addOnsShippingCost += (it.shippingRules()?.first()?.cost() ?: 0.0) * (it.quantity() ?: 0) + addOnsShippingCost += (it.shippingRules()?.firstOrNull()?.cost() ?: 0.0) * (it.quantity() ?: 0) } else 0.0 } diff --git a/app/src/main/java/com/kickstarter/mock/services/MockApolloClientV2.kt b/app/src/main/java/com/kickstarter/mock/services/MockApolloClientV2.kt index ae7731bf76..abf3efb5e8 100644 --- a/app/src/main/java/com/kickstarter/mock/services/MockApolloClientV2.kt +++ b/app/src/main/java/com/kickstarter/mock/services/MockApolloClientV2.kt @@ -53,6 +53,10 @@ import java.util.Collections open class MockApolloClientV2 : ApolloClientTypeV2 { + override fun getRewardsFromProject(slug: String): io.reactivex.Observable> { + return io.reactivex.Observable.just(emptyList()) + } + override fun watchProject(project: Project): io.reactivex.Observable { return io.reactivex.Observable.just(project.toBuilder().isStarred(true).build()) } diff --git a/app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt b/app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt index 975f03e64d..1f14cfe756 100644 --- a/app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt +++ b/app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt @@ -15,6 +15,7 @@ import DeletePaymentSourceMutation import ErroredBackingsQuery import FetchCategoryQuery import FetchProjectQuery +import FetchProjectRewardsQuery import FetchProjectsQuery import GetBackingQuery import GetCommentQuery @@ -52,6 +53,7 @@ import com.kickstarter.features.pledgedprojectsoverview.data.PledgedProjectsOver import com.kickstarter.libs.utils.extensions.isNotNull import com.kickstarter.libs.utils.extensions.toBoolean import com.kickstarter.libs.utils.extensions.toProjectSort +import com.kickstarter.mock.factories.RewardFactory import com.kickstarter.models.Backing import com.kickstarter.models.Category import com.kickstarter.models.Checkout @@ -208,6 +210,7 @@ interface ApolloClientTypeV2 { fun createOrUpdateBackingAddress(eventInput: CreateOrUpdateBackingAddressData): Observable fun completeOrder(orderInput: CompleteOrderInput): Observable fun getPledgedProjectsOverviewPledges(inputData: PledgedProjectsOverviewQueryData): Observable + fun getRewardsFromProject(slug: String): Observable> } private const val PAGE_SIZE = 25 @@ -714,6 +717,45 @@ class KSApolloClientV2(val service: ApolloClient, val gson: Gson) : ApolloClient }.subscribeOn(Schedulers.io()) } + override fun getRewardsFromProject(slug: String): Observable> { + return Observable.defer { + val ps = PublishSubject.create>() + val query = FetchProjectRewardsQuery.builder() + .slug(slug) + .build() + + this.service.query(query) + .enqueue(object : ApolloCall.Callback() { + override fun onFailure(e: ApolloException) { + ps.onError(e) + } + + override fun onResponse(response: Response) { + if (response.hasErrors()) { + ps.onError(Exception(response.errors?.first()?.message)) + } + response.data?.let { data -> + val rwList = data.project()?.rewards()?.nodes()?.map { + rewardTransformer( + rewardGr = it.fragments().reward(), + allowedAddons = it.allowedAddons().pageInfo().startCursor()?.isNotEmpty() ?: false, + rewardItems = complexRewardItemsTransformer(it.items()?.fragments()?.rewardItems()), + simpleShippingRules = it.simpleShippingRulesExpanded() + ) + } ?: emptyList() + // - API does not provide the Reward no reward, we need to add it first + val minPledge = data.project()?.minPledge()?.toDouble() ?: 1.0 + val modifiedRewards = rwList.toMutableList() + modifiedRewards.add(0, RewardFactory.noReward().toBuilder().minimum(minPledge).build()) + ps.onNext(modifiedRewards.toList()) + } + ps.onComplete() + } + }) + return@defer ps + } + } + private fun getAddOnsFromProject(addOnsGr: GetProjectAddOnsQuery.AddOns): List { return addOnsGr.nodes()?.map { node -> val shippingRulesGr = diff --git a/app/src/main/java/com/kickstarter/services/transformers/GraphQLTransformers.kt b/app/src/main/java/com/kickstarter/services/transformers/GraphQLTransformers.kt index 94ee86b1e9..130a65dd9a 100644 --- a/app/src/main/java/com/kickstarter/services/transformers/GraphQLTransformers.kt +++ b/app/src/main/java/com/kickstarter/services/transformers/GraphQLTransformers.kt @@ -130,7 +130,7 @@ fun environmentalCommitmentTransformer(envCommit: fragment.EnvironmentalCommitme fun rewardTransformer( rewardGr: fragment.Reward, shippingRulesExpanded: List = emptyList(), - simpleShippingRules: List = emptyList(), + simpleShippingRules: List = emptyList(), allowedAddons: Boolean = false, rewardItems: List = emptyList(), addOnItems: List = emptyList() @@ -198,7 +198,7 @@ fun rewardTransformer( .build() } -fun simpleShippingRuleTransformer(simpleShippingRules: FullProject.SimpleShippingRulesExpanded): ShippingRule { +fun simpleShippingRuleTransformer(simpleShippingRules: FetchProjectRewardsQuery.SimpleShippingRulesExpanded): ShippingRule { val id = decodeRelayId(simpleShippingRules.locationId()) ?: -1 val country = simpleShippingRules.country() ?: "" val displayName = simpleShippingRules.locationName() @@ -327,12 +327,8 @@ fun projectTransformer(projectFragment: FullProject?): Project { val minPledge = projectFragment?.minPledge()?.toDouble() ?: 1.0 val rewards = projectFragment?.rewards()?.nodes()?.map { - val shippingRules = it.simpleShippingRulesExpanded() rewardTransformer( - it.fragments().reward(), - simpleShippingRules = shippingRules, - allowedAddons = it.allowedAddons().pageInfo().startCursor()?.isNotEmpty() ?: false, - rewardItems = complexRewardItemsTransformer(it.items()?.fragments()?.rewardItems()) + it.fragments().reward() ) } diff --git a/app/src/main/java/com/kickstarter/ui/activities/compose/projectpage/RewardCarouselScreen.kt b/app/src/main/java/com/kickstarter/ui/activities/compose/projectpage/RewardCarouselScreen.kt index 39690bc6f7..0b4bd2e22b 100644 --- a/app/src/main/java/com/kickstarter/ui/activities/compose/projectpage/RewardCarouselScreen.kt +++ b/app/src/main/java/com/kickstarter/ui/activities/compose/projectpage/RewardCarouselScreen.kt @@ -223,7 +223,8 @@ fun RewardCarouselScreen( id = R.string.Back_it_because_you_believe_in_it ), onRewardSelectClicked = { onRewardSelected(reward) }, - isCTAButtonVisible = project.isAllowedToPledge() + isCTAButtonVisible = project.isAllowedToPledge(), + yourSelectionIsVisible = project.backing()?.isBacked(reward) ?: false, ) } else { KSRewardCard( diff --git a/app/src/main/java/com/kickstarter/viewmodels/projectpage/AddOnsViewModel.kt b/app/src/main/java/com/kickstarter/viewmodels/projectpage/AddOnsViewModel.kt index c684bbe6fd..0796f2b3bf 100644 --- a/app/src/main/java/com/kickstarter/viewmodels/projectpage/AddOnsViewModel.kt +++ b/app/src/main/java/com/kickstarter/viewmodels/projectpage/AddOnsViewModel.kt @@ -150,7 +150,6 @@ class AddOnsViewModel(val environment: Environment, bundle: Bundle? = null) : Vi bonusAmount = b.amount() } else { backedAddOns = b.addOns() ?: emptyList() - currentUserReward = b.reward() ?: currentUserReward bonusAmount = b.bonusAmount() } } @@ -229,10 +228,14 @@ class AddOnsViewModel(val environment: Environment, bundle: Bundle? = null) : Vi holder[it.id()] = it } - // Take the backed AddOns, update with the backed AddOn information which will contain the backed quantity - backedAddOns.map { - holder[it.id()] = it - currentSelection[it.id()] = it.quantity() ?: 0 + // Take the backed AddOns, update with matching addOn ID with the quantity information + backedAddOns.map { backedAddOn -> + val aux = holder[backedAddOn.id()] + if (aux != null) { + val updated = aux.toBuilder().quantity(backedAddOn.quantity()).build() + holder[backedAddOn.id()] = updated + } + currentSelection[backedAddOn.id()] = backedAddOn.quantity() ?: 0 } return holder.values.toList() @@ -278,7 +281,7 @@ class AddOnsViewModel(val environment: Environment, bundle: Bundle? = null) : Vi val selectedAddOns = mutableListOf() addOns.forEach { val amount = currentSelection[it.id()] - if (amount != null) { + if (amount != null && amount > 0) { selectedAddOns.add(it.toBuilder().quantity(amount).build()) } } diff --git a/app/src/main/java/com/kickstarter/viewmodels/projectpage/CrowdfundCheckoutViewModel.kt b/app/src/main/java/com/kickstarter/viewmodels/projectpage/CrowdfundCheckoutViewModel.kt index a7cc56f98b..e1dab951e5 100644 --- a/app/src/main/java/com/kickstarter/viewmodels/projectpage/CrowdfundCheckoutViewModel.kt +++ b/app/src/main/java/com/kickstarter/viewmodels/projectpage/CrowdfundCheckoutViewModel.kt @@ -146,7 +146,6 @@ class CrowdfundCheckoutViewModel(val environment: Environment, bundle: Bundle? = fun provideBundle(arguments: Bundle?) { val pData = arguments?.getParcelable(ArgumentsKey.PLEDGE_PLEDGE_DATA) as PledgeData? pledgeReason = arguments?.getSerializable(ArgumentsKey.PLEDGE_PLEDGE_REASON) as PledgeReason? - val flowContext = pledgeReason?.let { PledgeFlowContext.forPledgeReason(it) } if (pData != null) { pledgeData = pData @@ -158,12 +157,11 @@ class CrowdfundCheckoutViewModel(val environment: Environment, bundle: Bundle? = sharedPreferences ) - when (flowContext) { - PledgeFlowContext.NEW_PLEDGE, - PledgeFlowContext.CHANGE_REWARD -> getPledgeInfoFrom(pData) - PledgeFlowContext.MANAGE_REWARD, - PledgeFlowContext.FIX_ERRORED_PLEDGE - -> { + when (pledgeReason) { + PledgeReason.PLEDGE, + PledgeReason.UPDATE_REWARD -> getPledgeInfoFrom(pData) + PledgeReason.UPDATE_PAYMENT, + PledgeReason.FIX_PLEDGE -> { backing?.let { getPledgeInfoFrom(it) } } else -> { diff --git a/app/src/main/java/com/kickstarter/viewmodels/projectpage/ProjectPageViewModel.kt b/app/src/main/java/com/kickstarter/viewmodels/projectpage/ProjectPageViewModel.kt index 0c58cc05b4..951d068616 100644 --- a/app/src/main/java/com/kickstarter/viewmodels/projectpage/ProjectPageViewModel.kt +++ b/app/src/main/java/com/kickstarter/viewmodels/projectpage/ProjectPageViewModel.kt @@ -1028,7 +1028,6 @@ interface ProjectPageViewModel { .addToDisposable(disposables) this.updatePledgeData - .distinctUntilChanged() .subscribe { this.showUpdatePledge.onNext(it) }.addToDisposable(disposables) diff --git a/app/src/main/java/com/kickstarter/viewmodels/projectpage/RewardsSelectionViewModel.kt b/app/src/main/java/com/kickstarter/viewmodels/projectpage/RewardsSelectionViewModel.kt index e1747307c0..661a578f70 100644 --- a/app/src/main/java/com/kickstarter/viewmodels/projectpage/RewardsSelectionViewModel.kt +++ b/app/src/main/java/com/kickstarter/viewmodels/projectpage/RewardsSelectionViewModel.kt @@ -25,7 +25,10 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import kotlinx.coroutines.rx2.asFlow @@ -33,13 +36,14 @@ import kotlinx.coroutines.rx2.asFlow data class RewardSelectionUIState( val selectedReward: Reward = Reward.builder().build(), val initialRewardIndex: Int = 0, - val project: ProjectData = ProjectData.builder().build(), + val project: ProjectData = ProjectData.builder().build() ) class RewardsSelectionViewModel(private val environment: Environment, private var shippingRulesUseCase: GetShippingRulesUseCase? = null) : ViewModel() { private val analytics = requireNotNull(environment.analytics()) private val apolloClient = requireNotNull(environment.apolloClientV2()) + private val currentConfig = requireNotNull(environment.currentConfigV2()?.observable()) private lateinit var currentProjectData: ProjectData private var pReason: PledgeReason? = null @@ -83,26 +87,30 @@ class RewardsSelectionViewModel(private val environment: Environment, private va indexOfBackedReward = indexOfBackedReward(project = projectData.project()) pReason = when { previousUserBacking == null && projectData.project().isInPostCampaignPledgingPhase() == true -> PledgeReason.LATE_PLEDGE - previousUserBacking != null -> PledgeReason.UPDATE_PLEDGE + previousUserBacking != null -> PledgeReason.UPDATE_REWARD previousUserBacking == null && projectData.project().isInPostCampaignPledgingPhase() == false -> PledgeReason.PLEDGE else -> PledgeReason.PLEDGE } - + val project = projectData.project() viewModelScope.launch { emitCurrentState() - environment.currentConfigV2()?.observable()?.asFlow()?.collectLatest { - if (shippingRulesUseCase == null) { - shippingRulesUseCase = GetShippingRulesUseCase( - projectData.project(), - it, - viewModelScope, - Dispatchers.IO - ) + apolloClient.getRewardsFromProject(project.slug() ?: "") + .asFlow() + .combine(currentConfig.asFlow()) { rewardsList, config -> + if (shippingRulesUseCase == null) { + shippingRulesUseCase = GetShippingRulesUseCase( + project = projectData.project(), + config = config, + projectRewards = rewardsList, + viewModelScope, + Dispatchers.IO + ) + } + shippingRulesUseCase?.invoke() + emitShippingUIState() } - shippingRulesUseCase?.invoke() - - emitShippingUIState() - } + .catch { } + .collect() } } @@ -209,7 +217,7 @@ class RewardsSelectionViewModel(private val environment: Environment, private va fun shouldShowAlert(): Boolean { val prevRw = previousUserBacking?.reward() prevRw?.let { - if (pReason == PledgeReason.UPDATE_PLEDGE) { + if (pReason == PledgeReason.UPDATE_REWARD) { return !previousUserBacking?.addOns().isNullOrEmpty() && prevRw.id() != newUserReward.id() } } diff --git a/app/src/main/java/com/kickstarter/viewmodels/usecases/GetShippingRulesUseCase.kt b/app/src/main/java/com/kickstarter/viewmodels/usecases/GetShippingRulesUseCase.kt index 2aefc60b07..f0b4f88b32 100644 --- a/app/src/main/java/com/kickstarter/viewmodels/usecases/GetShippingRulesUseCase.kt +++ b/app/src/main/java/com/kickstarter/viewmodels/usecases/GetShippingRulesUseCase.kt @@ -15,7 +15,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch data class ShippingRulesState( @@ -41,6 +40,7 @@ data class ShippingRulesState( class GetShippingRulesUseCase( private val project: Project, private val config: Config?, + private val projectRewards: List = emptyList(), private val scope: CoroutineScope, private val dispatcher: CoroutineDispatcher = Dispatchers.IO ) { @@ -49,28 +49,27 @@ class GetShippingRulesUseCase( private var defaultShippingRule = ShippingRule.builder().build() private var rewardsByShippingType: List private val allAvailableRulesForProject = mutableMapOf() - private val projectRewards = project.rewards()?.filter { RewardUtils.isNoReward(it) || it.isAvailable() } ?: listOf() init { // To avoid duplicates insert reward.id as key - val rewardsToQuery = mutableMapOf() + val rewardsToExtractLocation = mutableMapOf() // Get first reward with unrestricted shipping preference, when quering `getShippingRules` will return ALL available locations, no need to query more rewards locations - project.rewards()?.filter { RewardUtils.shipsWorldwide(reward = it) }?.firstOrNull()?.let { - rewardsToQuery.put(it.id(), it) + projectRewards.filter { RewardUtils.shipsWorldwide(reward = it) }?.firstOrNull()?.let { + rewardsToExtractLocation.put(it.id(), it) } // In case there is no unrestricted preference need to get restricted and local rewards, to query their specific locations - if (rewardsToQuery.isEmpty()) { - project.rewards()?.filter { + if (rewardsToExtractLocation.isEmpty()) { + projectRewards.filter { RewardUtils.shipsToRestrictedLocations(reward = it) - }?.forEach { - rewardsToQuery[it.id()] = it + }.forEach { + rewardsToExtractLocation[it.id()] = it } } - this.rewardsByShippingType = rewardsToQuery.values.toList() + this.rewardsByShippingType = rewardsToExtractLocation.values.toList() } // - Do not expose mutable states @@ -171,7 +170,7 @@ class GetShippingRulesUseCase( val isIsValidRule = allAvailableShippingRules[locationId] rewards.map { rw -> - if (RewardUtils.shipsWorldwide(rw)) { + if (RewardUtils.shipsWorldwide(rw) && rw.isAvailable()) { filteredRewards.add(rw) } @@ -179,16 +178,16 @@ class GetShippingRulesUseCase( filteredRewards.add(rw) } - if (RewardUtils.isLocalPickup(rw)) { + if (RewardUtils.isLocalPickup(rw) && rw.isAvailable()) { filteredRewards.add(rw) } - if (RewardUtils.isDigital(rw)) { + if (RewardUtils.isDigital(rw) && rw.isAvailable()) { filteredRewards.add(rw) } // - If shipping is restricted, make sure the reward is able to ship to selected rule - if (RewardUtils.shipsToRestrictedLocations(rw)) { + if (RewardUtils.shipsToRestrictedLocations(rw) && rw.isAvailable()) { if (isIsValidRule != null) { rw.shippingRules()?.map { if (it.location()?.id() == locationId) { @@ -215,7 +214,7 @@ class GetShippingRulesUseCase( val backing = project.backing() val locationId = project.backing()?.locationId() ?: 0L this.id(locationId) - val reward = backing?.reward()?.let { + backing?.reward()?.let { if (RewardUtils.shipsToRestrictedLocations(it)) { val rule = backing?.reward()?.shippingRules() ?.first { it.location()?.id() == locationId } diff --git a/app/src/test/java/com/kickstarter/services/GraphQLTransformersTest.kt b/app/src/test/java/com/kickstarter/services/GraphQLTransformersTest.kt index 52208cb093..e8e4fc23ff 100644 --- a/app/src/test/java/com/kickstarter/services/GraphQLTransformersTest.kt +++ b/app/src/test/java/com/kickstarter/services/GraphQLTransformersTest.kt @@ -15,7 +15,6 @@ import fragment.AiDisclosure import fragment.Amount import fragment.EnvironmentalCommitment import fragment.Faq -import fragment.FullProject import fragment.Reward import fragment.Reward.AllowedAddons import fragment.User @@ -187,7 +186,7 @@ class GraphQLTransformersTest : KSRobolectricTestCase() { @Test fun `test simpleShippingRuleTransformer provides appropriate shippingRule`() { - val canadaSimpleSR = FullProject.SimpleShippingRulesExpanded( + val canadaSimpleSR = FetchProjectRewardsQuery.SimpleShippingRulesExpanded( "SimpleShippingRule", "17.34562379823645234875620384756203847234", "CA", @@ -197,7 +196,7 @@ class GraphQLTransformersTest : KSRobolectricTestCase() { "Canada" ) - val australiaSR = FullProject.SimpleShippingRulesExpanded( + val australiaSR = FetchProjectRewardsQuery.SimpleShippingRulesExpanded( "SimpleShippingRule", "0", "AU", @@ -207,7 +206,7 @@ class GraphQLTransformersTest : KSRobolectricTestCase() { "Australia" ) - val forbiddenValues = FullProject.SimpleShippingRulesExpanded( + val forbiddenValues = FetchProjectRewardsQuery.SimpleShippingRulesExpanded( "SimpleShippingRule", "Pikachusito", "AU", @@ -287,7 +286,7 @@ class GraphQLTransformersTest : KSRobolectricTestCase() { @Test fun `test rewardTransformer returns appropriate shippingRules field when querying for simpleShippingRulesExpanded`() { - val canadaSimpleSR = FullProject.SimpleShippingRulesExpanded( + val canadaSimpleSR = FetchProjectRewardsQuery.SimpleShippingRulesExpanded( "SimpleShippingRule", "17.34562379823645234875620384756203847234", "CA", @@ -297,7 +296,7 @@ class GraphQLTransformersTest : KSRobolectricTestCase() { "Canada" ) - val australiaSR = FullProject.SimpleShippingRulesExpanded( + val australiaSR = FetchProjectRewardsQuery.SimpleShippingRulesExpanded( "SimpleShippingRule", "0", "AU", diff --git a/app/src/test/java/com/kickstarter/viewmodels/RewardsSelectionViewModelTest.kt b/app/src/test/java/com/kickstarter/viewmodels/RewardsSelectionViewModelTest.kt index b1e8557518..e2eed88c83 100644 --- a/app/src/test/java/com/kickstarter/viewmodels/RewardsSelectionViewModelTest.kt +++ b/app/src/test/java/com/kickstarter/viewmodels/RewardsSelectionViewModelTest.kt @@ -351,7 +351,7 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { val shippingUiState = mutableListOf() backgroundScope.launch(dispatcher) { - val useCase = GetShippingRulesUseCase(testProject, config, this, dispatcher) + val useCase = GetShippingRulesUseCase(testProject, config, testRewards, this, dispatcher) createViewModel(env, useCase) viewModel.provideProjectData(testProjectData) @@ -449,8 +449,9 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { .locationName(testShippingRulesList.first().location()?.displayableName()) .build() + val rwList = listOf(rw1, rw2, rw3) val project = ProjectFactory.project().toBuilder() - .rewards(listOf(rw1, rw2, rw3)) + .rewards(rwList) .backing(backing) .isBacking(true) .build() @@ -470,7 +471,7 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { val dispatcher = UnconfinedTestDispatcher(testScheduler) val shippingUiState = mutableListOf() backgroundScope.launch(dispatcher) { - val useCase = GetShippingRulesUseCase(project, config, this, dispatcher) + val useCase = GetShippingRulesUseCase(project, config, rwList, this, dispatcher) createViewModel(env, useCase) viewModel.provideProjectData(projectData) viewModel.shippingUIState.toList(shippingUiState) @@ -495,7 +496,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { .shippingRules(testShippingRulesList.shippingRules()) .build() val user = UserFactory.user() - val project = ProjectFactory.project().toBuilder().rewards(listOf(rw, rw, rw)).build() + val rwList = listOf(rw, rw, rw) + val project = ProjectFactory.project().toBuilder().rewards(rwList).build() val projectData = ProjectDataFactory.project(project, null, null) val config = ConfigFactory.configForCA() @@ -511,7 +513,7 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { val dispatcher = UnconfinedTestDispatcher(testScheduler) val shippingUiState = mutableListOf() backgroundScope.launch(dispatcher) { - val useCase = GetShippingRulesUseCase(project, config, this, dispatcher) + val useCase = GetShippingRulesUseCase(project, config, rwList, this, dispatcher) createViewModel(env, useCase) viewModel.provideProjectData(projectData) viewModel.shippingUIState.toList(shippingUiState) @@ -525,7 +527,7 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { } @Test - fun `When user is updating pledge, if selecting a different reward and had addOns backed, show alert`() = runTest { + fun `When user is updating reward selection, if selecting a different reward and had addOns backed, show alert`() = runTest { val reward = RewardFactory.digitalReward() val addOns = listOf(RewardFactory.reward(), RewardFactory.addOnMultiple()) val backing = Backing.builder() @@ -553,7 +555,7 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { advanceUntilIdle() // wait until all state emissions completed assertEquals(viewModel.shouldShowAlert(), true) - assertEquals(viewModel.getPledgeData()?.second, PledgeReason.UPDATE_PLEDGE) + assertEquals(viewModel.getPledgeData()?.second, PledgeReason.UPDATE_REWARD) } @Test diff --git a/app/src/test/java/com/kickstarter/viewmodels/usecases/GetShippingRulesUseCaseTest.kt b/app/src/test/java/com/kickstarter/viewmodels/usecases/GetShippingRulesUseCaseTest.kt index fb0a3b14f3..e3215d2604 100644 --- a/app/src/test/java/com/kickstarter/viewmodels/usecases/GetShippingRulesUseCaseTest.kt +++ b/app/src/test/java/com/kickstarter/viewmodels/usecases/GetShippingRulesUseCaseTest.kt @@ -33,7 +33,7 @@ class GetShippingRulesUseCaseTest : KSRobolectricTestCase() { val dispatcher = UnconfinedTestDispatcher(testScheduler) val scope = backgroundScope - val useCase = GetShippingRulesUseCase(project, config, scope, dispatcher) + val useCase = GetShippingRulesUseCase(project, config, project.rewards() ?: emptyList(), scope, dispatcher) val state = mutableListOf() scope.launch(dispatcher) { @@ -66,9 +66,10 @@ class GetShippingRulesUseCaseTest : KSRobolectricTestCase() { .shippingRules(listOf(shippingRule1, shippingRule2, shippingRule3)) .build() + val rwList = listOf(reward, reward2) val project = ProjectFactory.project() .toBuilder() - .rewards(listOf(reward, reward2)) + .rewards(rwList) .state(Project.STATE_SUCCESSFUL) .isInPostCampaignPledgingPhase(true) .postCampaignPledgingEnabled(true) @@ -77,7 +78,7 @@ class GetShippingRulesUseCaseTest : KSRobolectricTestCase() { val dispatcher = UnconfinedTestDispatcher(testScheduler) val scope = backgroundScope - val useCase = GetShippingRulesUseCase(project, config, scope, dispatcher) + val useCase = GetShippingRulesUseCase(project, config, rwList, scope, dispatcher) val state = mutableListOf() scope.launch(dispatcher) {