feat(creator): 커뮤니티 탭 상태 관리를 추가한다
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.community
|
||||
|
||||
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.creator.channel.community.data.CreatorChannelCommunityPostResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.times
|
||||
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 CreatorChannelCommunityPaginationTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var repository: CreatorChannelRepository
|
||||
private lateinit var viewModel: CreatorChannelCommunityViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
repository = org.mockito.kotlin.mock()
|
||||
viewModel = CreatorChannelCommunityViewModel(repository)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hasNext가 true이면 다음 페이지는 마지막 성공 응답의 page plus 1로 요청하고 append한다`() {
|
||||
stubGetCommunity(
|
||||
page = 0,
|
||||
response = Single.just(
|
||||
ApiResponse(true, communityResponse(page = 0, ids = listOf(1L), hasNext = true), null)
|
||||
)
|
||||
)
|
||||
stubGetCommunity(
|
||||
page = 1,
|
||||
response = Single.just(
|
||||
ApiResponse(true, communityResponse(page = 1, ids = listOf(2L), hasNext = false), null)
|
||||
)
|
||||
)
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
|
||||
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
assertEquals(1, state.page)
|
||||
assertEquals(listOf(1L, 2L), state.communityPosts.map { it.postId })
|
||||
assertFalse(state.hasNext)
|
||||
verifyGetCommunity(page = 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `다음 페이지 로딩 중 중복 load-more 요청은 막고 size 20을 유지한다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<CreatorChannelCommunityTabResponse>>()
|
||||
stubGetCommunity(
|
||||
page = 0,
|
||||
response = Single.just(
|
||||
ApiResponse(true, communityResponse(page = 0, ids = listOf(1L), hasNext = true), null)
|
||||
)
|
||||
)
|
||||
stubGetCommunity(page = 1, response = pending)
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
viewModel.loadMore()
|
||||
|
||||
val loadingState = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
assertTrue(loadingState.isLoadingMore)
|
||||
verifyGetCommunity(page = 1, times = 1)
|
||||
pending.onSuccess(
|
||||
ApiResponse(true, communityResponse(page = 1, ids = listOf(2L), hasNext = false), null)
|
||||
)
|
||||
val loadedState = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
assertEquals(CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE, loadedState.size)
|
||||
assertEquals(listOf(1L, 2L), loadedState.communityPosts.map { it.postId })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `다음 페이지 실패는 기존 목록을 유지하고 pagination error를 표시한다`() {
|
||||
stubGetCommunity(
|
||||
page = 0,
|
||||
response = Single.just(
|
||||
ApiResponse(true, communityResponse(page = 0, ids = listOf(1L), hasNext = true), null)
|
||||
)
|
||||
)
|
||||
stubGetCommunity(
|
||||
page = 1,
|
||||
response = Single.just(ApiResponse(false, null, "failed"))
|
||||
)
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
|
||||
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
assertEquals(listOf(1L), state.communityPosts.map { it.postId })
|
||||
assertFalse(state.isLoadingMore)
|
||||
assertEquals("failed", state.paginationErrorMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `pagination error message는 표시 후 clear되어 다시 표시되지 않는다`() {
|
||||
stubGetCommunity(
|
||||
page = 0,
|
||||
response = Single.just(
|
||||
ApiResponse(true, communityResponse(page = 0, ids = listOf(1L), hasNext = true), null)
|
||||
)
|
||||
)
|
||||
stubGetCommunity(
|
||||
page = 1,
|
||||
response = Single.just(ApiResponse(false, null, "failed"))
|
||||
)
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
viewModel.loadMore()
|
||||
|
||||
viewModel.consumePaginationErrorMessage()
|
||||
|
||||
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
assertEquals(null, state.paginationErrorMessage)
|
||||
assertEquals(listOf(1L), state.communityPosts.map { it.postId })
|
||||
}
|
||||
|
||||
private fun stubGetCommunity(
|
||||
page: Int,
|
||||
response: Single<ApiResponse<CreatorChannelCommunityTabResponse>>
|
||||
) {
|
||||
whenever(
|
||||
repository.getCommunity(
|
||||
100L,
|
||||
page,
|
||||
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
|
||||
"Bearer test-token"
|
||||
)
|
||||
).thenReturn(response)
|
||||
}
|
||||
|
||||
private fun verifyGetCommunity(page: Int, times: Int? = null) {
|
||||
val verification = times?.let { verify(repository, times(it)) } ?: verify(repository)
|
||||
verification.getCommunity(
|
||||
100L,
|
||||
page,
|
||||
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
|
||||
"Bearer test-token"
|
||||
)
|
||||
}
|
||||
|
||||
private fun setImmediateRxSchedulers() {
|
||||
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
|
||||
RxJavaPlugins.setIoSchedulerHandler(trampoline)
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
}
|
||||
|
||||
private fun communityResponse(
|
||||
page: Int,
|
||||
ids: List<Long>,
|
||||
hasNext: Boolean
|
||||
) = CreatorChannelCommunityTabResponse(
|
||||
communityPostCount = ids.size,
|
||||
communityPosts = ids.map { communityPost(it) },
|
||||
page = page,
|
||||
size = CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun communityPost(id: Long) = CreatorChannelCommunityPostResponse(
|
||||
postId = id,
|
||||
creatorId = 100L,
|
||||
creatorNickname = "creator",
|
||||
creatorProfileUrl = "profile.png",
|
||||
createdAtUtc = "2026-06-21T00:00:00Z",
|
||||
content = "community $id",
|
||||
imageUrl = null,
|
||||
audioUrl = null,
|
||||
price = 0,
|
||||
existOrdered = true,
|
||||
isCommentAvailable = true,
|
||||
likeCount = 1,
|
||||
commentCount = 2,
|
||||
isPinned = false
|
||||
)
|
||||
|
||||
private fun <T> LiveData<T>.requireValue(): T? {
|
||||
var value: T? = null
|
||||
val observer = Observer<T> { value = it }
|
||||
observeForever(observer)
|
||||
removeObserver(observer)
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.community
|
||||
|
||||
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 kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityPostResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.never
|
||||
import org.mockito.kotlin.times
|
||||
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 CreatorChannelCommunityViewModelTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var repository: CreatorChannelRepository
|
||||
private lateinit var viewModel: CreatorChannelCommunityViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
repository = org.mockito.kotlin.mock()
|
||||
viewModel = CreatorChannelCommunityViewModel(repository)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `최초 로드는 page 0 size 20으로 커뮤니티 API를 호출하고 기본 리스트형 Content를 emit한다`() {
|
||||
stubGetCommunity(response = Single.just(ApiResponse(true, communityResponse(ids = listOf(1L)), null)))
|
||||
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
|
||||
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
assertEquals(CreatorChannelCommunityViewMode.List, state.viewMode)
|
||||
assertEquals(0, state.page)
|
||||
assertEquals(CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE, state.size)
|
||||
assertEquals(listOf(1L), state.communityPosts.map { it.postId })
|
||||
verifyGetCommunity()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `보기 방식 toggle은 리스트형 그리드형 리스트형 순서이며 API를 재호출하지 않는다`() {
|
||||
stubGetCommunity(response = Single.just(ApiResponse(true, communityResponse(ids = listOf(1L)), null)))
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
|
||||
viewModel.toggleViewMode()
|
||||
val gridState = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
viewModel.toggleViewMode()
|
||||
val listState = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
|
||||
assertEquals(CreatorChannelCommunityViewMode.Grid, gridState.viewMode)
|
||||
assertEquals(CreatorChannelCommunityViewMode.List, listState.viewMode)
|
||||
assertEquals(listOf(1L), listState.communityPosts.map { it.postId })
|
||||
verifyGetCommunity(times = 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `communityPostCount가 0이면 표시 가능한 item이 있어도 Empty를 emit한다`() {
|
||||
stubGetCommunity(
|
||||
response = Single.just(
|
||||
ApiResponse(true, communityResponse(communityPostCount = 0, ids = listOf(1L)), null)
|
||||
)
|
||||
)
|
||||
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
|
||||
assertTrue(viewModel.communityStateLiveData.requireValue() is CreatorChannelCommunityUiState.Empty)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `표시 가능한 communityPosts가 없으면 Empty를 emit한다`() {
|
||||
stubGetCommunity(response = Single.just(ApiResponse(true, communityResponse(ids = emptyList()), null)))
|
||||
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
|
||||
assertTrue(viewModel.communityStateLiveData.requireValue() is CreatorChannelCommunityUiState.Empty)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `retryCommunity는 첫 페이지를 다시 조회한다`() {
|
||||
whenever(
|
||||
repository.getCommunity(
|
||||
100L,
|
||||
0,
|
||||
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
|
||||
"Bearer test-token"
|
||||
)
|
||||
).thenReturn(
|
||||
Single.just(ApiResponse(false, null, "failed")),
|
||||
Single.just(ApiResponse(true, communityResponse(ids = listOf(2L)), null))
|
||||
)
|
||||
viewModel.loadCommunity(100L, isOwner = false)
|
||||
|
||||
viewModel.retryCommunity()
|
||||
|
||||
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
|
||||
assertEquals(listOf(2L), state.communityPosts.map { it.postId })
|
||||
verifyGetCommunity(times = 2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `creatorId가 0 이하이면 커뮤니티 API를 호출하지 않는다`() {
|
||||
viewModel.loadCommunity(0L, isOwner = false)
|
||||
|
||||
verify(repository, never()).getCommunity(any(), any(), any(), any())
|
||||
}
|
||||
|
||||
private fun stubGetCommunity(
|
||||
page: Int = 0,
|
||||
response: Single<ApiResponse<CreatorChannelCommunityTabResponse>>
|
||||
) {
|
||||
whenever(
|
||||
repository.getCommunity(
|
||||
100L,
|
||||
page,
|
||||
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
|
||||
"Bearer test-token"
|
||||
)
|
||||
).thenReturn(response)
|
||||
}
|
||||
|
||||
private fun verifyGetCommunity(page: Int = 0, times: Int? = null) {
|
||||
val verification = times?.let { verify(repository, times(it)) } ?: verify(repository)
|
||||
verification.getCommunity(
|
||||
100L,
|
||||
page,
|
||||
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
|
||||
"Bearer test-token"
|
||||
)
|
||||
}
|
||||
|
||||
private fun setImmediateRxSchedulers() {
|
||||
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
|
||||
RxJavaPlugins.setIoSchedulerHandler(trampoline)
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
}
|
||||
|
||||
private fun communityResponse(
|
||||
page: Int = 0,
|
||||
communityPostCount: Int? = null,
|
||||
ids: List<Long>,
|
||||
hasNext: Boolean = false
|
||||
) = CreatorChannelCommunityTabResponse(
|
||||
communityPostCount = communityPostCount ?: ids.size,
|
||||
communityPosts = ids.map { communityPost(it) },
|
||||
page = page,
|
||||
size = CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun communityPost(id: Long) = CreatorChannelCommunityPostResponse(
|
||||
postId = id,
|
||||
creatorId = 100L,
|
||||
creatorNickname = "creator",
|
||||
creatorProfileUrl = "profile.png",
|
||||
createdAtUtc = "2026-06-21T00:00:00Z",
|
||||
content = "community $id",
|
||||
imageUrl = null,
|
||||
audioUrl = null,
|
||||
price = 0,
|
||||
existOrdered = true,
|
||||
isCommentAvailable = true,
|
||||
likeCount = 1,
|
||||
commentCount = 2,
|
||||
isPinned = false
|
||||
)
|
||||
|
||||
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