Skip to content

Commit c6b8bac

Browse files
committed
update tests
1 parent 19f852a commit c6b8bac

File tree

3 files changed

+104
-31
lines changed

3 files changed

+104
-31
lines changed

feature-add/src/test/java/com/hoc/flowmvi/ui/add/AddVMTest.kt

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
package com.hoc.flowmvi.ui.add
22

33
import androidx.lifecycle.SavedStateHandle
4+
import arrow.core.right
45
import com.flowmvi.mvi_testing.BaseMviViewModelTest
56
import com.flowmvi.mvi_testing.mapRight
7+
import com.hoc.flowmvi.domain.entity.User
68
import com.hoc.flowmvi.domain.usecase.AddUserUseCase
79
import com.hoc.flowmvi.ui.add.ValidationError.TOO_SHORT_FIRST_NAME
810
import com.hoc.flowmvi.ui.add.ValidationError.TOO_SHORT_LAST_NAME
911
import com.hoc.flowmvi.ui.add.ValidationError.values
12+
import io.mockk.coEvery
13+
import io.mockk.coVerify
1014
import io.mockk.confirmVerified
1115
import io.mockk.mockk
1216
import kotlinx.coroutines.ExperimentalCoroutinesApi
1317
import kotlinx.coroutines.flow.flowOf
1418
import kotlin.test.Test
19+
import kotlin.time.Duration
1520
import kotlin.time.ExperimentalTime
1621

1722
private val ALL_ERRORS = values().toSet()
23+
private const val EMAIL = "hoc081098@gmail.com"
24+
private const val NAME = "hoc081098"
1825

1926
@ExperimentalCoroutinesApi
2027
@ExperimentalTime
@@ -144,9 +151,9 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
144151
ViewIntent.FirstNameChanged(""),
145152
ViewIntent.LastNameChanged(""),
146153
// all fields changed
147-
ViewIntent.EmailChanged("hoc081098@gmail.com"),
148-
ViewIntent.FirstNameChanged("hoc081098"),
149-
ViewIntent.LastNameChanged("hoc081098"),
154+
ViewIntent.EmailChanged(EMAIL),
155+
ViewIntent.FirstNameChanged(NAME),
156+
ViewIntent.LastNameChanged(NAME),
150157
),
151158
expectedStates = listOf(
152159
ViewState.initial(),
@@ -197,7 +204,7 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
197204
emailChanged = false,
198205
firstNameChanged = false,
199206
lastNameChanged = false,
200-
email = "hoc081098@gmail.com",
207+
email = EMAIL,
201208
firstName = "",
202209
lastName = ""
203210
),
@@ -207,7 +214,7 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
207214
emailChanged = false,
208215
firstNameChanged = false,
209216
lastNameChanged = false,
210-
email = "hoc081098@gmail.com",
217+
email = EMAIL,
211218
firstName = "",
212219
lastName = ""
213220
),
@@ -217,8 +224,8 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
217224
emailChanged = false,
218225
firstNameChanged = false,
219226
lastNameChanged = false,
220-
email = "hoc081098@gmail.com",
221-
firstName = "hoc081098",
227+
email = EMAIL,
228+
firstName = NAME,
222229
lastName = ""
223230
),
224231
ViewState(
@@ -227,8 +234,8 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
227234
emailChanged = false,
228235
firstNameChanged = false,
229236
lastNameChanged = false,
230-
email = "hoc081098@gmail.com",
231-
firstName = "hoc081098",
237+
email = EMAIL,
238+
firstName = NAME,
232239
lastName = ""
233240
),
234241
ViewState(
@@ -237,9 +244,9 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
237244
emailChanged = false,
238245
firstNameChanged = false,
239246
lastNameChanged = false,
240-
email = "hoc081098@gmail.com",
241-
firstName = "hoc081098",
242-
lastName = "hoc081098"
247+
email = EMAIL,
248+
firstName = NAME,
249+
lastName = NAME
243250
),
244251
// valid state
245252
ViewState(
@@ -248,12 +255,73 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
248255
emailChanged = false,
249256
firstNameChanged = false,
250257
lastNameChanged = false,
251-
email = "hoc081098@gmail.com",
252-
firstName = "hoc081098",
253-
lastName = "hoc081098"
258+
email = EMAIL,
259+
firstName = NAME,
260+
lastName = NAME
254261
),
255262
).mapRight(),
256263
expectedEvents = emptyList(),
257264
)
258265
}
266+
267+
@Test
268+
fun test_withSubmitIntentFormValid_callAddUser() {
269+
val user = User(
270+
id = "",
271+
email = EMAIL,
272+
firstName = NAME,
273+
lastName = NAME,
274+
avatar = ""
275+
)
276+
277+
coEvery { addUser(user) } returns Unit.right()
278+
279+
test(
280+
vmProducer = { vm },
281+
intents = flowOf(ViewIntent.Submit),
282+
intentsBeforeCollecting = flowOf(
283+
ViewIntent.EmailChanged(EMAIL),
284+
ViewIntent.FirstNameChanged(NAME),
285+
ViewIntent.LastNameChanged(NAME),
286+
),
287+
expectedStates = listOf(
288+
ViewState(
289+
errors = emptySet(),
290+
isLoading = false,
291+
emailChanged = false,
292+
firstNameChanged = false,
293+
lastNameChanged = false,
294+
email = EMAIL,
295+
firstName = NAME,
296+
lastName = NAME,
297+
),
298+
ViewState(
299+
errors = emptySet(),
300+
isLoading = true,
301+
emailChanged = false,
302+
firstNameChanged = false,
303+
lastNameChanged = false,
304+
email = EMAIL,
305+
firstName = NAME,
306+
lastName = NAME,
307+
),
308+
ViewState(
309+
errors = emptySet(),
310+
isLoading = false,
311+
emailChanged = false,
312+
firstNameChanged = false,
313+
lastNameChanged = false,
314+
email = EMAIL,
315+
firstName = NAME,
316+
lastName = NAME,
317+
),
318+
).mapRight(),
319+
expectedEvents = listOf(
320+
SingleEvent.AddUserSuccess(user),
321+
).mapRight(),
322+
delayAfterDispatchingIntents = Duration.seconds(1),
323+
) {
324+
coVerify { addUser(user) }
325+
}
326+
}
259327
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ class MainVMTest : BaseMviViewModelTest<
224224
isRefreshing = false,
225225
)
226226
).mapRight(),
227-
expectedEvents = listOf(
228-
SingleEvent.GetUsersError(userError),
229-
).mapRight(),
227+
expectedEvents = emptyList(),
230228
delayAfterDispatchingIntents = Duration.milliseconds(100),
231229
) {
232230
coVerify(exactly = 1) { getUserUseCase() }
@@ -282,9 +280,7 @@ class MainVMTest : BaseMviViewModelTest<
282280
isRefreshing = false,
283281
)
284282
).mapRight(),
285-
expectedEvents = listOf(
286-
SingleEvent.GetUsersError(userError),
287-
).mapRight(),
283+
expectedEvents = emptyList(),
288284
) { verify(exactly = 2) { getUserUseCase() } }
289285
}
290286

@@ -324,7 +320,6 @@ class MainVMTest : BaseMviViewModelTest<
324320
)
325321
).mapRight(),
326322
expectedEvents = listOf(
327-
SingleEvent.GetUsersError(userError1),
328323
SingleEvent.GetUsersError(userError2),
329324
).mapRight(),
330325
) { verify(exactly = 2) { getUserUseCase() } }

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
1414
import kotlinx.coroutines.delay
1515
import kotlinx.coroutines.flow.Flow
1616
import kotlinx.coroutines.flow.collect
17+
import kotlinx.coroutines.flow.launchIn
1718
import kotlinx.coroutines.flow.onCompletion
1819
import kotlinx.coroutines.flow.onEach
1920
import kotlinx.coroutines.flow.toList
@@ -63,19 +64,28 @@ abstract class BaseMviViewModelTest<
6364
fun logIfEnabled(s: () -> String) = if (logging) println(s()) else Unit
6465

6566
val vm = vmProducer()
66-
intentsBeforeCollecting
67-
?.onCompletion { logIfEnabled { "-".repeat(32) } }
68-
?.collect {
69-
vm.processIntent(it)
70-
logIfEnabled { "[BEFORE] Dispatch $it -> $vm" }
71-
}
67+
intentsBeforeCollecting?.let { flow ->
68+
val job = vm.singleEvent.launchIn(this) // ignore events
69+
70+
flow
71+
.onCompletion {
72+
job.cancel()
73+
logIfEnabled { "-".repeat(32) }
74+
}
75+
.collect {
76+
vm.processIntent(it)
77+
logIfEnabled { "[BEFORE] Dispatch $it -> $vm" }
78+
}
79+
}
7280

7381
logIfEnabled { "[START] $vm" }
7482

7583
val states = mutableListOf<S>()
7684
val events = mutableListOf<E>()
7785

78-
val stateJob = launch(start = CoroutineStart.UNDISPATCHED) { vm.viewState.onEach { logIfEnabled { "[STATE] <- $it" } }.toList(states) }
86+
val stateJob = launch(start = CoroutineStart.UNDISPATCHED) {
87+
vm.viewState.onEach { logIfEnabled { "[STATE] <- $it" } }.toList(states)
88+
}
7989
val eventJob = launch(start = CoroutineStart.UNDISPATCHED) { vm.singleEvent.toList(events) }
8090

8191
intents.collect {
@@ -84,6 +94,8 @@ abstract class BaseMviViewModelTest<
8494
}
8595
delay(delayAfterDispatchingIntents)
8696
logIfEnabled { "-".repeat(32) }
97+
stateJob.cancel()
98+
eventJob.cancel()
8799

88100
logIfEnabled { "[DONE] states=${states.joinToStringWithIndex()}" }
89101
logIfEnabled { "[DONE] events=${events.joinToStringWithIndex()}" }
@@ -119,8 +131,6 @@ abstract class BaseMviViewModelTest<
119131
}
120132

121133
otherAssertions?.invoke()
122-
stateJob.cancel()
123-
eventJob.cancel()
124134
}
125135
}
126136

0 commit comments

Comments
 (0)