feat(creator): 후원 탭 상태 관리를 추가한다
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.donation
|
||||
|
||||
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.AndroidUtcRelativeTimeTextFormatter
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.common.SodaLiveApplicationHolder
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.MemberDonationRankingResponse
|
||||
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 CreatorChannelDonationPaginationTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var repository: CreatorChannelRepository
|
||||
private lateinit var viewModel: CreatorChannelDonationViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
SodaLiveApplicationHolder.init(context as Application)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
repository = org.mockito.kotlin.mock()
|
||||
viewModel = CreatorChannelDonationViewModel(repository, AndroidUtcRelativeTimeTextFormatter(context))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hasNext가 true이면 다음 페이지는 마지막 응답의 page plus 1로 요청하고 append한다`() {
|
||||
stubGetDonations(0, Single.just(ApiResponse(true, donationResponse(page = 0, ids = listOf(1L), hasNext = true), null)))
|
||||
stubGetDonations(1, Single.just(ApiResponse(true, donationResponse(page = 1, ids = listOf(2L), hasNext = false), null)))
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
|
||||
val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content
|
||||
assertEquals(1, state.page)
|
||||
assertEquals(listOf("member 1", "member 2"), state.donations.map { it.nickname })
|
||||
assertFalse(state.hasNext)
|
||||
verifyGetDonations(page = 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `다음 페이지 로딩 중 중복 load-more 요청은 막고 size 20을 유지한다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<CreatorChannelDonationTabResponse>>()
|
||||
stubGetDonations(0, Single.just(ApiResponse(true, donationResponse(page = 0, ids = listOf(1L), hasNext = true), null)))
|
||||
stubGetDonations(1, pending)
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
viewModel.loadMore()
|
||||
|
||||
val loadingState = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content
|
||||
assertTrue(loadingState.isLoadingMore)
|
||||
verifyGetDonations(page = 1, times = 1)
|
||||
pending.onSuccess(ApiResponse(true, donationResponse(page = 1, ids = listOf(2L), hasNext = false), null))
|
||||
val loadedState = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content
|
||||
assertEquals(CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, loadedState.size)
|
||||
assertEquals(listOf("member 1", "member 2"), loadedState.donations.map { it.nickname })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `다음 페이지 실패는 기존 목록을 유지하고 pagination error message만 설정한다`() {
|
||||
stubGetDonations(0, Single.just(ApiResponse(true, donationResponse(page = 0, ids = listOf(1L), hasNext = true), null)))
|
||||
stubGetDonations(1, Single.just(ApiResponse(false, null, "failed")))
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
|
||||
val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content
|
||||
assertEquals(listOf("member 1"), state.donations.map { it.nickname })
|
||||
assertFalse(state.isLoadingMore)
|
||||
assertEquals("failed", state.paginationErrorMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `pagination error message는 consume 후 null이 된다`() {
|
||||
stubGetDonations(0, Single.just(ApiResponse(true, donationResponse(page = 0, ids = listOf(1L), hasNext = true), null)))
|
||||
stubGetDonations(1, Single.just(ApiResponse(false, null, "failed")))
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
viewModel.loadMore()
|
||||
|
||||
viewModel.consumePaginationErrorMessage()
|
||||
|
||||
val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content
|
||||
assertEquals(null, state.paginationErrorMessage)
|
||||
assertEquals(listOf("member 1"), state.donations.map { it.nickname })
|
||||
}
|
||||
|
||||
private fun stubGetDonations(page: Int, response: Single<ApiResponse<CreatorChannelDonationTabResponse>>) {
|
||||
whenever(repository.getDonations(100L, page, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token"))
|
||||
.thenReturn(response)
|
||||
}
|
||||
|
||||
private fun verifyGetDonations(page: Int, times: Int? = null) {
|
||||
val verification = times?.let { verify(repository, times(it)) } ?: verify(repository)
|
||||
verification.getDonations(100L, page, CreatorChannelDonationViewModel.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 donationResponse(page: Int, ids: List<Long>, hasNext: Boolean) = CreatorChannelDonationTabResponse(
|
||||
donationCount = ids.size,
|
||||
rankings = listOf(ranking()),
|
||||
donations = ids.map { donation(it) },
|
||||
page = page,
|
||||
size = CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun ranking() = MemberDonationRankingResponse(
|
||||
userId = 10L,
|
||||
nickname = "member",
|
||||
profileImage = "profile.png",
|
||||
donationCan = 100
|
||||
)
|
||||
|
||||
private fun donation(id: Long) = CreatorChannelDonationResponse(
|
||||
nickname = "member $id",
|
||||
profileImageUrl = "profile.png",
|
||||
can = 50,
|
||||
message = "응원 $id",
|
||||
createdAtUtc = "2026-06-21T00:00:00Z"
|
||||
)
|
||||
|
||||
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,191 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.donation
|
||||
|
||||
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.AndroidUtcRelativeTimeTextFormatter
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.common.SodaLiveApplicationHolder
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.MemberDonationRankingResponse
|
||||
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 CreatorChannelDonationViewModelTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var repository: CreatorChannelRepository
|
||||
private lateinit var viewModel: CreatorChannelDonationViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
SodaLiveApplicationHolder.init(context as Application)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
SharedPreferenceManager.can = 200
|
||||
repository = org.mockito.kotlin.mock()
|
||||
viewModel = CreatorChannelDonationViewModel(repository, AndroidUtcRelativeTimeTextFormatter(context))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `최초 로드는 page 0 size 20으로 Donation API를 호출하고 Content를 emit한다`() {
|
||||
stubGetDonations(response = Single.just(ApiResponse(true, donationResponse(ids = listOf(1L)), null)))
|
||||
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
|
||||
val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content
|
||||
assertEquals(0, state.page)
|
||||
assertEquals(CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, state.size)
|
||||
assertEquals(listOf("member 1"), state.donations.map { it.nickname })
|
||||
verifyGetDonations()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `donationCount가 0이면 Empty 상태가 된다`() {
|
||||
stubGetDonations(response = Single.just(ApiResponse(true, donationResponse(donationCount = 0, ids = listOf(1L)), null)))
|
||||
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
|
||||
val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Empty
|
||||
assertEquals(0, state.donationCount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `표시 가능한 donations가 없으면 Empty 상태가 된다`() {
|
||||
stubGetDonations(response = Single.just(ApiResponse(true, donationResponse(ids = emptyList()), null)))
|
||||
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
|
||||
assertTrue(viewModel.donationStateLiveData.requireValue() is CreatorChannelDonationUiState.Empty)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `creatorId가 0 이하이면 API를 호출하지 않는다`() {
|
||||
viewModel.loadDonations(0L, isOwner = false)
|
||||
|
||||
verify(repository, never()).getDonations(any(), any(), any(), any())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `채널 후원 성공은 can을 차감하고 첫 페이지를 다시 로드하며 성공 이벤트를 emit한다`() {
|
||||
whenever(repository.getDonations(100L, 0, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token"))
|
||||
.thenReturn(Single.just(ApiResponse(true, donationResponse(ids = listOf(1L)), null)))
|
||||
.thenReturn(Single.just(ApiResponse(true, donationResponse(ids = listOf(2L)), null)))
|
||||
whenever(repository.postChannelDonation(100L, 50, true, "응원", "Bearer test-token")).thenReturn(
|
||||
Single.just(ApiResponse(true, Any(), null))
|
||||
)
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
|
||||
viewModel.postChannelDonation(can = 50, isSecret = true, message = "응원")
|
||||
|
||||
verify(repository).postChannelDonation(100L, 50, true, "응원", "Bearer test-token")
|
||||
verify(repository, times(2)).getDonations(100L, 0, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token")
|
||||
assertEquals(150, SharedPreferenceManager.can)
|
||||
assertEquals(true, viewModel.consumeDonationSuccessEvent())
|
||||
assertEquals(false, viewModel.consumeDonationSuccessEvent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `채널 후원 성공 시 보유 can보다 큰 금액 차감은 0으로 보정한다`() {
|
||||
whenever(repository.getDonations(100L, 0, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token"))
|
||||
.thenReturn(Single.just(ApiResponse(true, donationResponse(ids = listOf(1L)), null)))
|
||||
.thenReturn(Single.just(ApiResponse(true, donationResponse(ids = listOf(2L)), null)))
|
||||
whenever(repository.postChannelDonation(100L, 250, false, "응원", "Bearer test-token")).thenReturn(
|
||||
Single.just(ApiResponse(true, Any(), null))
|
||||
)
|
||||
viewModel.loadDonations(100L, isOwner = false)
|
||||
|
||||
SharedPreferenceManager.can = 30
|
||||
viewModel.postChannelDonation(can = 250, isSecret = false, message = "응원")
|
||||
|
||||
assertEquals(0, SharedPreferenceManager.can)
|
||||
}
|
||||
|
||||
private fun stubGetDonations(
|
||||
page: Int = 0,
|
||||
response: Single<ApiResponse<CreatorChannelDonationTabResponse>>
|
||||
) {
|
||||
whenever(repository.getDonations(100L, page, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token"))
|
||||
.thenReturn(response)
|
||||
}
|
||||
|
||||
private fun verifyGetDonations(page: Int = 0) {
|
||||
verify(repository).getDonations(100L, page, CreatorChannelDonationViewModel.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 donationResponse(
|
||||
page: Int = 0,
|
||||
donationCount: Int? = null,
|
||||
ids: List<Long>,
|
||||
hasNext: Boolean = false
|
||||
) = CreatorChannelDonationTabResponse(
|
||||
donationCount = donationCount ?: ids.size,
|
||||
rankings = listOf(ranking()),
|
||||
donations = ids.map { donation(it) },
|
||||
page = page,
|
||||
size = CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun ranking() = MemberDonationRankingResponse(
|
||||
userId = 10L,
|
||||
nickname = "member",
|
||||
profileImage = "profile.png",
|
||||
donationCan = 100
|
||||
)
|
||||
|
||||
private fun donation(id: Long) = CreatorChannelDonationResponse(
|
||||
nickname = "member $id",
|
||||
profileImageUrl = "profile.png",
|
||||
can = 50,
|
||||
message = "응원 $id",
|
||||
createdAtUtc = "2026-06-21T00:00:00Z"
|
||||
)
|
||||
|
||||
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