Skip to content

Commit

Permalink
Merge branch 'main' into fix/navigation/topAppBar-ui-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agonzalez-r authored Oct 30, 2024
2 parents 478facd + 7cba8e7 commit 3df84d0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,31 @@ class AlertScreenTest {
verify(navigationActions, never()).navigateTo(any<TopLevelDestination>())
verify(navigationActions, never()).navigateTo(any<String>())
}

@Test
fun createInvalidAlertNoMessage() {
// Leave message empty
composeTestRule.onNodeWithTag("alertProduct").performClick()
composeTestRule.waitForIdle()
composeTestRule.onNodeWithText("Pads").performClick()
composeTestRule.waitForIdle()

composeTestRule.onNodeWithTag("alertUrgency").performClick()
composeTestRule.waitForIdle()
composeTestRule.onNodeWithText("!! Medium").performClick()
composeTestRule.waitForIdle()

composeTestRule.onNodeWithTag("alertLocation").performTextInput("Rolex")

composeTestRule.onNodeWithTag("alertSubmit").performClick()
verify(navigationActions, never()).navigateTo(any<TopLevelDestination>())
verify(navigationActions, never()).navigateTo(any<String>())
}

@Test
fun createInvalidAlertAllEmptyFields() {
composeTestRule.onNodeWithTag("alertSubmit").assertIsDisplayed().performClick()
verify(navigationActions, never()).navigateTo(any<TopLevelDestination>())
verify(navigationActions, never()).navigateTo(any<String>())
}
}
5 changes: 0 additions & 5 deletions app/src/main/java/com/android/periodpals/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.android.periodpals

// import androidx.navigation.compose.NavHost
// import androidx.navigation.compose.composable
// import androidx.navigation.navigation
// import com.android.periodpals.ui.navigation.Route
// import com.android.periodpals.ui.navigation.Screen
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
Expand Down
33 changes: 25 additions & 8 deletions app/src/main/java/com/android/periodpals/ui/alert/AlertScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fun AlertScreen(navigationActions: NavigationActions) {
ExposedDropdownMenuSample(
listOf("Tampons", "Pads", "No Preference"),
"Product Needed",
"Please choose a product",
"alertProduct",
setProductIsSelected,
)
Expand All @@ -79,6 +80,7 @@ fun AlertScreen(navigationActions: NavigationActions) {
ExposedDropdownMenuSample(
listOf("!!! High", "!! Medium", "! Low"),
"Urgency level",
"Please choose an urgency level",
"alertUrgency",
setUrgencyIsSelected,
)
Expand All @@ -104,14 +106,12 @@ fun AlertScreen(navigationActions: NavigationActions) {
// Submit Button
Button(
onClick = {
if (location.isEmpty()) {
Toast.makeText(context, "Please enter a location", Toast.LENGTH_SHORT).show()
} else if (!productIsSelected) {
Toast.makeText(context, "Please select a product", Toast.LENGTH_SHORT).show()
} else if (!urgencyIsSelected) {
Toast.makeText(context, "Please select an urgency level", Toast.LENGTH_SHORT)
.show()
val errorMessage =
validateFields(productIsSelected, urgencyIsSelected, location, message)
if (errorMessage != null) {
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Alert sent", Toast.LENGTH_SHORT).show()
navigationActions.navigateTo(Screen.ALERT_LIST)
}
},
Expand All @@ -130,12 +130,13 @@ fun AlertScreen(navigationActions: NavigationActions) {
fun ExposedDropdownMenuSample(
list: List<String>,
label: String,
instruction: String,
testTag: String,
setIsSelected: (Boolean) -> Unit,
) {
var options = list
var expanded by remember { mutableStateOf(false) }
var text by remember { mutableStateOf("Please choose one option") }
var text by remember { mutableStateOf(instruction) }

ExposedDropdownMenuBox(
modifier = Modifier.testTag(testTag),
Expand Down Expand Up @@ -168,3 +169,19 @@ fun ExposedDropdownMenuSample(
}
}
}

/** Validates the fields of the alert screen. */
private fun validateFields(
productIsSelected: Boolean,
urgencyIsSelected: Boolean,
location: String,
message: String
): String? {
return when {
!productIsSelected -> "Please select a product"
!urgencyIsSelected -> "Please select an urgency level"
location.isEmpty() -> "Please enter a location"
message.isEmpty() -> "Please write your message"
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,24 +196,14 @@ private fun validateFields(
description: String
): String? {
return when {
!validateEmail(email) -> "Please enter an email"
!validateName(name) -> "Please enter a name"
email.isEmpty() -> "Please enter an email"
name.isEmpty() -> "Please enter a name"
!validateDate(date) -> "Invalid date"
!validateDescription(description) -> "Please enter a description"
description.isEmpty() -> "Please enter a description"
else -> null
}
}

/** Validates the email is not empty. */
private fun validateEmail(email: String): Boolean {
return email.isNotEmpty()
}

/** Validates the name is not empty. */
private fun validateName(name: String): Boolean {
return name.isNotEmpty()
}

/** Validates the date is in the format DD/MM/YYYY and is a valid date. */
fun validateDate(date: String): Boolean {
val parts = date.split("/")
Expand Down

0 comments on commit 3df84d0

Please sign in to comment.