I'm using jetpack compose and use this code to show my Snackbar:
LaunchedEffect(true) {
viewModel.snackBar.collectLatest { message ->
scaffoldState.snackbarHostState.currentSnackbarData?.dismiss()
scaffoldState.snackbarHostState.showSnackbar(message = message)
}
}
Normally, when I want to access to a compose element, I use the testTag in modifier. But the Snackbar does not have any. So how can I test that my Snackbar is shown with specific text?
I tried to use:
composeRule.onNodeWithText(SNACKBAR_MESSAGE).assertIsDisplayed()
but it can't find any node.
I had the same issue, and ultimately resorted to the following in order to get my test to pass:
composeTestRule.onNode(
hasText(SNACKBAR_MESSAGE),
useUnmergedTree = true
).assertIsDisplayed()
What was of critical importance was including useUnmergedTree = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With