-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
8b5b581
commit 8a3d9d0
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/github/swent/echo/viewmodels/authentication/RegisterViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
app/src/test/java/com/github/swent/echo/viewmodels/authentication/RegisterViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |