From f8b6a222f6f457c369afe0cb578a0c646de3e291 Mon Sep 17 00:00:00 2001 From: Siddharth Agarwal Date: Thu, 14 Sep 2023 13:08:32 +0530 Subject: [PATCH] add test to radio button input component --- .../ui/designsystem/component/RadioButton.kt | 4 +- .../component/RadioButtonInputTest.kt | 229 ++++++++++++++++++ 2 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButton.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButton.kt index e22f633a7..fa1c34321 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButton.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButton.kt @@ -15,6 +15,7 @@ import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag import org.hisp.dhis.mobile.ui.designsystem.theme.InternalSizeValues import org.hisp.dhis.mobile.ui.designsystem.theme.Outline import org.hisp.dhis.mobile.ui.designsystem.theme.Ripple @@ -66,7 +67,8 @@ fun RadioButton( interactionSource = interactionSource, modifier = Modifier .size(InternalSizeValues.Size40) - .hoverPointerIcon(radioButtonData.enabled), + .hoverPointerIcon(radioButtonData.enabled) + .testTag("RADIO_BUTTON_${radioButtonData.uid}"), colors = RadioButtonDefaults.colors( selectedColor = SurfaceColor.Primary, unselectedColor = Outline.Dark, diff --git a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt new file mode 100644 index 000000000..9bc2be39b --- /dev/null +++ b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt @@ -0,0 +1,229 @@ +package org.hisp.dhis.mobile.ui.designsystem.component + +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.test.assertHasClickAction +import androidx.compose.ui.test.assertIsNotSelected +import androidx.compose.ui.test.assertIsSelected +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.performClick +import org.hisp.dhis.mobile.ui.designsystem.theme.SurfaceColor +import org.junit.Rule +import org.junit.Test + +class RadioButtonInputTest { + + @get:Rule + val rule = createComposeRule() + + @Test + fun shouldDisplayRadioInputCorrectly() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + val selectedItem by remember { + mutableStateOf(radioButtonData[0]) + } + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + itemSelected = selectedItem, + modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + ) + } + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_LEGEND").assertDoesNotExist() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_SUPPORTING_TEXT").assertDoesNotExist() + } + + @Test + fun shouldAllowUserSelectionWhenEnabled() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + var selectedItem by remember { + mutableStateOf(radioButtonData[0]) + } + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + itemSelected = selectedItem, + onItemChange = { + selectedItem = it + }, + ) + } + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_1").performClick() + rule.onNodeWithTag("RADIO_BUTTON_1").assertIsSelected() + } + + @Test + fun shouldNotAllowUserSelectionWhenDisabled() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + var selectedItem by remember { + mutableStateOf(radioButtonData[0]) + } + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + state = InputShellState.DISABLED, + itemSelected = selectedItem, + onItemChange = { + selectedItem = it + }, + ) + } + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_1").performClick() + rule.onNodeWithTag("RADIO_BUTTON_1").assertIsNotSelected() + } + + @Test + fun shouldShowClearButtonWhenItemSelected() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + val selectedItem by remember { + mutableStateOf(radioButtonData[0]) + } + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + itemSelected = selectedItem, + ) + } + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_CLEAR_BUTTON").assertExists() + } + + @Test + fun shouldHideClearButtonWhenNoItemIsSelected() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + ) + } + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_CLEAR_BUTTON").assertDoesNotExist() + } + + @Test + fun shouldHideClearButtonWhenDisabled() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + val selectedItem by remember { + mutableStateOf(radioButtonData[0]) + } + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + state = InputShellState.DISABLED, + itemSelected = selectedItem, + modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + ) + } + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_CLEAR_BUTTON").assertDoesNotExist() + } + + @Test + fun shouldClearSelectionWhenClearButtonIsClickedAndHideClearButton() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + var selectedItem by remember { + mutableStateOf(radioButtonData[0]) + } + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + modifier = Modifier.testTag("RADIO_BUTTON_INPUT"), + itemSelected = selectedItem, + onItemChange = { + selectedItem = it + }, + ) + } + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_CLEAR_BUTTON").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_CLEAR_BUTTON").performClick() + rule.onNodeWithTag("RADIO_BUTTON_0").assertIsNotSelected() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_CLEAR_BUTTON").assertDoesNotExist() + } + + @Test + fun shouldShowLegendCorrectly() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + legendData = LegendData(SurfaceColor.CustomGreen, "Legend"), + ) + } + + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_LEGEND").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_LEGEND").assertHasClickAction() + } + + @Test + fun shouldShowSupportingTextCorrectly() { + rule.setContent { + val radioButtonData = listOf( + RadioButtonData("0", selected = false, enabled = true, textInput = "Option 1"), + RadioButtonData("1", selected = false, enabled = true, textInput = "Option 2"), + RadioButtonData("2", selected = false, enabled = true, textInput = "Option 3"), + ) + RadioButtonInput( + title = "Label", + radioButtonData = radioButtonData, + supportingText = listOf(SupportingTextData("Supporting text", SupportingTextState.DEFAULT)), + ) + } + rule.onNodeWithTag("RADIO_BUTTON_INPUT").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_INPUT_SUPPORTING_TEXT").assertExists() + } +}