From 1605f9515d6a1ef83d2f6e6a3784e71e3aa8b4f6 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Thu, 24 Jun 2021 00:21:47 +0900 Subject: [PATCH] Add state username change ProfileFragmentViewModel test --- .../ProfileFragmentViewModelTest.kt | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt b/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt index b85b90e4..7b8dd320 100644 --- a/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt +++ b/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt @@ -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( + "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) + } } \ No newline at end of file