-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
|
@@ -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>()) | ||
} | ||
} |