Skip to content

Commit

Permalink
Merge pull request #15 from myofficework000/repo-viewmode-testing
Browse files Browse the repository at this point in the history
Added test case for repo view model
  • Loading branch information
myofficework000 authored Jul 9, 2024
2 parents 702d42e + d708a43 commit 897f4d1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ dependencies {
testImplementation(Dependencies.Test.kotlinxCoroutinesTest)
testImplementation(Dependencies.Test.koinTest)
testImplementation(Dependencies.Test.okHttp3MockWebServer)
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")

// Android Test
androidTestImplementation(Dependencies.AndroidTest.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.abhishek.pathak.kotlin.android.githubcompose.ui.feature.repos

import com.abhishek.pathak.kotlin.android.githubcompose.data.GithubRepository
import com.abhishek.pathak.kotlin.android.githubcompose.data.model.Repo
import com.abhishek.pathak.kotlin.android.githubcompose.data.model.User
import com.abhishek.pathak.kotlin.android.githubcompose.data.model.UserDetail
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.*

@OptIn(ExperimentalCoroutinesApi::class)
class ReposViewModelTest{
private val githubRepository = mockk<GithubRepository>()

@Before
fun setUp(){
Dispatchers.setMain(Dispatchers.Unconfined)
}
@Test
fun `when view model initialized then should emit initial view state first`() = runTest {
val expectedInitialViewState = ReposContract.State(
user = null,
reposList = emptyList(),
isUserLoading = true,
isReposLoading = true,
isError = false
)
val viewmodel = ReposViewModel("testUserId",githubRepository)
assertEquals(expectedInitialViewState,viewmodel.viewState.value)
}

@Test
fun `when fetchInitialData called then should emit user and repos state`() = runTest {
val userId = "testUserId"
val user = UserDetail(avatarUrl = "avatar_url", htmlUrl = "html_url")
val repos = listOf(Repo(id = 1, name = "Repo 1", description = "Description 1"))
val expectedViewState = ReposContract.State(
user = user,
reposList = repos,
isUserLoading = false,
isReposLoading = false,
isError = false
)
coEvery { githubRepository.getUser(userId) } returns Result.success(user)
coEvery { githubRepository.getRepos(userId) } returns Result.success(repos)

val viewmodel = ReposViewModel(userId,githubRepository)

assertEquals(expectedViewState,viewmodel.viewState.value)
}

@Test
fun `test state after repos retrieval failure`() = runTest {
val userId = "testUserId"
val user = UserDetail(avatarUrl = "avatar_url", htmlUrl = "html_url")
val expectedViewState = ReposContract.State(
user = user,
reposList = emptyList(),
isUserLoading = false,
isReposLoading = false,
isError = true
)

coEvery { githubRepository.getUser(userId) } returns Result.success(user)
coEvery { githubRepository.getRepos(userId) } returns Result.failure(Exception("Repos retrieval failed"))

val viewmodel = ReposViewModel(userId,githubRepository)

assertEquals(expectedViewState,viewmodel.viewState.value)
}
}

0 comments on commit 897f4d1

Please sign in to comment.