feat(content): 전체보기 ViewModel을 추가한다
This commit is contained in:
@@ -0,0 +1,293 @@
|
||||
package kr.co.vividnext.sodalive.v2.main.content.overview
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
import io.reactivex.rxjava3.core.Scheduler
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import io.reactivex.rxjava3.plugins.RxJavaPlugins
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import io.reactivex.rxjava3.subjects.SingleSubject
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.v2.main.content.overview.data.ContentOverviewItemResponse
|
||||
import kr.co.vividnext.sodalive.v2.main.content.overview.data.ContentOverviewPageResponse
|
||||
import kr.co.vividnext.sodalive.v2.main.content.overview.data.ContentOverviewRepository
|
||||
import kr.co.vividnext.sodalive.v2.main.content.overview.data.ContentOverviewType
|
||||
import kr.co.vividnext.sodalive.v2.main.content.overview.model.ContentOverviewUiState
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.never
|
||||
import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.whenever
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class ContentOverviewViewModelTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var repository: ContentOverviewRepository
|
||||
private lateinit var viewModel: ContentOverviewViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
repository = org.mockito.kotlin.mock()
|
||||
viewModel = ContentOverviewViewModel(repository)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `초기 로드는 전달받은 type으로 첫 페이지를 요청하고 Content를 emit한다`() {
|
||||
stubGetContents(
|
||||
type = ContentOverviewType.FIRST_AUDIO_CONTENT,
|
||||
response = Single.just(
|
||||
ApiResponse(
|
||||
true,
|
||||
pageResponse(
|
||||
type = ContentOverviewType.FIRST_AUDIO_CONTENT,
|
||||
items = listOf(item(11L))
|
||||
),
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
viewModel.loadFirstPage(ContentOverviewType.FIRST_AUDIO_CONTENT)
|
||||
|
||||
val state = viewModel.overviewStateLiveData.requireValue() as ContentOverviewUiState.Content
|
||||
assertEquals(ContentOverviewType.FIRST_AUDIO_CONTENT, state.type)
|
||||
assertEquals(0, state.page)
|
||||
assertEquals(20, state.size)
|
||||
assertEquals(listOf(11L), state.items.map { it.contentId })
|
||||
verifyGetContents(type = ContentOverviewType.FIRST_AUDIO_CONTENT, page = 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `첫 페이지 성공이지만 items가 비어 있으면 Empty를 emit한다`() {
|
||||
stubGetContents(
|
||||
response = Single.just(
|
||||
ApiResponse(
|
||||
true,
|
||||
pageResponse(items = emptyList()),
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
viewModel.loadFirstPage(ContentOverviewType.NEW_AND_HOT_AUDIO)
|
||||
|
||||
val state = viewModel.overviewStateLiveData.requireValue() as ContentOverviewUiState.Empty
|
||||
assertEquals(ContentOverviewType.NEW_AND_HOT_AUDIO, state.type)
|
||||
assertEquals(0, state.totalCount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `첫 페이지 실패는 Error와 toast를 emit한다`() {
|
||||
stubGetContents(
|
||||
response = Single.just(ApiResponse(false, null, "failed"))
|
||||
)
|
||||
|
||||
viewModel.loadFirstPage(ContentOverviewType.NEW_AND_HOT_AUDIO)
|
||||
|
||||
val state = viewModel.overviewStateLiveData.requireValue() as ContentOverviewUiState.Error
|
||||
assertEquals("failed", state.message)
|
||||
assertEquals(false, viewModel.isLoading.requireValue())
|
||||
assertEquals(
|
||||
kr.co.vividnext.sodalive.R.string.common_error_unknown,
|
||||
viewModel.toastLiveData.requireValue()?.resId
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hasNext true이면 loadMore는 다음 page를 요청하고 append한다`() {
|
||||
stubGetContents(
|
||||
response = Single.just(
|
||||
ApiResponse(
|
||||
true,
|
||||
pageResponse(items = listOf(item(1L)), hasNext = true),
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
stubGetContents(
|
||||
page = 1,
|
||||
response = Single.just(
|
||||
ApiResponse(
|
||||
true,
|
||||
pageResponse(page = 1, items = listOf(item(2L)), hasNext = false),
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
viewModel.loadFirstPage(ContentOverviewType.NEW_AND_HOT_AUDIO)
|
||||
viewModel.loadMore()
|
||||
|
||||
val state = viewModel.overviewStateLiveData.requireValue() as ContentOverviewUiState.Content
|
||||
assertEquals(listOf(1L, 2L), state.items.map { it.contentId })
|
||||
assertEquals(1, state.page)
|
||||
assertFalse(state.hasNext)
|
||||
verifyGetContents(page = 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hasNext false이면 loadMore는 추가 요청하지 않는다`() {
|
||||
stubGetContents(
|
||||
response = Single.just(
|
||||
ApiResponse(
|
||||
true,
|
||||
pageResponse(items = listOf(item(1L)), hasNext = false),
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
viewModel.loadFirstPage(ContentOverviewType.NEW_AND_HOT_AUDIO)
|
||||
viewModel.loadMore()
|
||||
|
||||
verify(
|
||||
repository,
|
||||
never()
|
||||
).getContents(
|
||||
"Bearer test-token",
|
||||
1,
|
||||
ContentOverviewViewModel.DEFAULT_PAGE_SIZE,
|
||||
ContentOverviewType.NEW_AND_HOT_AUDIO
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `추가 페이지 실패 시 기존 items를 유지하고 pagination error를 emit한다`() {
|
||||
stubGetContents(
|
||||
response = Single.just(
|
||||
ApiResponse(
|
||||
true,
|
||||
pageResponse(items = listOf(item(1L)), hasNext = true),
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
stubGetContents(
|
||||
page = 1,
|
||||
response = Single.just(ApiResponse(false, null, "page failed"))
|
||||
)
|
||||
|
||||
viewModel.loadFirstPage(ContentOverviewType.NEW_AND_HOT_AUDIO)
|
||||
viewModel.loadMore()
|
||||
|
||||
val state = viewModel.overviewStateLiveData.requireValue() as ContentOverviewUiState.Content
|
||||
assertEquals(listOf(1L), state.items.map { it.contentId })
|
||||
assertFalse(state.isLoadingMore)
|
||||
assertEquals("page failed", state.paginationErrorMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `새 type 요청 이후 이전 stale response는 현재 상태를 덮어쓰지 않는다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<ContentOverviewPageResponse>>()
|
||||
stubGetContents(type = ContentOverviewType.NEW_AND_HOT_AUDIO, response = pending)
|
||||
stubGetContents(
|
||||
type = ContentOverviewType.FIRST_AUDIO_CONTENT,
|
||||
response = Single.just(
|
||||
ApiResponse(
|
||||
true,
|
||||
pageResponse(
|
||||
type = ContentOverviewType.FIRST_AUDIO_CONTENT,
|
||||
items = listOf(item(9L))
|
||||
),
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
viewModel.loadFirstPage(ContentOverviewType.NEW_AND_HOT_AUDIO)
|
||||
viewModel.loadFirstPage(ContentOverviewType.FIRST_AUDIO_CONTENT)
|
||||
pending.onSuccess(
|
||||
ApiResponse(
|
||||
true,
|
||||
pageResponse(items = listOf(item(1L))),
|
||||
null
|
||||
)
|
||||
)
|
||||
|
||||
val state = viewModel.overviewStateLiveData.requireValue() as ContentOverviewUiState.Content
|
||||
assertEquals(ContentOverviewType.FIRST_AUDIO_CONTENT, state.type)
|
||||
assertEquals(listOf(9L), state.items.map { it.contentId })
|
||||
}
|
||||
|
||||
private fun stubGetContents(
|
||||
type: ContentOverviewType = ContentOverviewType.NEW_AND_HOT_AUDIO,
|
||||
page: Int = 0,
|
||||
size: Int = ContentOverviewViewModel.DEFAULT_PAGE_SIZE,
|
||||
response: Single<ApiResponse<ContentOverviewPageResponse>>
|
||||
) {
|
||||
whenever(repository.getContents("Bearer test-token", page, size, type)).thenReturn(response)
|
||||
}
|
||||
|
||||
private fun verifyGetContents(
|
||||
type: ContentOverviewType = ContentOverviewType.NEW_AND_HOT_AUDIO,
|
||||
page: Int = 0,
|
||||
size: Int = ContentOverviewViewModel.DEFAULT_PAGE_SIZE
|
||||
) {
|
||||
verify(repository).getContents("Bearer test-token", page, size, type)
|
||||
}
|
||||
|
||||
private fun pageResponse(
|
||||
type: ContentOverviewType = ContentOverviewType.NEW_AND_HOT_AUDIO,
|
||||
items: List<ContentOverviewItemResponse> = listOf(item()),
|
||||
page: Int = 0,
|
||||
size: Int = 20,
|
||||
hasNext: Boolean = false
|
||||
) = ContentOverviewPageResponse(
|
||||
type = type,
|
||||
items = items,
|
||||
page = page,
|
||||
size = size,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun item(contentId: Long = 1L) = ContentOverviewItemResponse(
|
||||
contentId = contentId,
|
||||
title = "content $contentId",
|
||||
coverImage = "https://example.com/$contentId.png",
|
||||
price = 100,
|
||||
isAdult = false,
|
||||
isPointAvailable = false,
|
||||
isFirstContent = false,
|
||||
isOriginalSeries = false,
|
||||
creatorNickname = "creator"
|
||||
)
|
||||
|
||||
private fun setImmediateRxSchedulers() {
|
||||
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
|
||||
RxJavaPlugins.setIoSchedulerHandler(trampoline)
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
}
|
||||
|
||||
private fun <T> LiveData<T>.requireValue(): T? {
|
||||
var value: T? = null
|
||||
val observer = Observer<T> { value = it }
|
||||
observeForever(observer)
|
||||
removeObserver(observer)
|
||||
return value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user