-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test to radio button input component
- Loading branch information
Siddharth Agarwal
committed
Sep 14, 2023
1 parent
8930914
commit f8b6a22
Showing
2 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
...desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/RadioButtonInputTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |