updating with previous state helper and including javadoc to help explain its usage

This commit is contained in:
Adam Brown 2022-03-17 16:50:20 +00:00
parent d77061b229
commit 5df2ae9ae2
2 changed files with 15 additions and 11 deletions

View File

@ -126,7 +126,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.UpdateSignMode(SignMode.SignUp))
test
.assertStatesWithPrevious(
.assertStatesChanges(
initialState,
{ copy(signMode = SignMode.SignUp) },
{ copy(asyncRegistration = Loading()) },
@ -144,7 +144,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.PostRegisterAction(A_LOADABLE_REGISTER_ACTION))
test
.assertStatesWithPrevious(
.assertStatesChanges(
initialState,
{ copy(asyncRegistration = Loading()) },
{ copy(asyncRegistration = Uninitialized) }
@ -174,7 +174,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.PostRegisterAction(A_RESULT_IGNORED_REGISTER_ACTION))
test
.assertStatesWithPrevious(
.assertStatesChanges(
initialState,
{ copy(asyncRegistration = Loading()) },
{ copy(asyncRegistration = Uninitialized) }
@ -192,7 +192,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.PostRegisterAction(A_LOADABLE_REGISTER_ACTION))
test
.assertStatesWithPrevious(
.assertStatesChanges(
initialState,
{ copy(asyncRegistration = Loading()) },
{ copy(asyncLoginAction = Success(Unit), personalizationState = A_HOMESERVER_CAPABILITIES.toPersonalisationState()) },
@ -210,7 +210,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.PostRegisterAction(A_LOADABLE_REGISTER_ACTION))
test
.assertStatesWithPrevious(
.assertStatesChanges(
initialState,
{ copy(asyncRegistration = Loading()) },
{ copy(asyncLoginAction = Success(Unit), personalizationState = A_HOMESERVER_CAPABILITIES.toPersonalisationState()) },
@ -229,7 +229,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.UpdateDisplayName(A_DISPLAY_NAME))
test
.assertStatesWithPrevious(personalisedInitialState, expectedSuccessfulDisplayNameUpdateStates())
.assertStatesChanges(personalisedInitialState, expectedSuccessfulDisplayNameUpdateStates())
.assertEvents(OnboardingViewEvents.OnChooseProfilePicture)
.finish()
fakeSession.fakeProfileService.verifyUpdatedName(fakeSession.myUserId, A_DISPLAY_NAME)
@ -244,7 +244,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.UpdateDisplayName(A_DISPLAY_NAME))
test
.assertStatesWithPrevious(personalisedInitialState, expectedSuccessfulDisplayNameUpdateStates())
.assertStatesChanges(personalisedInitialState, expectedSuccessfulDisplayNameUpdateStates())
.assertEvents(OnboardingViewEvents.OnPersonalizationComplete)
.finish()
fakeSession.fakeProfileService.verifyUpdatedName(fakeSession.myUserId, A_DISPLAY_NAME)
@ -258,7 +258,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.UpdateDisplayName(A_DISPLAY_NAME))
test
.assertStatesWithPrevious(
.assertStatesChanges(
initialState,
{ copy(asyncDisplayName = Loading()) },
{ copy(asyncDisplayName = Fail(AN_ERROR)) },

View File

@ -55,11 +55,15 @@ class ViewModelTest<S, VE>(
return this
}
fun assertStatesWithPrevious(initial: S, vararg expected: S.() -> S): ViewModelTest<S, VE> {
return assertStatesWithPrevious(initial, expected.toList())
fun assertStatesChanges(initial: S, vararg expected: S.() -> S): ViewModelTest<S, VE> {
return assertStatesChanges(initial, expected.toList())
}
fun assertStatesWithPrevious(initial: S, expected: List<S.() -> S>): ViewModelTest<S, VE> {
/**
* Asserts the expected states are in the same order as the actual state emissions
* Each expected lambda is given the previous expected state, starting with the initial
*/
fun assertStatesChanges(initial: S, expected: List<S.() -> S>): ViewModelTest<S, VE> {
val reducedExpectedStates = expected.fold(mutableListOf(initial)) { acc, curr ->
val next = curr.invoke(acc.last())
acc.add(next)