allowing test withPrevious to be supplied a list

This commit is contained in:
Adam Brown 2022-03-02 18:07:13 +00:00
parent 804513c808
commit 390ae4344d
2 changed files with 9 additions and 9 deletions

View File

@ -196,7 +196,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.UpdateDisplayName(A_DISPLAY_NAME)) viewModel.handle(OnboardingAction.UpdateDisplayName(A_DISPLAY_NAME))
test test
.assertStates(expectedSuccessfulDisplayNameUpdateStates(personalisedInitialState)) .assertStatesWithPrevious(personalisedInitialState, expectedSuccessfulDisplayNameUpdateStates())
.assertEvents(OnboardingViewEvents.OnChooseProfilePicture) .assertEvents(OnboardingViewEvents.OnChooseProfilePicture)
.finish() .finish()
fakeSession.fakeProfileService.verifyUpdatedName(fakeSession.myUserId, A_DISPLAY_NAME) fakeSession.fakeProfileService.verifyUpdatedName(fakeSession.myUserId, A_DISPLAY_NAME)
@ -211,7 +211,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.UpdateDisplayName(A_DISPLAY_NAME)) viewModel.handle(OnboardingAction.UpdateDisplayName(A_DISPLAY_NAME))
test test
.assertStates(expectedSuccessfulDisplayNameUpdateStates(personalisedInitialState)) .assertStatesWithPrevious(personalisedInitialState, expectedSuccessfulDisplayNameUpdateStates())
.assertEvents(OnboardingViewEvents.OnPersonalizationComplete) .assertEvents(OnboardingViewEvents.OnPersonalizationComplete)
.finish() .finish()
fakeSession.fakeProfileService.verifyUpdatedName(fakeSession.myUserId, A_DISPLAY_NAME) fakeSession.fakeProfileService.verifyUpdatedName(fakeSession.myUserId, A_DISPLAY_NAME)
@ -339,14 +339,10 @@ class OnboardingViewModelTest {
state.copy(asyncProfilePicture = Fail(cause)) state.copy(asyncProfilePicture = Fail(cause))
) )
private fun expectedSuccessfulDisplayNameUpdateStates(personalisedInitialState: OnboardingViewState): List<OnboardingViewState> { private fun expectedSuccessfulDisplayNameUpdateStates(): List<OnboardingViewState.() -> OnboardingViewState> {
return listOf( return listOf(
personalisedInitialState, { copy(asyncDisplayName = Loading()) },
personalisedInitialState.copy(asyncDisplayName = Loading()), { copy(asyncDisplayName = Success(Unit), personalizationState = personalizationState.copy(displayName = A_DISPLAY_NAME)) }
personalisedInitialState.copy(
asyncDisplayName = Success(Unit),
personalizationState = personalisedInitialState.personalizationState.copy(displayName = A_DISPLAY_NAME)
)
) )
} }

View File

@ -56,6 +56,10 @@ class ViewModelTest<S, VE>(
} }
fun assertStatesWithPrevious(initial: S, vararg expected: S.() -> S): ViewModelTest<S, VE> { fun assertStatesWithPrevious(initial: S, vararg expected: S.() -> S): ViewModelTest<S, VE> {
return assertStatesWithPrevious(initial, expected.toList())
}
fun assertStatesWithPrevious(initial: S, expected: List<S.() -> S>): ViewModelTest<S, VE> {
val reducedExpectedStates = expected.fold(mutableListOf(initial)) { acc, curr -> val reducedExpectedStates = expected.fold(mutableListOf(initial)) { acc, curr ->
val next = curr.invoke(acc.last()) val next = curr.invoke(acc.last())
acc.add(next) acc.add(next)