Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies {
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.compose.foundation.layout)
testImplementation(libs.junit)
testImplementation(libs.robolectric)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
Expand Down
8 changes: 2 additions & 6 deletions app/src/main/java/com/example/mvvm/ui/login/ui/loginScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ fun Login(
val password by viewModel.password.collectAsStateWithLifecycle()
val enabled by viewModel.enabled.collectAsStateWithLifecycle()
val loading by viewModel.loading.collectAsStateWithLifecycle()
val coroutine = rememberCoroutineScope()

if (loading) {
Box(Modifier.fillMaxSize()) {
Expand All @@ -78,11 +77,8 @@ fun Login(
Spacer(modifier = Modifier.padding((8.dp)))
ForgotPassword(Modifier.align(Alignment.End)) // column organiza a lo que hay debajo, la propiedad align() de los hijos de una Column espera un Alignment.Horizontal, no un Alignment completo (que es bidimensional).
Spacer(modifier = Modifier.padding((16.dp)))
LoginButton(enabled ) {
coroutine.launch {
viewModel.pressButton()

}
Comment on lines -82 to -85
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rememberCoroutineScope totally works, but in this case, it is a bit more normal to use viewModelScope.launch in the ViewModel instead of doing the coroutine in the view.

LoginButton(enabled) {
viewModel.pressButton()
}
}

Expand Down
20 changes: 11 additions & 9 deletions app/src/main/java/com/example/mvvm/ui/login/ui/loginViewModel.kt
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
Expand All @@ -29,15 +32,14 @@ class LoginViewModel : ViewModel() {

}

@VisibleForTesting fun isValidEmail(email: String): Boolean = Patterns.EMAIL_ADDRESS.matcher(email).matches()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VisibleForTesting is cool (to me 🤓). Basically, this function should be private. But if it was private, you couldn't test it in LoginViewModelTest. So @VisibleForTesting lets you use it in your test, but if you try to use this function anywhere else you will have problems compiling the app.

@VisibleForTesting fun isValidPassword(password: String): Boolean = password.length > 6
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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 isValidPassword is working easier 🤔


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)
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ lifecycleRuntimeKtx = "2.6.1"
activityCompose = "1.8.0"
composeBom = "2024.09.00"
foundationLayout = "1.9.4"
robolectric = "4.16"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand All @@ -26,6 +27,7 @@ androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-
androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout", version.ref = "foundationLayout" }
robolectric = { group = "org.robolectric", name = "robolectric", version.ref = "robolectric" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down