11package com.hoc.flowmvi.ui.add
22
33import androidx.lifecycle.SavedStateHandle
4+ import arrow.core.left
45import arrow.core.right
56import com.flowmvi.mvi_testing.BaseMviViewModelTest
67import com.flowmvi.mvi_testing.mapRight
78import com.hoc.flowmvi.domain.entity.User
9+ import com.hoc.flowmvi.domain.repository.UserError
810import com.hoc.flowmvi.domain.usecase.AddUserUseCase
911import com.hoc.flowmvi.ui.add.ValidationError.TOO_SHORT_FIRST_NAME
1012import com.hoc.flowmvi.ui.add.ValidationError.TOO_SHORT_LAST_NAME
@@ -265,7 +267,7 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
265267 }
266268
267269 @Test
268- fun test_withSubmitIntentWhenFormValid_callAddUserAndReturnsStateWithLoading () {
270+ fun test_withSubmitIntentWhenFormValidAndAddUserSuccess_callAddUserAndReturnsStateWithLoading () {
269271 val user = User (
270272 id = " " ,
271273 email = EMAIL ,
@@ -325,6 +327,68 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
325327 }
326328 }
327329
330+ @Test
331+ fun test_withSubmitIntentWhenFormValidAndAddUserFailure_callAddUserAndReturnsStateWithLoading () {
332+ val user = User (
333+ id = " " ,
334+ email = EMAIL ,
335+ firstName = NAME ,
336+ lastName = NAME ,
337+ avatar = " "
338+ )
339+ val networkError = UserError .NetworkError
340+
341+ coEvery { addUser(user) } returns networkError.left()
342+
343+ test(
344+ vmProducer = { vm },
345+ intents = flowOf(ViewIntent .Submit ),
346+ intentsBeforeCollecting = flowOf(
347+ ViewIntent .EmailChanged (EMAIL ),
348+ ViewIntent .FirstNameChanged (NAME ),
349+ ViewIntent .LastNameChanged (NAME ),
350+ ),
351+ expectedStates = listOf (
352+ ViewState (
353+ errors = emptySet(),
354+ isLoading = false ,
355+ emailChanged = false ,
356+ firstNameChanged = false ,
357+ lastNameChanged = false ,
358+ email = EMAIL ,
359+ firstName = NAME ,
360+ lastName = NAME ,
361+ ),
362+ ViewState (
363+ errors = emptySet(),
364+ isLoading = true ,
365+ emailChanged = false ,
366+ firstNameChanged = false ,
367+ lastNameChanged = false ,
368+ email = EMAIL ,
369+ firstName = NAME ,
370+ lastName = NAME ,
371+ ),
372+ ViewState (
373+ errors = emptySet(),
374+ isLoading = false ,
375+ emailChanged = false ,
376+ firstNameChanged = false ,
377+ lastNameChanged = false ,
378+ email = EMAIL ,
379+ firstName = NAME ,
380+ lastName = NAME ,
381+ ),
382+ ).mapRight(),
383+ expectedEvents = listOf (
384+ SingleEvent .AddUserFailure (user = user, error = networkError),
385+ ).mapRight(),
386+ delayAfterDispatchingIntents = Duration .seconds(1 ),
387+ ) {
388+ coVerify { addUser(user) }
389+ }
390+ }
391+
328392 @Test
329393 fun test_withSubmitIntentWhenFormInvalid_doNothing () {
330394 test(
@@ -351,4 +415,50 @@ class AddVMTest : BaseMviViewModelTest<ViewIntent, ViewState, SingleEvent, AddVM
351415 delayAfterDispatchingIntents = Duration .seconds(1 ),
352416 )
353417 }
418+
419+ @Test
420+ fun test_withFirstChangeIntents_returnsStateWithFirstChangesSetToTrue () {
421+ test(
422+ vmProducer = { vm },
423+ intents = flowOf(
424+ ViewIntent .EmailChangedFirstTime ,
425+ ViewIntent .FirstNameChangedFirstTime ,
426+ ViewIntent .LastNameChangedFirstTime ,
427+ ),
428+ expectedStates = listOf (
429+ ViewState .initial(),
430+ ViewState (
431+ errors = setOf (),
432+ isLoading = false ,
433+ emailChanged = true ,
434+ firstNameChanged = false ,
435+ lastNameChanged = false ,
436+ email = null ,
437+ firstName = null ,
438+ lastName = null
439+ ),
440+ ViewState (
441+ errors = setOf (),
442+ isLoading = false ,
443+ emailChanged = true ,
444+ firstNameChanged = true ,
445+ lastNameChanged = false ,
446+ email = null ,
447+ firstName = null ,
448+ lastName = null
449+ ),
450+ ViewState (
451+ errors = setOf (),
452+ isLoading = false ,
453+ emailChanged = true ,
454+ firstNameChanged = true ,
455+ lastNameChanged = true ,
456+ email = null ,
457+ firstName = null ,
458+ lastName = null
459+ )
460+ ).mapRight(),
461+ expectedEvents = emptyList()
462+ )
463+ }
354464}
0 commit comments