feat(mypage): 최신 공지 배열 상태를 전달한다
This commit is contained in:
+109
@@ -0,0 +1,109 @@
|
||||
package kr.co.vividnext.sodalive.v2.mypage.notice
|
||||
|
||||
import android.app.Application
|
||||
import android.os.Looper
|
||||
import androidx.lifecycle.Observer
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
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.mypage.MyPageViewModel
|
||||
import kr.co.vividnext.sodalive.settings.notice.NoticeApi
|
||||
import kr.co.vividnext.sodalive.settings.notice.NoticeRepository
|
||||
import kr.co.vividnext.sodalive.user.UserRepository
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.verifyNoInteractions
|
||||
import org.mockito.kotlin.whenever
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class MyPageNoticesViewModelTest {
|
||||
private val api: NoticeApi = mock()
|
||||
private val userRepository: UserRepository = mock()
|
||||
private lateinit var viewModel: MyPageViewModel
|
||||
private val notices = listOf(
|
||||
LatestNoticeResponse(9L, "첫 제목", "<p>본문 전체</p>", "2026-10-28"),
|
||||
LatestNoticeResponse(3L, "둘째 제목", "<div>둘째</div>", "2026-10-27")
|
||||
)
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
viewModel = MyPageViewModel(userRepository, NoticeRepository(api))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `성공 배열을 순서와 원문 그대로 전달하고 이후 성공 빈 배열도 전달한다`() {
|
||||
load(Single.just(ApiResponse(true, notices)))
|
||||
assertEquals(notices, viewModel.latestNoticesLiveData.value)
|
||||
load(Single.just(ApiResponse(true, emptyList())))
|
||||
assertEquals(emptyList<LatestNoticeResponse>(), viewModel.latestNoticesLiveData.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `실패 응답은 이전 성공 배열을 유지한다`() {
|
||||
assertPreserved(Single.just(ApiResponse(false, emptyList(), "failed")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `성공이지만 null data이면 이전 성공 배열을 유지한다`() {
|
||||
assertPreserved(Single.just(ApiResponse(true)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `네트워크 오류는 이전 성공 배열을 유지한다`() {
|
||||
assertPreserved(Single.error(IllegalStateException("test network failure")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `최초 실패는 공지 데이터와 프로필 로딩 Toast를 만들지 않는다`() {
|
||||
val loading = mutableListOf<Boolean>()
|
||||
val toasts = mutableListOf<Any?>()
|
||||
val loadingObserver = Observer<Boolean> { loading += it }
|
||||
val toastObserver = Observer<kr.co.vividnext.sodalive.common.ToastMessage?> { toasts += it }
|
||||
viewModel.isLoading.observeForever(loadingObserver)
|
||||
viewModel.toastLiveData.observeForever(toastObserver)
|
||||
load(Single.just(ApiResponse(false, null, "failed")))
|
||||
load(Single.error(IllegalStateException("test network failure")))
|
||||
assertNull(viewModel.latestNoticesLiveData.value)
|
||||
load(Single.just(ApiResponse(true, notices)))
|
||||
load(Single.just(ApiResponse(true, emptyList())))
|
||||
assertEquals(listOf(false), loading)
|
||||
assertEquals(emptyList<Any?>(), toasts)
|
||||
assertNull(viewModel.myPageLiveData.value)
|
||||
verifyNoInteractions(userRepository)
|
||||
viewModel.isLoading.removeObserver(loadingObserver)
|
||||
viewModel.toastLiveData.removeObserver(toastObserver)
|
||||
}
|
||||
|
||||
private fun assertPreserved(response: Single<ApiResponse<List<LatestNoticeResponse>>>) {
|
||||
load(Single.just(ApiResponse(true, notices)))
|
||||
assertEquals(notices, viewModel.latestNoticesLiveData.value)
|
||||
load(response)
|
||||
assertEquals(notices, viewModel.latestNoticesLiveData.value)
|
||||
}
|
||||
|
||||
private fun load(response: Single<ApiResponse<List<LatestNoticeResponse>>>) {
|
||||
whenever(api.getLatestNotices()).thenReturn(response)
|
||||
viewModel.getLatestNotices()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user