From 9d5298e9db6524775c047f5edd3838806b0a969f Mon Sep 17 00:00:00 2001 From: yamilmedina Date: Fri, 24 Jan 2025 11:36:11 +0100 Subject: [PATCH] feat: viewmodel tests for main function --- .../login/NewLoginViewModelTest.kt | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 app/src/test/kotlin/com/wire/android/ui/newauthentication/login/NewLoginViewModelTest.kt diff --git a/app/src/test/kotlin/com/wire/android/ui/newauthentication/login/NewLoginViewModelTest.kt b/app/src/test/kotlin/com/wire/android/ui/newauthentication/login/NewLoginViewModelTest.kt new file mode 100644 index 0000000000..70c7dcfbb3 --- /dev/null +++ b/app/src/test/kotlin/com/wire/android/ui/newauthentication/login/NewLoginViewModelTest.kt @@ -0,0 +1,78 @@ +package com.wire.android.ui.newauthentication.login + +import androidx.lifecycle.SavedStateHandle +import com.wire.android.config.CoroutineTestExtension +import com.wire.android.di.AuthServerConfigProvider +import com.wire.android.ui.authentication.login.LoginState +import com.wire.android.util.newServerConfig +import io.mockk.MockKAnnotations +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@OptIn(ExperimentalCoroutinesApi::class) +@ExtendWith(CoroutineTestExtension::class) +class NewLoginViewModelTest { + + @Test + fun `given onLoginStarted is called, when valid input, then proceed`() = runTest { + val (arrangement, sut) = Arrangement() + .withEmailOrSSOCodeValidatorReturning(true) + .arrange() + + sut.onLoginStarted(onSuccess = arrangement.onSuccess) + + assertEquals(LoginState.Loading, sut.loginState.flowState) + advanceUntilIdle() + + verify { arrangement.onSuccess() } + assertEquals(LoginState.Default, sut.loginState.flowState) + } + + @Test + fun `given onLoginStarted is called, when invalid input, then update error state`() = runTest { + val (arrangement, sut) = Arrangement() + .withEmailOrSSOCodeValidatorReturning(false) + .arrange() + + sut.onLoginStarted(onSuccess = arrangement.onSuccess) + + assertEquals(LoginState.Error.TextFieldError.InvalidValue, sut.loginState.flowState) + verify(exactly = 0) { arrangement.onSuccess() } + } + + private class Arrangement { + @MockK + private lateinit var savedStateHandle: SavedStateHandle + val authServerConfigProvider: AuthServerConfigProvider = mockk() + val emailOrSSOCodeValidator: EmailOrSSOCodeValidator = mockk() + + val onSuccess: () -> Unit = mockk() + + init { + MockKAnnotations.init(this, relaxUnitFun = true) + every { authServerConfigProvider.authServer } returns MutableStateFlow((newServerConfig(1).links)) + every { savedStateHandle.get(any()) } returns null + every { savedStateHandle.set(any(), any()) } returns Unit + every { onSuccess() } returns Unit + } + + fun withEmailOrSSOCodeValidatorReturning(result: Boolean = true) = apply { + every { emailOrSSOCodeValidator.validate(any()) } returns result + } + + fun arrange() = this to NewLoginViewModel( + authServerConfigProvider, + emailOrSSOCodeValidator, + savedStateHandle + ) + } +}