-
Notifications
You must be signed in to change notification settings - Fork 1
Add LoginViewModelTest #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| package com.example.mvvm.ui.login.ui | ||
|
|
||
| import android.util.Patterns | ||
| import androidx.annotation.VisibleForTesting | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| class LoginViewModel : ViewModel() { | ||
| private val _email = MutableStateFlow("") // crea un flujo mutable | ||
|
|
@@ -29,15 +32,14 @@ class LoginViewModel : ViewModel() { | |
|
|
||
| } | ||
|
|
||
| @VisibleForTesting fun isValidEmail(email: String): Boolean = Patterns.EMAIL_ADDRESS.matcher(email).matches() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| @VisibleForTesting fun isValidPassword(password: String): Boolean = password.length > 6 | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: you could make "6" here a constant, and then it would make unit testing whether |
||
|
|
||
| fun isValidEmail(email: String): Boolean = Patterns.EMAIL_ADDRESS.matcher(email).matches() | ||
| fun isValidPassword(password: String): Boolean = password.length > 6 | ||
|
|
||
| suspend fun pressButton() { | ||
| _loading.value = true | ||
| delay(4000) | ||
| _loading.value = false | ||
| fun pressButton() { | ||
| viewModelScope.launch { | ||
| _loading.value = true | ||
| delay(4000) | ||
| _loading.value = false | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.example.mvvm.ui.login.ui | ||
|
|
||
| import org.junit.Before | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class LoginViewModelTest { | ||
| private lateinit var viewModel: LoginViewModel | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| viewModel = LoginViewModel() | ||
| } | ||
|
|
||
| @Test | ||
| fun `email and password are empty on launch`() { | ||
| assert(viewModel.email.value.isEmpty()) | ||
| assert(viewModel.password.value.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `login button is not enabled on launch`() { | ||
| assert(!viewModel.enabled.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `the password value updates when the event is received`() { | ||
| val newPassword = "myValidPassword" | ||
| viewModel.onLoginChanged( | ||
| newEmail = "", // notice this is awkward to test when coupled | ||
| newPassword = newPassword, | ||
| ) | ||
| assertEquals(newPassword, viewModel.password.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a non-valid e-mail is not considered valid`() { | ||
| val emailIsValidExpected = false | ||
| val emailIsValidActual = viewModel.isValidEmail("bloop") | ||
|
|
||
| assertEquals(emailIsValidExpected, emailIsValidActual) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a valid e-mail is considered valid`() { | ||
| val emailIsValidExpected = true | ||
| val emailIsValidActual = viewModel.isValidEmail("bloop@gmail.com") | ||
|
|
||
| assertEquals(emailIsValidExpected, emailIsValidActual) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when an email and password are entered, the button to submit is enabled`() { | ||
| val newEmail = "myValidEmail@gmail.com" | ||
| val newPassword = "myValidPassword" | ||
|
|
||
| viewModel.onLoginChanged( | ||
| newEmail = newEmail, | ||
| newPassword = newPassword, | ||
| ) | ||
|
|
||
| assert(viewModel.enabled.value) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rememberCoroutineScopetotally works, but in this case, it is a bit more normal to useviewModelScope.launchin theViewModelinstead of doing the coroutine in the view.