Skip to content

Commit

Permalink
Implemented GithubReposVMTest
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamsinghmutualmobile committed Jan 25, 2022
1 parent 5d960c9 commit da7efe7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions ui-githubrepos/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ dependencies {
testImplementation(TestLib.ROBO_ELECTRIC)
testImplementation(TestLib.COROUTINES)
testImplementation(TestLib.MOCKK)
testImplementation(TestLib.TURBINE)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.mutualmobile.feat.githubrepos.ui.github.repolist

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.paging.PagingData
import app.cash.turbine.test
import com.mutualmobile.feat.githubrepos.ui.model.UIRepoMapper
import com.mutualmobile.praxis.data.repository.GithubRepoImpl
import com.mutualmobile.praxis.domain.model.DOMOwner
import com.mutualmobile.praxis.domain.model.DOMRepo
import com.mutualmobile.praxis.domain.usecases.GetGithubTrendingReposUseCase
import com.mutualmobile.praxis.navigator.asFlow
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule

@ExperimentalCoroutinesApi
class GithubReposVMTest {

// Important for your test's execution while testing LiveData
@get:Rule
var rule: TestRule = InstantTaskExecutorRule()

lateinit var githubReposVM: GithubReposVM

@MockK
lateinit var githubRepoImpl: GithubRepoImpl

lateinit var githubTrendingReposUseCase: GetGithubTrendingReposUseCase

@MockK
lateinit var uiRepoMapper: UIRepoMapper

@Before
fun setUp() {
MockKAnnotations.init(this, true)
Dispatchers.setMain(StandardTestDispatcher())
uiRepoMapper = UIRepoMapper()
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun `test that trendingGithubRepos() completes without exception`() = runTest {
launch {
val testRepo = flowOf(
PagingData.from(
listOf(
DOMRepo(
id = 0,
name = "Test",
fullName = "Test",
description = "Test",
url = "Test",
stars = 0,
forks = 0,
language = "Test",
watchers = 0,
owner = DOMOwner(
id = 0,
login = "Test",
avatarUrl = "Test"
),
createDate = "Test",
updateDate = "Test",
openIssues = 0
)
)
)
)

coEvery {
githubRepoImpl.getTrendingRepos("flutter")
} returns testRepo

githubTrendingReposUseCase = GetGithubTrendingReposUseCase(githubRepoImpl)

githubReposVM = GithubReposVM(
githubTrendingReposUseCase,
uiRepoMapper
)

githubReposVM.getGitHubTrendingRepos()

githubReposVM.reposFlowLiveData.asFlow().test {
assert(awaitItem() == testRepo)
}
}
}
}

0 comments on commit da7efe7

Please sign in to comment.