Skip to content

Commit 4ca38af

Browse files
committed
up
1 parent 87f8e18 commit 4ca38af

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

feature-main/src/test/java/com/hoc/flowmvi/ui/main/MainVMTest.kt

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import com.hoc.flowmvi.domain.entity.User
55
import com.hoc.flowmvi.domain.usecase.GetUsersUseCase
66
import com.hoc.flowmvi.domain.usecase.RefreshGetUsersUseCase
77
import com.hoc.flowmvi.domain.usecase.RemoveUserUseCase
8+
import io.mockk.coEvery
9+
import io.mockk.coVerify
810
import io.mockk.every
911
import io.mockk.mockk
1012
import io.mockk.verify
11-
import java.io.IOException
12-
import kotlin.test.Test
13-
import kotlin.time.ExperimentalTime
1413
import kotlinx.coroutines.ExperimentalCoroutinesApi
1514
import kotlinx.coroutines.FlowPreview
1615
import kotlinx.coroutines.flow.flow
1716
import kotlinx.coroutines.flow.flowOf
17+
import java.io.IOException
18+
import kotlin.test.Test
19+
import kotlin.time.ExperimentalTime
1820

1921
private val USERS = listOf(
2022
User(
@@ -73,7 +75,7 @@ class MainVMTest : BaseMviViewModelTest<
7375
}
7476

7577
@Test
76-
fun `ViewIntent_Initial returns success`() = test(
78+
fun test_withInitialIntentWhenSuccess_returnsUserItems() = test(
7779
vmProducer = {
7880
every { getUserUseCase() } returns flowOf(USERS)
7981
vm
@@ -92,7 +94,7 @@ class MainVMTest : BaseMviViewModelTest<
9294
) { verify(exactly = 1) { getUserUseCase() } }
9395

9496
@Test
95-
fun `ViewIntent_Initial returns failure`() {
97+
fun test_withInitialIntentWhenError_returnsErrorState() {
9698
val ioException = IOException()
9799

98100
test(
@@ -119,12 +121,76 @@ class MainVMTest : BaseMviViewModelTest<
119121
}
120122

121123
@Test
122-
fun `ViewIntent_Refresh returns success`() {
124+
fun test_withRefreshIntentWhenSuccess_isNotRefreshing() {
123125
test(
124-
vmProducer = { vm },
126+
vmProducer = {
127+
every { getUserUseCase() } returns flowOf(USERS)
128+
coEvery { refreshGetUsersUseCase() } returns Unit
129+
vm
130+
},
131+
intentsBeforeCollecting = flowOf(ViewIntent.Initial),
125132
intents = flowOf(ViewIntent.Refresh),
126-
expectedStates = listOf(ViewState.initial()),
127-
expectedEvents = emptyList(),
128-
)
133+
expectedStates = listOf(
134+
ViewState(
135+
userItems = USER_ITEMS,
136+
isLoading = false,
137+
error = null,
138+
isRefreshing = false
139+
),
140+
ViewState(
141+
userItems = USER_ITEMS,
142+
isLoading = false,
143+
error = null,
144+
isRefreshing = true,
145+
),
146+
ViewState(
147+
userItems = USER_ITEMS,
148+
isLoading = false,
149+
error = null,
150+
isRefreshing = false
151+
),
152+
),
153+
expectedEvents = listOf(
154+
SingleEvent.Refresh.Success
155+
),
156+
) { coVerify(exactly = 1) { refreshGetUsersUseCase() } }
157+
}
158+
159+
@Test
160+
fun test_withRefreshIntentWhenFailure_isNotRefreshing() {
161+
val ioException = IOException()
162+
163+
test(
164+
vmProducer = {
165+
coEvery { getUserUseCase() } returns flowOf(USERS)
166+
coEvery { refreshGetUsersUseCase() } throws ioException
167+
vm
168+
},
169+
intentsBeforeCollecting = flowOf(ViewIntent.Initial),
170+
intents = flowOf(ViewIntent.Refresh),
171+
expectedStates = listOf(
172+
ViewState(
173+
userItems = USER_ITEMS,
174+
isLoading = false,
175+
error = null,
176+
isRefreshing = false
177+
),
178+
ViewState(
179+
userItems = USER_ITEMS,
180+
isLoading = false,
181+
error = null,
182+
isRefreshing = true,
183+
),
184+
ViewState(
185+
userItems = USER_ITEMS,
186+
isLoading = false,
187+
error = null,
188+
isRefreshing = false
189+
),
190+
),
191+
expectedEvents = listOf(
192+
SingleEvent.Refresh.Failure(ioException)
193+
),
194+
) { coVerify(exactly = 1) { refreshGetUsersUseCase() } }
129195
}
130196
}

mvi/mvi-testing/src/main/java/com/flowmvi/mvi_testing/BaseMviViewModelTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ abstract class BaseMviViewModelTest<
4747
expectedStates: List<S>,
4848
expectedEvents: List<E>,
4949
delayAfterDispatchingIntents: Duration = Duration.milliseconds(50),
50+
intentsBeforeCollecting: Flow<I>? = null,
5051
otherAssertions: (suspend () -> Unit)? = null,
5152
) = testDispatcher.runBlockingTest {
5253
val vm = vmProducer()
54+
intentsBeforeCollecting?.collect { vm.processIntent(it) }
55+
5356
val states = mutableListOf<S>()
5457
val events = mutableListOf<E>()
5558

@@ -59,6 +62,9 @@ abstract class BaseMviViewModelTest<
5962
intents.collect { vm.processIntent(it) }
6063
delay(delayAfterDispatchingIntents)
6164

65+
println(states)
66+
println(events)
67+
6268
assertEquals(expectedStates.size, states.size)
6369
assertContentEquals(
6470
expectedStates,

0 commit comments

Comments
 (0)