Skip to content

Commit

Permalink
Add tests for component
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimolloy committed Sep 8, 2023
1 parent 62a9ff6 commit 9c3280f
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ fun InputPositiveInteger(
val pattern = remember { Regex("^(?!0)\\d*") }
val keyboardOptions = KeyboardOptions(imeAction = imeAction, keyboardType = KeyboardType.Number)
InputShell(
modifier = modifier.testTag("INPUT_INTEGER"),
modifier = modifier.testTag("INPUT_POSITIVE_INTEGER"),
isRequiredField = isRequiredField,
title = title,
primaryButton = {
if (deleteButtonIsVisible) {
IconButton(
modifier = Modifier.testTag("INPUT_INTEGER_RESET_BUTTON"),
modifier = Modifier.testTag("INPUT_POSITIVE_INTEGER_RESET_BUTTON"),
icon = {
Icon(
imageVector = Icons.Outlined.Cancel,
Expand All @@ -75,7 +75,7 @@ fun InputPositiveInteger(
state = state,
legend = {
legendData?.let {
Legend(legendData, Modifier.testTag("INPUT_INTEGER_LEGEND"))
Legend(legendData, Modifier.testTag("INPUT_POSITIVE_INTEGER_LEGEND"))
}
},
supportingText = {
Expand All @@ -84,13 +84,13 @@ fun InputPositiveInteger(
SupportingText(
label.text,
label.state,
modifier = Modifier.testTag("INPUT_INTEGER_SUPPORTING_TEXT"),
modifier = Modifier.testTag("INPUT_POSITIVE_INTEGER_SUPPORTING_TEXT"),
)
}
},
inputField = {
BasicInput(
modifier = Modifier.testTag("INPUT_INTEGER_FIELD"),
modifier = Modifier.testTag("INPUT_POSITIVE_INTEGER_FIELD"),
inputText = inputValue ?: "",
onInputChanged = {
if (it.matches(pattern) || it.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package org.hisp.dhis.mobile.ui.designsystem.component

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertHasClickAction
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import org.hisp.dhis.mobile.ui.designsystem.theme.SurfaceColor
import org.junit.Rule
import org.junit.Test

class InputPositiveIntegerTest {

@get:Rule
val rule = createComposeRule()

@Test
fun shouldDisplayInputPositiveIntegerCorrectly() {
rule.setContent {
InputPositiveInteger(
title = "Label",
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_LEGEND").assertDoesNotExist()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_SUPPORTING_TEXT").assertDoesNotExist()
}

@Test
fun shouldAllowUserInputWhenEnabled() {
rule.setContent {
var inputValue by rememberSaveable { mutableStateOf("") }
InputPositiveInteger(
title = "Label",
inputText = inputValue,
onValueChanged = {
if (it != null) {
inputValue = it
}
},
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").performTextInput("1234")
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").assert(hasText("1234"))
}

@Test
fun shouldNotAllowUserInputWhenDisabled() {
rule.setContent {
InputPositiveInteger(
title = "Label",
state = InputShellState.DISABLED,
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").assertIsNotEnabled()
}

@Test
fun shouldShowResetButtonWhenTextFieldHasContent() {
rule.setContent {
var inputValue by rememberSaveable { mutableStateOf("") }
InputPositiveInteger(
title = "Label",
inputText = inputValue,
onValueChanged = {
if (it != null) {
inputValue = it
}
},
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").performTextInput("1234")
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_RESET_BUTTON").assertExists()
}

@Test
fun shouldDeleteContentWhenResetButtonIsClickedAndHideResetButton() {
rule.setContent {
var inputValue by rememberSaveable { mutableStateOf("1234") }

InputPositiveInteger(
title = "Label",
inputText = inputValue,
onValueChanged = {
if (it != null) {
inputValue = it
}
},
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_RESET_BUTTON").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_RESET_BUTTON").performClick()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").assertTextEquals("")
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_RESET_BUTTON").assertDoesNotExist()
}

@Test
fun shouldShowLegendCorrectly() {
rule.setContent {
InputPositiveInteger(
title = "Label",
inputText = "",
legendData = LegendData(SurfaceColor.CustomGreen, "Legend"),
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_LEGEND").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_LEGEND").assertHasClickAction()
}

@Test
fun shouldShowSupportingTextCorrectly() {
rule.setContent {
InputPositiveInteger(
title = "Label",
inputText = "",
supportingText = listOf(SupportingTextData("Supporting text", SupportingTextState.DEFAULT)),
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_SUPPORTING_TEXT").assertExists()
}

@Test
fun shouldNotAllowDecimalValues() {
rule.setContent {
var inputValue by rememberSaveable { mutableStateOf("") }
InputPositiveInteger(
title = "Label",
inputText = inputValue,
onValueChanged = {
if (it != null) {
inputValue = it
}
},
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").performTextInput("12.12")
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").assert(hasText(""))
}

@Test
fun shouldNotAllowNegativeValues() {
rule.setContent {
var inputValue by rememberSaveable { mutableStateOf("") }
InputPositiveInteger(
title = "Label",
inputText = inputValue,
onValueChanged = {
if (it != null) {
inputValue = it
}
},
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").performTextInput("-1212")
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").assert(hasText(""))
}

@Test
fun shouldNotAllowValuesWithALeadingZero() {
rule.setContent {
var inputValue by rememberSaveable { mutableStateOf("") }
InputPositiveInteger(
title = "Label",
inputText = inputValue,
onValueChanged = {
if (it != null) {
inputValue = it
}
},
)
}
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER").assertExists()
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").performTextInput("01212")
rule.onNodeWithTag("INPUT_POSITIVE_INTEGER_FIELD").assert(hasText(""))
}
}

0 comments on commit 9c3280f

Please sign in to comment.