diff --git a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt index d6cfcac96f..1bc1b5bbf4 100644 --- a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt @@ -22,7 +22,9 @@ import im.vector.app.R import im.vector.app.features.login.LoginConfig import im.vector.app.features.login.LoginMode import im.vector.app.features.login.ReAuthHelper +import im.vector.app.features.login.ServerType import im.vector.app.features.login.SignMode +import im.vector.app.features.onboarding.RegistrationStateFixture.aRegistrationState import im.vector.app.features.onboarding.StartAuthenticationFlowUseCase.StartAuthenticationResult import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeAnalyticsTracker @@ -78,6 +80,7 @@ private const val A_USERNAME = "hello-world" private const val A_DEVICE_NAME = "a-device-name" private const val A_MATRIX_ID = "@$A_USERNAME:matrix.org" private const val A_LOGIN_TOKEN = "a-login-token" +private val A_REGISTRATION_STATE = aRegistrationState(email = AN_EMAIL) class OnboardingViewModelTest { @@ -782,6 +785,107 @@ class OnboardingViewModelTest { .finish() } + @Test + fun `given homeserver state, when resetting homeserver url, then resets auth service and state`() = runTest { + viewModelWith(initialState.copy(isLoading = true, selectedHomeserver = SELECTED_HOMESERVER_STATE)) + val test = viewModel.test() + fakeAuthenticationService.expectReset() + + viewModel.handle(OnboardingAction.ResetHomeServerUrl) + + test + .assertStatesChanges( + initialState, + { copy(isLoading = false, selectedHomeserver = SelectedHomeserverState()) }, + ) + .assertNoEvents() + .finish() + fakeAuthenticationService.verifyReset() + } + + @Test + fun `given server type, when resetting homeserver type, then resets state`() = runTest { + viewModelWith(initialState.copy(serverType = ServerType.EMS)) + val test = viewModel.test() + + viewModel.handle(OnboardingAction.ResetHomeServerType) + + test + .assertStatesChanges( + initialState, + { copy(serverType = ServerType.Unknown) }, + ) + .assertNoEvents() + .finish() + } + + @Test + fun `given sign mode, when resetting sign mode, then resets state`() = runTest { + viewModelWith(initialState.copy(isLoading = true, signMode = SignMode.SignIn)) + val test = viewModel.test() + + viewModel.handle(OnboardingAction.ResetSignMode) + + test + .assertStatesChanges( + initialState, + { copy(isLoading = false, signMode = SignMode.Unknown) }, + ) + .assertNoEvents() + .finish() + } + + @Test + fun `given registration state, when resetting authentication attempt, then cancels pending logic or registration and resets state`() = runTest { + viewModelWith(initialState.copy(isLoading = true, registrationState = A_REGISTRATION_STATE)) + val test = viewModel.test() + fakeAuthenticationService.expectedCancelsPendingLogin() + + viewModel.handle(OnboardingAction.ResetAuthenticationAttempt) + + test + .assertStatesChanges( + initialState, + { copy(isLoading = false, registrationState = RegistrationState()) }, + ) + .assertNoEvents() + .finish() + fakeAuthenticationService.verifyCancelsPendingLogin() + } + + + @Test + fun `given reset state, when resetting reset state, then resets state`() = runTest { + viewModelWith(initialState.copy(isLoading = true, resetState = ResetState(AN_EMAIL))) + val test = viewModel.test() + + viewModel.handle(OnboardingAction.ResetResetPassword) + + test + .assertStatesChanges( + initialState, + { copy(isLoading = false, resetState = ResetState()) }, + ) + .assertNoEvents() + .finish() + } + + @Test + fun `given registration state, when resetting user name, then resets state`() = runTest { + viewModelWith(initialState.copy(registrationState = A_REGISTRATION_STATE)) + val test = viewModel.test() + + viewModel.handle(OnboardingAction.ResetSelectedRegistrationUserName) + + test + .assertStatesChanges( + initialState, + { copy(registrationState = RegistrationState()) }, + ) + .assertNoEvents() + .finish() + } + private fun viewModelWith(state: OnboardingViewState) { OnboardingViewModel( state, diff --git a/vector/src/test/java/im/vector/app/features/onboarding/RegistrationStateFixture.kt b/vector/src/test/java/im/vector/app/features/onboarding/RegistrationStateFixture.kt new file mode 100644 index 0000000000..bb7711a5bc --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/onboarding/RegistrationStateFixture.kt @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.onboarding + +object RegistrationStateFixture { + + fun aRegistrationState( + email: String? = null, + isUserNameAvailable: Boolean = false, + selectedMatrixId: String? = null, + ) = RegistrationState(email, isUserNameAvailable, selectedMatrixId) +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeAuthenticationService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeAuthenticationService.kt index cc606497f5..0075fb29b1 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeAuthenticationService.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeAuthenticationService.kt @@ -18,6 +18,7 @@ package im.vector.app.test.fakes import io.mockk.coEvery import io.mockk.coJustRun +import io.mockk.coVerify import io.mockk.every import io.mockk.mockk import org.matrix.android.sdk.api.auth.AuthenticationService @@ -68,4 +69,12 @@ class FakeAuthenticationService : AuthenticationService by mockk() { fun givenDirectAuthenticationThrows(config: HomeServerConnectionConfig, matrixId: String, password: String, deviceName: String, cause: Throwable) { coEvery { directAuthentication(config, matrixId, password, deviceName) } throws cause } + + fun verifyReset() { + coVerify { reset() } + } + + fun verifyCancelsPendingLogin() { + coVerify { cancelPendingLoginOrRegistration() } + } }