|
| 1 | +package com.runnect.runnect.presentation.mypage |
| 2 | + |
| 3 | +import app.cash.turbine.test |
| 4 | +import app.cash.turbine.turbineScope |
| 5 | +import com.runnect.runnect.domain.entity.User |
| 6 | +import com.runnect.runnect.domain.repository.UserRepository |
| 7 | +import io.mockk.coEvery |
| 8 | +import io.mockk.mockk |
| 9 | +import kotlinx.coroutines.Dispatchers |
| 10 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 11 | +import kotlinx.coroutines.delay |
| 12 | +import kotlinx.coroutines.flow.flow |
| 13 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 14 | +import kotlinx.coroutines.test.resetMain |
| 15 | +import kotlinx.coroutines.test.runTest |
| 16 | +import kotlinx.coroutines.test.setMain |
| 17 | +import org.junit.After |
| 18 | +import org.junit.Assert.assertEquals |
| 19 | +import org.junit.Assert.assertFalse |
| 20 | +import org.junit.Assert.assertNull |
| 21 | +import org.junit.Assert.assertTrue |
| 22 | +import org.junit.Before |
| 23 | +import org.junit.Test |
| 24 | + |
| 25 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 26 | +class MyPageViewModelTest { |
| 27 | + |
| 28 | + private val testDispatcher = StandardTestDispatcher() |
| 29 | + |
| 30 | + private lateinit var userRepository: UserRepository |
| 31 | + private lateinit var viewModel: MyPageViewModel |
| 32 | + |
| 33 | + @Before |
| 34 | + fun setUp() { |
| 35 | + Dispatchers.setMain(testDispatcher) |
| 36 | + userRepository = mockk() |
| 37 | + } |
| 38 | + |
| 39 | + @After |
| 40 | + fun tearDown() { |
| 41 | + Dispatchers.resetMain() |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `LoadUserInfo 성공 시 유저 정보로 상태가 갱신된다`() = runTest(testDispatcher) { |
| 46 | + val user = User( |
| 47 | + email = "runner@runnect.com", |
| 48 | + latestStamp = "CSPR0", |
| 49 | + level = 3, |
| 50 | + levelPercent = 42, |
| 51 | + nickname = "러너" |
| 52 | + ) |
| 53 | + coEvery { userRepository.getUserInfo() } returns flow { |
| 54 | + delay(1) // onLoading 상태를 별도 프레임으로 관찰하기 위한 실제 suspension 지점 |
| 55 | + emit(Result.success(user)) |
| 56 | + } |
| 57 | + viewModel = MyPageViewModel(userRepository) |
| 58 | + |
| 59 | + viewModel.state.test { |
| 60 | + val initial = awaitItem() |
| 61 | + assertEquals(MyPageUiState(), initial) |
| 62 | + assertTrue("초기 상태는 isLoading=true가 기본값", initial.isLoading) |
| 63 | + |
| 64 | + viewModel.intent(MyPageIntent.LoadUserInfo) |
| 65 | + |
| 66 | + // MyPageUiState의 isLoading 기본값이 true라서, onLoading reduce는 |
| 67 | + // 초기 상태와 동일해 StateFlow가 별도로 emit하지 않고 곧바로 결과 상태로 넘어간다. |
| 68 | + val success = awaitItem() |
| 69 | + assertFalse(success.isLoading) |
| 70 | + assertEquals("러너", success.nickname) |
| 71 | + assertEquals("CSPR0", success.stampId) |
| 72 | + assertEquals("3", success.level) |
| 73 | + assertEquals(42, success.levelPercent) |
| 74 | + assertEquals("runner@runnect.com", success.email) |
| 75 | + assertNull(success.error) |
| 76 | + |
| 77 | + cancelAndIgnoreRemainingEvents() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun `LoadUserInfo 실패 시 에러 상태와 ShowError 이펙트가 발생한다`() = runTest(testDispatcher) { |
| 83 | + coEvery { userRepository.getUserInfo() } returns flow { |
| 84 | + delay(1) |
| 85 | + throw RuntimeException("네트워크 오류") |
| 86 | + } |
| 87 | + viewModel = MyPageViewModel(userRepository) |
| 88 | + |
| 89 | + turbineScope { |
| 90 | + val stateTurbine = viewModel.state.testIn(backgroundScope) |
| 91 | + val effectTurbine = viewModel.effect.testIn(backgroundScope) |
| 92 | + |
| 93 | + assertEquals(MyPageUiState(), stateTurbine.awaitItem()) |
| 94 | + |
| 95 | + viewModel.intent(MyPageIntent.LoadUserInfo) |
| 96 | + |
| 97 | + // 초기 상태가 이미 isLoading=true라 onLoading reduce는 별도로 emit되지 않는다. |
| 98 | + val failure = stateTurbine.awaitItem() |
| 99 | + assertFalse(failure.isLoading) |
| 100 | + assertEquals("네트워크 오류 (unknown)", failure.error) |
| 101 | + |
| 102 | + val effect = effectTurbine.awaitItem() |
| 103 | + assertTrue(effect is MyPageEffect.ShowError) |
| 104 | + |
| 105 | + stateTurbine.cancelAndIgnoreRemainingEvents() |
| 106 | + effectTurbine.cancelAndIgnoreRemainingEvents() |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `UpdateNickname 인텐트는 닉네임 상태를 갱신한다`() = runTest(testDispatcher) { |
| 112 | + viewModel = MyPageViewModel(userRepository) |
| 113 | + |
| 114 | + viewModel.state.test { |
| 115 | + assertEquals(MyPageUiState(), awaitItem()) |
| 116 | + |
| 117 | + viewModel.intent(MyPageIntent.UpdateNickname("새닉네임")) |
| 118 | + |
| 119 | + assertEquals("새닉네임", awaitItem().nickname) |
| 120 | + |
| 121 | + cancelAndIgnoreRemainingEvents() |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments