Skip to content

Commit

Permalink
MBL-1306: Added tests (#2029)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkariang authored May 7, 2024
1 parent 7868ad4 commit 7f7cbf7
Showing 1 changed file with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ package com.kickstarter.viewmodels

import com.kickstarter.KSRobolectricTestCase
import com.kickstarter.libs.Environment
import com.kickstarter.libs.MockCurrentUserV2
import com.kickstarter.libs.utils.EventName
import com.kickstarter.mock.factories.ProjectDataFactory
import com.kickstarter.mock.factories.ProjectFactory
import com.kickstarter.mock.factories.RewardFactory
import com.kickstarter.mock.factories.ShippingRuleFactory
import com.kickstarter.mock.factories.StoredCardFactory
import com.kickstarter.mock.factories.UserFactory
import com.kickstarter.mock.services.MockApolloClientV2
import com.kickstarter.models.StoredCard
import com.kickstarter.models.UserPrivacy
import com.kickstarter.viewmodels.projectpage.LatePledgeCheckoutUIState
import com.kickstarter.viewmodels.projectpage.LatePledgeCheckoutViewModel
import io.reactivex.Observable
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Test

class LatePledgeCheckoutViewModelTest : KSRobolectricTestCase() {
Expand Down Expand Up @@ -70,4 +82,71 @@ class LatePledgeCheckoutViewModelTest : KSRobolectricTestCase() {

this.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName)
}

@Test
fun `Test VM init state when user and stored cards requests succeed will generate state with saved cards and user email`() = runTest {
val discover = StoredCardFactory.discoverCard()
val visa = StoredCardFactory.visa()
val cardsList = listOf(visa, discover)

val currentUser = MockCurrentUserV2(UserFactory.user())
setUpEnvironment(
environment()
.toBuilder()
.currentUserV2(currentUser) // - mock the user
.apolloClientV2(object : MockApolloClientV2() {
override fun getStoredCards(): Observable<List<StoredCard>> { // - mock the stored cards
return Observable.just(cardsList)
}

override fun userPrivacy(): Observable<UserPrivacy> { // - mock the user email and name
return Observable.just(
UserPrivacy("Hola holita", "[email protected]", true, true, true, true, "MXN")
)
}
}).build()
)

val state = mutableListOf<LatePledgeCheckoutUIState>()
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
viewModel.latePledgeCheckoutUIState.toList(state)
}

assertEquals(state.size, 2)
assertEquals(state.last().userEmail, "[email protected]")
assertEquals(state.last().storeCards, cardsList)
assertEquals(state.last().storeCards.first(), cardsList.first())
assertEquals(state.last().storeCards.last(), cardsList.last())
assertEquals(state.last().isLoading, false)
}

@Test
fun `Test VM error init state when user or stored cards requests fail will generate state without saved cards or user email`() = runTest {

val currentUser = MockCurrentUserV2(UserFactory.user())
setUpEnvironment(
environment()
.toBuilder()
.currentUserV2(currentUser) // - mock the user
.apolloClientV2(object : MockApolloClientV2() {
override fun getStoredCards(): Observable<List<StoredCard>> { // - mock the stored cards
return Observable.error(Throwable("Something went wrong"))
}

override fun userPrivacy(): Observable<UserPrivacy> { // - mock the user email and name
return Observable.error(Throwable("Something went wrong"))
}
}).build()
)

val state = mutableListOf<LatePledgeCheckoutUIState>()

backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
viewModel.latePledgeCheckoutUIState.toList(state)
}

assertEquals(state.size, 2)
assertEquals(state.last().userEmail, "")
assertEquals(state.last().storeCards, emptyList<StoredCard>())
}
}

0 comments on commit 7f7cbf7

Please sign in to comment.