-
Notifications
You must be signed in to change notification settings - Fork 0
Check if the button is displayed on the screen and on click of it the proper message is displayed on snack bar
Devrath edited this page Mar 4, 2024
·
2 revisions
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun DemoSnackBar() {
val snackBarHostState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope()
Scaffold(content = {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(onClick = {
coroutineScope.launch {
// Launch the snack-bar in the co-routine
val snackBarResult = snackBarHostState.showSnackbar(
message = "SnackBarDemo",
actionLabel = "Undo",
duration = SnackbarDuration.Short
)
when (snackBarResult) {
SnackbarResult.ActionPerformed -> {
Log.d("Snackbar", "Action Performed")
}
else -> {
Log.d("Snackbar", "Snackbar dismissed")
}
}
}
}) {
Text(text = "Show Snack Bar", color = Color.White)
}
}
}, snackbarHost = { SnackbarHost(hostState = snackBarHostState) })
}
class SnackbarDemoTest {
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun myTest() {
// <------------------------ ASSIGN ------------------------>
val buttonText = "Show Snack Bar"
val snackBarText = "SnackBarDemo"
composeTestRule.setContent {
DemoSnackBar()
}
// <------------------------ ACT -------------------------->
composeTestRule.onNodeWithText(buttonText).performClick()
composeTestRule.onNodeWithText(snackBarText).isDisplayed()
}
}