Created
May 24, 2021 06:43
-
-
Save alfianyusufabdullah/35c8e634ba706c6da9f64be65245ae6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(MockitoJUnitRunner::class) | |
class TvShowViewModelTest { | |
private lateinit var viewModel: TvShowViewModel | |
@get:Rule | |
var instantTaskExecutorRule = InstantTaskExecutorRule() | |
@Mock | |
private lateinit var catalogueRepository: CatalogueRepository | |
@Mock | |
private lateinit var observer: Observer<Resource<List<TvShowEntity>>> | |
@Mock | |
private lateinit var favoredObserver: Observer<PagingData<TvShowEntity>> | |
@Before | |
fun setUp() { | |
viewModel = TvShowViewModel(catalogueRepository) | |
} | |
@Test | |
fun getFavoredTvShows() { | |
val dummyTvShows = DataDummy.generateDummyTvShows() | |
val data = PagedTestDataSources.snapshot(dummyTvShows) | |
val tvShows = MutableLiveData<PagingData<TvShowEntity>>() | |
tvShows.value = data | |
`when`(catalogueRepository.getFavoredTvShows()).thenReturn(tvShows) | |
val tvShowEntities = viewModel.getFavoredTvShows().value | |
verify(catalogueRepository).getFavoredTvShows() | |
assertNotNull(tvShowEntities) | |
viewModel.getFavoredTvShows().observeForever(favoredObserver) | |
verify(favoredObserver).onChanged(data) | |
} | |
class PagedTestDataSources private constructor(private val items: List<TvShowEntity>) : | |
PagingSource<Int, LiveData<List<TvShowEntity>>>() { | |
companion object { | |
fun snapshot(items: List<TvShowEntity>): PagingData<TvShowEntity> { | |
return PagingData.from(items) | |
} | |
} | |
override fun getRefreshKey(state: PagingState<Int, LiveData<List<TvShowEntity>>>): Int { | |
return 0 | |
} | |
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, LiveData<List<TvShowEntity>>> { | |
return LoadResult.Page(emptyList(), 0 , 1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment