From 9c3280fed30b56af3f07f57a700d4cd172f8f8ee Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Fri, 8 Sep 2023 08:51:31 +0200 Subject: [PATCH] Add tests for component --- .../component/InputPositiveInteger.kt | 10 +- .../component/InputPositiveIntegerTest.kt | 193 ++++++++++++++++++ 2 files changed, 198 insertions(+), 5 deletions(-) create mode 100644 designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputPositiveIntegerTest.kt diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputPositiveInteger.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputPositiveInteger.kt index 6eb104762..36c23260f 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputPositiveInteger.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputPositiveInteger.kt @@ -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, @@ -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 = { @@ -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()) { diff --git a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputPositiveIntegerTest.kt b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputPositiveIntegerTest.kt new file mode 100644 index 000000000..d329b4bc9 --- /dev/null +++ b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputPositiveIntegerTest.kt @@ -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("")) + } +}