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