mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 02:15:35 +03:00
catching username availabilty exceptions and handling as user facing error
This commit is contained in:
parent
825ba77bb2
commit
a4ea47e740
3 changed files with 43 additions and 18 deletions
|
@ -193,25 +193,32 @@ class OnboardingViewModel @AssistedInject constructor(
|
|||
}
|
||||
|
||||
private suspend fun checkUserNameAvailability(userName: String) {
|
||||
when (val result = registrationWizard.registrationAvailable(userName)) {
|
||||
RegistrationAvailability.Available -> {
|
||||
setState {
|
||||
copy(
|
||||
registrationState = RegistrationState(
|
||||
isUserNameAvailable = true,
|
||||
selectedMatrixId = when {
|
||||
userName.isMatrixId() -> userName
|
||||
else -> "@$userName:${selectedHomeserver.userFacingUrl.toReducedUrl()}"
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
runCatching { registrationWizard.registrationAvailable(userName) }.fold(
|
||||
onSuccess = { result ->
|
||||
when (result) {
|
||||
RegistrationAvailability.Available -> {
|
||||
setState {
|
||||
copy(
|
||||
registrationState = RegistrationState(
|
||||
isUserNameAvailable = true,
|
||||
selectedMatrixId = when {
|
||||
userName.isMatrixId() -> userName
|
||||
else -> "@$userName:${selectedHomeserver.userFacingUrl.toReducedUrl()}"
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is RegistrationAvailability.NotAvailable -> {
|
||||
_viewEvents.post(OnboardingViewEvents.Failure(result.failure))
|
||||
}
|
||||
}
|
||||
is RegistrationAvailability.NotAvailable -> {
|
||||
_viewEvents.post(OnboardingViewEvents.Failure(result.failure))
|
||||
}
|
||||
}
|
||||
},
|
||||
onFailure = {
|
||||
_viewEvents.post(OnboardingViewEvents.Failure(it))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun withAction(action: OnboardingAction, block: (OnboardingAction) -> Unit) {
|
||||
|
|
|
@ -390,6 +390,20 @@ class OnboardingViewModelTest {
|
|||
.finish()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given available username throws, when a register username is entered, then emits error`() = runTest {
|
||||
viewModelWith(initialRegistrationState(A_HOMESERVER_URL))
|
||||
fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also { it.givenUserNameIsAvailableThrows(A_USERNAME, AN_ERROR) })
|
||||
val test = viewModel.test()
|
||||
|
||||
viewModel.handle(OnboardingAction.UserNameEnteredAction.Registration(A_USERNAME))
|
||||
|
||||
test
|
||||
.assertStates(initialState)
|
||||
.assertEvents(OnboardingViewEvents.Failure(AN_ERROR))
|
||||
.finish()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given available username, when a register username is entered, then emits available registration state`() = runTest {
|
||||
viewModelWith(initialRegistrationState(A_HOMESERVER_URL))
|
||||
|
|
|
@ -57,6 +57,10 @@ class FakeRegistrationWizard : RegistrationWizard by mockk(relaxed = false) {
|
|||
coEvery { registrationAvailable(userName) } returns RegistrationAvailability.Available
|
||||
}
|
||||
|
||||
fun givenUserNameIsAvailableThrows(userName: String, cause: Throwable) {
|
||||
coEvery { registrationAvailable(userName) } throws cause
|
||||
}
|
||||
|
||||
fun givenUserNameIsUnavailable(userName: String, failure: Failure.ServerError) {
|
||||
coEvery { registrationAvailable(userName) } returns RegistrationAvailability.NotAvailable(failure)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue