Skip to content

Commit

Permalink
Add unit test for negative scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth Agarwal committed Sep 12, 2024
1 parent 790419b commit 8ce8aa8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class InputAgeSnapshotTest {
},
)

SubTitle("Input Age Component - Date Of Birth")
SubTitle("Input Age Component - Invalid Date Of Birth")
InputAge(
state = rememberInputAgeState(
inputAgeData = InputAgeData(
Expand All @@ -59,6 +59,22 @@ class InputAgeSnapshotTest {
},
)

SubTitle("Input Age Component - Date Of Birth")
InputAge(
state = rememberInputAgeState(
inputAgeData = InputAgeData(
title = "Label",
),
inputType = AgeInputType.DateOfBirth(
TextFieldValue("1991-11-27"),
),
inputState = InputShellState.DISABLED,
),
onValueChanged = {
},
)


SubTitle("Input Age Component - Date Of Birth Required Error")
InputAge(
state = rememberInputAgeState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import java.util.Calendar
* @param modifier: optional modifier.
*/
@Suppress("DEPRECATION")
@Deprecated("This component is deprecated and will be removed in the next release. Use InputDateTime instead.")
@Deprecated("This component is deprecated and will be removed in the next release. Use InputAge instead.")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun InputAge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package org.hisp.dhis.mobile.ui.designsystem.component
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.text.input.TextFieldValue
import org.hisp.dhis.mobile.ui.designsystem.component.state.InputAgeData
import org.hisp.dhis.mobile.ui.designsystem.component.state.rememberInputAgeState
import org.junit.Rule
Expand Down Expand Up @@ -223,4 +225,62 @@ class InputAgeTest {
assert(newInputDaysType.value.text == "28")
assert(newInputDaysType.unit == TimeUnitValues.DAYS)
}

@Test
fun shouldFormatDateCorrectly() {
rule.setContent {
InputAge(
state = rememberInputAgeState(
inputAgeData = InputAgeData(
title = "Label",
),
inputType = AgeInputType.DateOfBirth(TextFieldValue("1991-11-27")),
),
onValueChanged = {
// no-op
},
)
}

rule.onNodeWithTag("INPUT_AGE_TEXT_FIELD").assertExists().assertTextEquals("27/11/1991")
}

@Test
fun shouldShowErrorForOutsideRangeDate() {
rule.setContent {
InputAge(
state = rememberInputAgeState(
inputAgeData = InputAgeData(
title = "Label",
),
inputType = AgeInputType.DateOfBirth(TextFieldValue("2025-11-27")),
),
onValueChanged = {
// no-op
},
)
}

rule.onNodeWithTag("INPUT_AGE_TEXT_FIELD").assertExists().assertTextEquals("27/11/2025")
rule.onNodeWithTag("INPUT_AGE_SUPPORTING_TEXT").assertExists()
}

@Test
fun shouldWorkWithInvalidDate() {
rule.setContent {
InputAge(
state = rememberInputAgeState(
inputAgeData = InputAgeData(
title = "Label",
),
inputType = AgeInputType.DateOfBirth(TextFieldValue("1004-9999-9999")),
),
onValueChanged = {
// no-op
},
)
}

rule.onNodeWithTag("INPUT_AGE_TEXT_FIELD").assertExists().assertTextEquals("99/99/9999")
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8ce8aa8

Please sign in to comment.