Skip to content

Commit 76f10a7

Browse files
committed
add tests
1 parent 0f0b254 commit 76f10a7

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import com.hoc.flowmvi.domain.usecase.RefreshGetUsersUseCase
77
import com.hoc.flowmvi.domain.usecase.RemoveUserUseCase
88
import io.mockk.coEvery
99
import io.mockk.coVerify
10+
import io.mockk.coVerifySequence
1011
import io.mockk.every
1112
import io.mockk.mockk
1213
import io.mockk.verify
1314
import kotlinx.coroutines.ExperimentalCoroutinesApi
1415
import kotlinx.coroutines.FlowPreview
16+
import kotlinx.coroutines.flow.MutableStateFlow
1517
import kotlinx.coroutines.flow.emptyFlow
1618
import kotlinx.coroutines.flow.flow
1719
import kotlinx.coroutines.flow.flowOf
20+
import kotlinx.coroutines.flow.update
1821
import java.io.IOException
1922
import kotlin.test.Test
2023
import kotlin.time.Duration
@@ -327,4 +330,92 @@ class MainVMTest : BaseMviViewModelTest<
327330
),
328331
) { verify(exactly = 2) { getUserUseCase() } }
329332
}
333+
334+
@Test
335+
fun test_withRemoveUserIntentWhenSuccess_returnsUserItemsExceptRemovedUserItems() {
336+
val user1 = USERS[0]
337+
val user2 = USERS[1]
338+
val item1 = USER_ITEMS[0]
339+
val item2 = USER_ITEMS[1]
340+
val usersFlow = MutableStateFlow(USERS)
341+
342+
test(
343+
vmProducer = {
344+
every { getUserUseCase() } returns usersFlow
345+
coEvery { removeUser(any()) } coAnswers {
346+
usersFlow.update { users ->
347+
users.filter { it.id != firstArg<User>().id }
348+
}
349+
}
350+
vm
351+
},
352+
intentsBeforeCollecting = flowOf(ViewIntent.Initial),
353+
intents = flowOf(
354+
ViewIntent.RemoveUser(item1),
355+
ViewIntent.RemoveUser(item2),
356+
),
357+
expectedStates = listOf(
358+
ViewState(
359+
userItems = USER_ITEMS,
360+
isLoading = false,
361+
error = null,
362+
isRefreshing = false,
363+
),
364+
ViewState(
365+
userItems = USER_ITEMS.toMutableList().apply { remove(item1) },
366+
isLoading = false,
367+
error = null,
368+
isRefreshing = false,
369+
),
370+
ViewState(
371+
userItems = USER_ITEMS.toMutableList().apply {
372+
remove(item1)
373+
remove(item2)
374+
},
375+
isLoading = false,
376+
error = null,
377+
isRefreshing = false,
378+
),
379+
),
380+
expectedEvents = listOf(
381+
SingleEvent.RemoveUser.Success(item1),
382+
SingleEvent.RemoveUser.Success(item2),
383+
)
384+
) {
385+
coVerifySequence {
386+
removeUser(user1)
387+
removeUser(user2)
388+
}
389+
}
390+
}
391+
392+
@Test
393+
fun test_withRemoveUserIntentWhenError_stateDoNotChange() {
394+
val user = USERS[0]
395+
val item = USER_ITEMS[0]
396+
val ioException = IOException()
397+
398+
test(
399+
vmProducer = {
400+
every { getUserUseCase() } returns flowOf(USERS)
401+
coEvery { removeUser(any()) } throws ioException
402+
vm
403+
},
404+
intentsBeforeCollecting = flowOf(ViewIntent.Initial),
405+
intents = flowOf(ViewIntent.RemoveUser(item)),
406+
expectedStates = listOf(
407+
ViewState(
408+
userItems = USER_ITEMS,
409+
isLoading = false,
410+
error = null,
411+
isRefreshing = false,
412+
),
413+
),
414+
expectedEvents = listOf(
415+
SingleEvent.RemoveUser.Failure(item, ioException),
416+
)
417+
) {
418+
coVerify(exactly = 1) { removeUser(user) }
419+
}
420+
}
330421
}

0 commit comments

Comments
 (0)