Skip to content

Instantly share code, notes, and snippets.

View vikasmain's full-sized avatar
🎯
Focusing

Vikas Bajpayee vikasmain

🎯
Focusing
View GitHub Profile
@DisplayName("test on click listener for movie item")
@Test
fun testOnclickListener() {
MovieStateFlow.onClickStateFlow.value = "Spider man"
moviePresenter.observeOnClickStateFlow()
verify(view).openSingleItemView("Spider man")
}
@DisplayName("when movie list api failed then show error view")
@Test
fun testApiFailed() {
moviePresenter.callMoviesApi()
verify(view).showLoadingView()
verify(view).showErrorView()
verify(view).hideLoadingView()
}
import com.app.coroutinesmvp.model.MovieModel
import com.app.coroutinesmvp.presenter.MoviePresenter
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.plus
internal fun callMoviesApi() {
movieModel.callMoviesApi()
.onStart {
movieView.showLoadingView()
}
.catch {
movieView.showErrorView()
}
.onEach {
movieView.showMoviesList(it)
import com.app.coroutinesmvp.data.MovieListResponse
import retrofit2.http.GET
import retrofit2.http.Query
interface MovieApi {
@GET("/3/discover/movie")
suspend fun getMoviesList(@Query("api_key") apiKey: String): MovieListResponse
}
import com.app.coroutinesmvp.api.MovieApi
import com.app.coroutinesmvp.model.MovieModel
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.runBlocking
@MovieActivityScope
class MovieModel @Inject constructor(
private val movieApi: MovieApi
) {
fun callMoviesApi(): Flow<MovieListResponse> {
return flow {
val carbonOffsetResponse = movieApi.getMoviesList("a427cfb730b4a73e67c646d8b44dd216")
emit(carbonOffsetResponse)
}.flowOn(Dispatchers.IO)
}
@MovieActivityScope
class MoviePresenter @Inject constructor(
private val movieView: MovieContract.MovieView,
private val scope: CoroutineScope,
private val movieModel: MovieModel
) {
fun onAttach() {
callMoviesApi()
observeOnClickStateFlow()
@Subcomponent(modules = [MovieActivityModule::class])
@MovieActivityScope
interface MovieComponent {
fun inject(movieActivity: MovieActivity)
@Subcomponent.Builder
interface Builder {
@BindsInstance
fun activity(appCompatActivity: AppCompatActivity): Builder
@Subcomponent(modules = [MovieActivityModule::class])
@MovieActivityScope
interface MovieComponent {
fun inject(movieActivity: MovieActivity)
@Subcomponent.Builder
interface Builder {
@BindsInstance
fun activity(appCompatActivity: AppCompatActivity): Builder