1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +00:00

Add state username change ProfileFragmentViewModel test

This commit is contained in:
Ammar Githam 2021-06-24 00:21:47 +09:00
parent 976c9a86b6
commit 1605f9515d

View File

@ -14,6 +14,7 @@ import awais.instagrabber.models.Resource
import awais.instagrabber.repositories.responses.User
import awais.instagrabber.webservices.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.json.JSONException
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
@ -36,6 +37,12 @@ internal class ProfileFragmentViewModelTest {
fullName = "Test user"
)
private val testPublicUser1 = User(
pk = 101,
username = "test1",
fullName = "Test1 user1"
)
@ExperimentalCoroutinesApi
@Test
fun `no state username and null current user`() {
@ -181,4 +188,47 @@ internal class ProfileFragmentViewModelTest {
}
assertEquals(testPublicUser, profile.data)
}
@ExperimentalCoroutinesApi
@Test
fun `state username changes`() {
val state = SavedStateHandle(
mutableMapOf<String, Any?>(
"username" to testPublicUser.username
)
)
val graphQLRepository = object : GraphQLRepository(GraphQLServiceAdapter()) {
override suspend fun fetchUser(username: String): User {
return when(username) {
testPublicUser.username -> testPublicUser
testPublicUser1.username -> testPublicUser1
else -> throw JSONException("")
}
}
}
val viewModel = ProfileFragmentViewModel(
state,
UserRepository(UserServiceAdapter()),
FriendshipRepository(FriendshipServiceAdapter()),
StoriesRepository(StoriesServiceAdapter()),
MediaRepository(MediaServiceAdapter()),
graphQLRepository,
AccountRepository(AccountDataSource(AccountDaoAdapter())),
FavoriteRepository(FavoriteDataSource(FavoriteDaoAdapter())),
coroutineScope.dispatcher,
)
viewModel.setCurrentUser(Resource.success(null))
assertEquals(false, viewModel.isLoggedIn.getOrAwaitValue())
var profile = viewModel.profile.getOrAwaitValue()
while (profile.status == Resource.Status.LOADING) {
profile = viewModel.profile.getOrAwaitValue()
}
assertEquals(testPublicUser, profile.data)
state.set("username", testPublicUser1.username)
profile = viewModel.profile.getOrAwaitValue()
while (profile.status == Resource.Status.LOADING) {
profile = viewModel.profile.getOrAwaitValue()
}
assertEquals(testPublicUser1, profile.data)
}
}