Skip to content

Commit

Permalink
Merge pull request #219 from myofficework000/compose-testing-registra…
Browse files Browse the repository at this point in the history
…tion

registration confirmpassword and compose testing
  • Loading branch information
myofficework000 authored Dec 15, 2023
2 parents 4da1d77 + 1c12958 commit 10803cb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand Down Expand Up @@ -38,6 +39,7 @@ class RegistrationKtTest{
onNodeWithText("Mobile Number").assertExists()
onNodeWithText("Password").assertExists()
onNodeWithText("Confirm Password").assertExists()
onNodeWithTag("Register button").assertExists()
}
}

Expand Down Expand Up @@ -66,6 +68,62 @@ class RegistrationKtTest{
assertTrue(errorMsg == emailErrMsg.value)
performTextClearance()
}
}

@Test
fun verifyConfirmPasswordValidationWithValidPassword(){
val validConfirmPassword = ""
val errorMsg = ""

composeTestRule.onNodeWithText("Confirm Password").apply {
performTextInput(validConfirmPassword)
assert(hasText(validConfirmPassword))
assertFalse(isConfirmPasswordInvalid.value)
assertTrue(errorMsg == "")
performTextClearance()
}
}

@Test
fun verifyConfirmPasswordValidationWithInvalidPassword(){
val validConfirmPassword = "password"
val errorMsg = "Password is not matched"

composeTestRule.onNodeWithText("Confirm Password").apply {
performTextInput(validConfirmPassword)
assert(hasText(validConfirmPassword))
assertTrue(isConfirmPasswordInvalid.value)
assertTrue(errorMsg == "Password is not matched")
performTextClearance()
}
}

@Test
fun verifyButtonClick(){
val userName = "user"
val email = "[email protected]"
val mobile = "123-456-789"
val password = "password"
val confirmPassword = "password"

composeTestRule.onNodeWithText("Username")
.performTextInput(userName)

composeTestRule.onNodeWithText("Email")
.performTextInput(email)

composeTestRule.onNodeWithText("Mobile Number")
.performTextInput(mobile)

composeTestRule.onNodeWithText("Password")
.performTextInput(password)

composeTestRule.onNodeWithText("Confirm Password")
.performTextInput(confirmPassword)

composeTestRule.onNodeWithTag("Register button").performClick()

assertTrue(email.isNotEmpty())
assertTrue(password.isNotEmpty())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fun RegistrationForm(
SpacerSmall()

//register button
SimpleTextButton("Register") {
SimpleTextButton("Register", modifier = Modifier.testTag("Register button")) {
onRegister
}
}
Expand Down Expand Up @@ -170,7 +170,10 @@ fun ConfirmPasswordTextField() {
OutlinedTextField(
value = confirmPassword,
leadingIcon = { Icon(imageVector = Icons.Default.Lock, contentDescription = "lockIcon") },
onValueChange = { confirmPassword = it },
onValueChange = {
confirmPassword = it
validConfirmPassword(password = password, confirmPassword = confirmPassword) },
isError = isConfirmPasswordInvalid.value,
label = { Text(text = "Confirm Password") },
)
}
Expand Down Expand Up @@ -199,10 +202,21 @@ fun validateEmail(email: String) {
}
}

fun validConfirmPassword(password: String, confirmPassword: String) {
if (password != confirmPassword) {
isConfirmPasswordInvalid.value = true
emailErrMsg.value = "Password is not matched"
} else {
isConfirmPasswordInvalid.value = false
emailErrMsg.value = ""
}
}

var regUser: RegisterUser = RegisterUser()
var username: MutableState<String> = mutableStateOf(regUser.name)
var password: String by mutableStateOf("")
var confirmPassword: String by mutableStateOf("")
var isConfirmPasswordInvalid: MutableState<Boolean> = mutableStateOf(false)
var email: MutableState<String> = mutableStateOf(regUser.email)
var isEmailValid: MutableState<Boolean> = mutableStateOf(false)
var emailErrMsg: MutableState<String> = mutableStateOf("")
Expand Down

0 comments on commit 10803cb

Please sign in to comment.