Skip to content

Commit

Permalink
Add a register view model
Browse files Browse the repository at this point in the history
This is very similar to the login view model. This is intentional, because later on the register screen will also handle the user profile creation.
  • Loading branch information
octogradiste committed Mar 21, 2024
1 parent 8b5b581 commit 8a3d9d0
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.swent.echo.viewmodels.authentication

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.swent.echo.authentication.AuthenticationResult
import com.github.swent.echo.authentication.AuthenticationService
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

/**
* ViewModel for the register screen.
*
* @param auth The authentication service to use.
*/
class RegisterViewModel(private val auth: AuthenticationService) : ViewModel() {

private val _state = MutableStateFlow<AuthenticationState>(AuthenticationState.SignedOut)

/** The current state of the view model. */
val state = _state.asStateFlow()

/**
* Sign up with email and password.
*
* @param email The email of the user.
* @param password The password of the user.
*/
fun register(email: String, password: String) {
_state.value = AuthenticationState.SigningIn
viewModelScope.launch {
when (val result = auth.signUp(email, password)) {
is AuthenticationResult.Success -> _state.value = AuthenticationState.SignedIn
is AuthenticationResult.Error ->
_state.value = AuthenticationState.Error(result.message)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.github.swent.echo.viewmodels.authentication

import com.github.swent.echo.authentication.AuthenticationResult
import com.github.swent.echo.fakes.FakeAuthenticationService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class RegisterViewModelTest {
private lateinit var fakeAuthenticationService: FakeAuthenticationService
private lateinit var viewModel: RegisterViewModel

companion object {
private const val EMAIL = "test@email.com"
private const val PASSWORD = "password"
}

@Before
fun setUp() {
fakeAuthenticationService = FakeAuthenticationService()
viewModel = RegisterViewModel(fakeAuthenticationService)
}

@Test
fun `state should be signed out when created`() {
assertEquals(AuthenticationState.SignedOut, viewModel.state.value)
}

@Test
fun `login should return success when successful`() = runTest {
val testDispatcher = UnconfinedTestDispatcher(testScheduler)
Dispatchers.setMain(testDispatcher)

viewModel.register(EMAIL, PASSWORD)
assertEquals(AuthenticationState.SigningIn, viewModel.state.value)
fakeAuthenticationService.completeSigningOperation(AuthenticationResult.Success)
assertEquals(AuthenticationState.SignedIn, viewModel.state.value)
}

@Test
fun `login should return error when failed`() = runTest {
val testDispatcher = UnconfinedTestDispatcher(testScheduler)
Dispatchers.setMain(testDispatcher)

viewModel.register(EMAIL, PASSWORD)
assertEquals(AuthenticationState.SigningIn, viewModel.state.value)
fakeAuthenticationService.completeSigningOperation(
AuthenticationResult.Error("Error message")
)
assertEquals(AuthenticationState.Error("Error message"), viewModel.state.value)
}
}

0 comments on commit 8a3d9d0

Please sign in to comment.