feat(gift): 선물 신청 입력 검증을 연결한다
This commit is contained in:
@@ -0,0 +1,112 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.gift
|
||||||
|
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import com.orhanobut.logger.Logger
|
||||||
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||||
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||||
|
import kr.co.vividnext.sodalive.R
|
||||||
|
import kr.co.vividnext.sodalive.base.BaseViewModel
|
||||||
|
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||||
|
import kr.co.vividnext.sodalive.common.ToastMessage
|
||||||
|
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||||
|
|
||||||
|
sealed interface GiftSendOptionsState {
|
||||||
|
data object Loading : GiftSendOptionsState
|
||||||
|
data class Content(val options: GiftFormOptionsUiModel) : GiftSendOptionsState
|
||||||
|
data class Error(val message: String?) : GiftSendOptionsState
|
||||||
|
}
|
||||||
|
|
||||||
|
class GiftSendRequestViewModel(
|
||||||
|
private val repository: GiftRepository
|
||||||
|
) : BaseViewModel() {
|
||||||
|
|
||||||
|
private val _optionsStateLiveData = MutableLiveData<GiftSendOptionsState>()
|
||||||
|
val optionsStateLiveData: LiveData<GiftSendOptionsState>
|
||||||
|
get() = _optionsStateLiveData
|
||||||
|
|
||||||
|
private val _toastLiveData = MutableLiveData<ToastMessage?>()
|
||||||
|
val toastLiveData: LiveData<ToastMessage?>
|
||||||
|
get() = _toastLiveData
|
||||||
|
|
||||||
|
private val _isSubmitLoading = MutableLiveData(false)
|
||||||
|
val isSubmitLoading: LiveData<Boolean>
|
||||||
|
get() = _isSubmitLoading
|
||||||
|
|
||||||
|
private var submitSuccessEvent = false
|
||||||
|
|
||||||
|
fun loadFormOptions() {
|
||||||
|
_optionsStateLiveData.value = GiftSendOptionsState.Loading
|
||||||
|
compositeDisposable.add(
|
||||||
|
repository.getFormOptions(token = authToken())
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(
|
||||||
|
{ response ->
|
||||||
|
val data = response.data
|
||||||
|
if (response.success && data != null) {
|
||||||
|
_optionsStateLiveData.value = GiftSendOptionsState.Content(
|
||||||
|
GiftSendRequestPresenter.toOptionsUiModel(data)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
showOptionsError(response.message)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ throwable ->
|
||||||
|
throwable.message?.let { Logger.e(it) }
|
||||||
|
showOptionsError(throwable.message)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun submitGift(entry: GiftSendEntry, formState: GiftSendFormState) {
|
||||||
|
if (_isSubmitLoading.value == true) return
|
||||||
|
_isSubmitLoading.value = true
|
||||||
|
compositeDisposable.add(
|
||||||
|
repository.createGift(
|
||||||
|
request = GiftSendRequestPresenter.toCreateRequest(entry, formState),
|
||||||
|
token = authToken()
|
||||||
|
)
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(
|
||||||
|
{ response ->
|
||||||
|
_isSubmitLoading.value = false
|
||||||
|
if (response.success) {
|
||||||
|
submitSuccessEvent = true
|
||||||
|
_toastLiveData.value = ToastMessage(resId = R.string.gift_send_submit_success)
|
||||||
|
} else {
|
||||||
|
showToast(response.message)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ throwable ->
|
||||||
|
_isSubmitLoading.value = false
|
||||||
|
throwable.message?.let { Logger.e(it) }
|
||||||
|
showToast(throwable.message)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun consumeSubmitSuccessEvent(): Boolean {
|
||||||
|
if (!submitSuccessEvent) return false
|
||||||
|
submitSuccessEvent = false
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showOptionsError(message: String?) {
|
||||||
|
_optionsStateLiveData.value = GiftSendOptionsState.Error(message)
|
||||||
|
showToast(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showToast(message: String?) {
|
||||||
|
_toastLiveData.value = if (message.isNullOrBlank()) {
|
||||||
|
ToastMessage(resId = R.string.common_error_unknown)
|
||||||
|
} else {
|
||||||
|
ToastMessage(message = message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
|
||||||
|
}
|
||||||
@@ -0,0 +1,203 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.gift
|
||||||
|
|
||||||
|
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.gift.data.GiftApi
|
||||||
|
import kr.co.vividnext.sodalive.v2.gift.data.GiftCategoryResponse
|
||||||
|
import kr.co.vividnext.sodalive.v2.gift.data.GiftCreateRequest
|
||||||
|
import kr.co.vividnext.sodalive.v2.gift.data.GiftFormOptionsResponse
|
||||||
|
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||||
|
import kr.co.vividnext.sodalive.v2.gift.data.GiftSizeResponse
|
||||||
|
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.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(sdk = [28], application = Application::class)
|
||||||
|
class GiftSendRequestViewModelTest {
|
||||||
|
|
||||||
|
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||||
|
private lateinit var api: FakeGiftApi
|
||||||
|
private lateinit var viewModel: GiftSendRequestViewModel
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
setImmediateRxSchedulers()
|
||||||
|
SharedPreferenceManager.resetForTest()
|
||||||
|
SharedPreferenceManager.init(context)
|
||||||
|
SharedPreferenceManager.token = "test-token"
|
||||||
|
api = FakeGiftApi()
|
||||||
|
viewModel = GiftSendRequestViewModel(GiftRepository(api))
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
fun tearDown() {
|
||||||
|
RxJavaPlugins.reset()
|
||||||
|
RxAndroidPlugins.reset()
|
||||||
|
SharedPreferenceManager.resetForTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `폼 옵션 조회 성공 시 UI 옵션 상태가 채워진다`() {
|
||||||
|
api.enqueueFormOptions(ApiResponse(true, formOptions(), null))
|
||||||
|
|
||||||
|
viewModel.loadFormOptions()
|
||||||
|
|
||||||
|
val state = viewModel.optionsStateLiveData.requireValue() as GiftSendOptionsState.Content
|
||||||
|
assertEquals(listOf("SMALL"), state.options.sizes.map { it.id })
|
||||||
|
assertEquals(listOf(10L), state.options.categories.map { it.id })
|
||||||
|
assertEquals("Bearer test-token", api.lastAuthHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `폼 옵션 조회 실패 시 오류 토스트 상태가 만들어진다`() {
|
||||||
|
api.enqueueFormOptions(ApiResponse(false, null, "옵션 실패"))
|
||||||
|
|
||||||
|
viewModel.loadFormOptions()
|
||||||
|
|
||||||
|
assertTrue(viewModel.optionsStateLiveData.requireValue() is GiftSendOptionsState.Error)
|
||||||
|
assertEquals("옵션 실패", viewModel.toastLiveData.requireValue()?.message)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `폼 옵션 조회 중에는 Loading 상태이고 완료되면 Content 상태가 된다`() {
|
||||||
|
val pending = SingleSubject.create<ApiResponse<GiftFormOptionsResponse>>()
|
||||||
|
api.enqueueFormOptions(pending)
|
||||||
|
|
||||||
|
viewModel.loadFormOptions()
|
||||||
|
|
||||||
|
assertTrue(viewModel.optionsStateLiveData.requireValue() is GiftSendOptionsState.Loading)
|
||||||
|
|
||||||
|
pending.onSuccess(ApiResponse(true, formOptions(), null))
|
||||||
|
|
||||||
|
assertTrue(viewModel.optionsStateLiveData.requireValue() is GiftSendOptionsState.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `제출 성공 이벤트는 한 번만 소비된다`() {
|
||||||
|
api.enqueueCreateGift(ApiResponse(true, Any(), null))
|
||||||
|
|
||||||
|
viewModel.submitGift(entry(), completeForm())
|
||||||
|
|
||||||
|
assertTrue(viewModel.consumeSubmitSuccessEvent())
|
||||||
|
assertFalse(viewModel.consumeSubmitSuccessEvent())
|
||||||
|
assertEquals("small", api.lastCreateRequest?.sizeCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `제출 실패 시 성공 이벤트 없이 오류 토스트가 만들어진다`() {
|
||||||
|
api.enqueueCreateGift(ApiResponse(false, null, "신청 실패"))
|
||||||
|
|
||||||
|
viewModel.submitGift(entry(), completeForm())
|
||||||
|
|
||||||
|
assertFalse(viewModel.consumeSubmitSuccessEvent())
|
||||||
|
assertEquals("신청 실패", viewModel.toastLiveData.requireValue()?.message)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `제출 중에는 loading이 true이고 완료되면 false가 된다`() {
|
||||||
|
val pending = SingleSubject.create<ApiResponse<Any>>()
|
||||||
|
api.enqueueCreateGift(pending)
|
||||||
|
|
||||||
|
viewModel.submitGift(entry(), completeForm())
|
||||||
|
|
||||||
|
assertEquals(true, viewModel.isSubmitLoading.requireValue())
|
||||||
|
|
||||||
|
pending.onSuccess(ApiResponse(true, Any(), null))
|
||||||
|
|
||||||
|
assertEquals(false, viewModel.isSubmitLoading.requireValue())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun formOptions(): GiftFormOptionsResponse = GiftFormOptionsResponse(
|
||||||
|
sizes = listOf(GiftSizeResponse(code = "SMALL", name = "소형", salePriceCan = 100)),
|
||||||
|
categories = listOf(GiftCategoryResponse(id = 10L, name = "일반", requiresDamageWaiver = false))
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun entry(): GiftSendEntry = GiftSendEntry(creatorMemberId = 99L, creatorNickname = "크리에이터")
|
||||||
|
|
||||||
|
private fun completeForm(): GiftSendFormState = GiftSendFormState(
|
||||||
|
senderName = "홍길동",
|
||||||
|
phoneNumber = "01000000000",
|
||||||
|
zoneCode = "00000",
|
||||||
|
address = "기본 주소",
|
||||||
|
detailAddress = "상세 주소",
|
||||||
|
selectedSizeId = "small",
|
||||||
|
selectedSizeSalePriceCan = 100,
|
||||||
|
selectedCategoryId = 10L,
|
||||||
|
selectedCategoryRequiresDamageWaiver = false,
|
||||||
|
termsAgreed = true,
|
||||||
|
privacyAgreed = true,
|
||||||
|
damageLossAgreed = false
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
private class FakeGiftApi : GiftApi {
|
||||||
|
private val formOptionsResponses = ArrayDeque<Single<ApiResponse<GiftFormOptionsResponse>>>()
|
||||||
|
private val createGiftResponses = ArrayDeque<Single<ApiResponse<Any>>>()
|
||||||
|
var lastAuthHeader: String? = null
|
||||||
|
private set
|
||||||
|
var lastCreateRequest: GiftCreateRequest? = null
|
||||||
|
private set
|
||||||
|
|
||||||
|
fun enqueueFormOptions(response: ApiResponse<GiftFormOptionsResponse>) {
|
||||||
|
formOptionsResponses.addLast(Single.just(response))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun enqueueFormOptions(response: Single<ApiResponse<GiftFormOptionsResponse>>) {
|
||||||
|
formOptionsResponses.addLast(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun enqueueCreateGift(response: ApiResponse<Any>) {
|
||||||
|
createGiftResponses.addLast(Single.just(response))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun enqueueCreateGift(response: Single<ApiResponse<Any>>) {
|
||||||
|
createGiftResponses.addLast(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getFormOptions(authHeader: String): Single<ApiResponse<GiftFormOptionsResponse>> {
|
||||||
|
lastAuthHeader = authHeader
|
||||||
|
return formOptionsResponses.removeFirst()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun createGift(
|
||||||
|
request: GiftCreateRequest,
|
||||||
|
authHeader: String
|
||||||
|
): Single<ApiResponse<Any>> {
|
||||||
|
lastAuthHeader = authHeader
|
||||||
|
lastCreateRequest = request
|
||||||
|
return createGiftResponses.removeFirst()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user