feat(mypage): 최신 공지 배열 상태를 전달한다

This commit is contained in:
2026-10-28 19:16:18 +09:00
parent 157787c6b6
commit 51a8d092a5
2 changed files with 116 additions and 7 deletions
@@ -9,9 +9,9 @@ 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.settings.notice.NoticeItem
import kr.co.vividnext.sodalive.settings.notice.NoticeRepository
import kr.co.vividnext.sodalive.user.UserRepository
import kr.co.vividnext.sodalive.v2.mypage.notice.LatestNoticeResponse
class MyPageViewModel(
private val userRepository: UserRepository,
@@ -29,9 +29,9 @@ class MyPageViewModel(
val myPageLiveData: LiveData<MyPageResponse>
get() = _myPageLiveData
private val _latestNoticeLiveData = MutableLiveData<NoticeItem?>()
val latestNoticeLiveData: LiveData<NoticeItem?>
get() = _latestNoticeLiveData
private val _latestNoticesLiveData = MutableLiveData<List<LatestNoticeResponse>>()
val latestNoticesLiveData: LiveData<List<LatestNoticeResponse>>
get() = _latestNoticesLiveData
fun getUserInfo() {
if (!_isLoading.value!!) {
@@ -68,15 +68,15 @@ class MyPageViewModel(
)
}
fun getLatestNotice() {
fun getLatestNotices() {
compositeDisposable.add(
noticeRepository.getLatestNotice()
noticeRepository.getLatestNotices()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ response ->
if (response.success && response.data != null) {
_latestNoticeLiveData.postValue(response.data)
_latestNoticesLiveData.postValue(response.data)
} else {
Logger.e("Failed to get latest notice: ${response.message}")
}
@@ -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()
}
}