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 bad37d82cd..a9bbb3eb07 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 @@ -102,6 +102,48 @@ class OnboardingViewModelTest { viewModelWith(initialState) } + @Test + fun `given registration started with currentThreePid, when handling InitWith, then emits restored session OnSendEmailSuccess`() = runTest { + val test = viewModel.test() + fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also { + it.givenRegistrationStarted(hasStarted = true) + it.givenCurrentThreePid(AN_EMAIL) + }) + + viewModel.handle(OnboardingAction.InitWith(LoginConfig(A_HOMESERVER_URL, identityServerUrl = null))) + + test + .assertEvents(OnboardingViewEvents.OnSendEmailSuccess(AN_EMAIL, isRestoredSession = true)) + .finish() + } + + @Test + fun `given registration not started, when handling InitWith, then does nothing`() = runTest { + val test = viewModel.test() + fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also { it.givenRegistrationStarted(hasStarted = false) }) + + viewModel.handle(OnboardingAction.InitWith(LoginConfig(A_HOMESERVER_URL, identityServerUrl = null))) + + test + .assertNoEvents() + .finish() + } + + @Test + fun `given registration started without currentThreePid, when handling InitWith, then does nothing`() = runTest { + val test = viewModel.test() + fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also { + it.givenRegistrationStarted(hasStarted = true) + it.givenCurrentThreePid(threePid = null) + }) + + viewModel.handle(OnboardingAction.InitWith(LoginConfig(A_HOMESERVER_URL, identityServerUrl = null))) + + test + .assertNoEvents() + .finish() + } + @Test fun `when handling PostViewEvent, then emits contents as view event`() = runTest { val test = viewModel.test() @@ -254,6 +296,24 @@ class OnboardingViewModelTest { .finish() } + @Test + fun `given register action returns email success, when handling action, then updates registration state and emits email success`() = runTest { + val test = viewModel.test() + givenRegistrationResultFor(A_LOADABLE_REGISTER_ACTION, RegistrationActionHandler.Result.SendEmailSuccess(AN_EMAIL)) + + viewModel.handle(OnboardingAction.PostRegisterAction(A_LOADABLE_REGISTER_ACTION)) + + test + .assertStatesChanges( + initialState, + { copy(isLoading = true) }, + { copy(registrationState = RegistrationState(email = AN_EMAIL)) }, + { copy(isLoading = false) } + ) + .assertEvents(OnboardingViewEvents.OnSendEmailSuccess(AN_EMAIL, isRestoredSession = false)) + .finish() + } + @Test fun `given unavailable deeplink, when selecting homeserver, then emits failure with default homeserver as retry action`() = runTest { fakeContext.givenHasConnection() diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt index e0b4586931..4f0b1fe083 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt @@ -45,6 +45,14 @@ class FakeRegistrationWizard : RegistrationWizard by mockk(relaxed = false) { } } + fun givenRegistrationStarted(hasStarted: Boolean) { + coEvery { isRegistrationStarted() } returns hasStarted + } + + fun givenCurrentThreePid(threePid: String?) { + coEvery { getCurrentThreePid() } returns threePid + } + fun givenUserNameIsAvailable(userName: String) { coEvery { registrationAvailable(userName) } returns RegistrationAvailability.Available }