Skip to content

Commit

Permalink
add test to radio button input component
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth Agarwal committed Sep 14, 2023
1 parent 8930914 commit f8b6a22
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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?>(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?>(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?>(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?>(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?>(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?>(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()
}
}

0 comments on commit f8b6a22

Please sign in to comment.