@@ -5,16 +5,18 @@ import com.hoc.flowmvi.domain.entity.User
5
5
import com.hoc.flowmvi.domain.usecase.GetUsersUseCase
6
6
import com.hoc.flowmvi.domain.usecase.RefreshGetUsersUseCase
7
7
import com.hoc.flowmvi.domain.usecase.RemoveUserUseCase
8
+ import io.mockk.coEvery
9
+ import io.mockk.coVerify
8
10
import io.mockk.every
9
11
import io.mockk.mockk
10
12
import io.mockk.verify
11
- import java.io.IOException
12
- import kotlin.test.Test
13
- import kotlin.time.ExperimentalTime
14
13
import kotlinx.coroutines.ExperimentalCoroutinesApi
15
14
import kotlinx.coroutines.FlowPreview
16
15
import kotlinx.coroutines.flow.flow
17
16
import kotlinx.coroutines.flow.flowOf
17
+ import java.io.IOException
18
+ import kotlin.test.Test
19
+ import kotlin.time.ExperimentalTime
18
20
19
21
private val USERS = listOf (
20
22
User (
@@ -73,7 +75,7 @@ class MainVMTest : BaseMviViewModelTest<
73
75
}
74
76
75
77
@Test
76
- fun `ViewIntent_Initial returns success` () = test(
78
+ fun test_withInitialIntentWhenSuccess_returnsUserItems () = test(
77
79
vmProducer = {
78
80
every { getUserUseCase() } returns flowOf(USERS )
79
81
vm
@@ -92,7 +94,7 @@ class MainVMTest : BaseMviViewModelTest<
92
94
) { verify(exactly = 1 ) { getUserUseCase() } }
93
95
94
96
@Test
95
- fun `ViewIntent_Initial returns failure` () {
97
+ fun test_withInitialIntentWhenError_returnsErrorState () {
96
98
val ioException = IOException ()
97
99
98
100
test(
@@ -119,12 +121,76 @@ class MainVMTest : BaseMviViewModelTest<
119
121
}
120
122
121
123
@Test
122
- fun `ViewIntent_Refresh returns success` () {
124
+ fun test_withRefreshIntentWhenSuccess_isNotRefreshing () {
123
125
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 ),
125
132
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() } }
129
195
}
130
196
}
0 commit comments