Problem
Dialog composables seem to be untestable in Jetpack Compose on the Desktop platform. Any action triggered from the test that adds a Dialog to the composition seems to immediately close the box again (when running the test, the dialog box itself actually opens for a brief second, which I don't expect to be happening), and the test fails to detect any element which is inside the dialog.
This does not happen on the actual production run -- we only see this problem in the tests. I have not tested this on other platforms, but (at least from the documentation) it seems to me that it works on Android.
Example Code
// Test Logic
@Test
fun `reproduce dialog test failure`() {
ui(compose) {
setContent {
var isDialogOpen by remember { mutableStateOf(false) }
DialogHolder(isDialogOpen) {v -> isDialogOpen = v}
}
onNodeWithText("Open").performClick()
awaitIdle()
onNodeWithText("Close").assertExists()
}
}
// --------------
@Composable fun DialogHolder(isDialogOpen: Boolean, updateDialog: (Boolean) -> Unit) {
Button(onClick = { updateDialog(true) }) {
Text("Open")
}
if (isDialogOpen) {
Dialog(onCloseRequest = { updateDialog(false) }) {
Button(onClick = { updateDialog(false) }) {
Text("Close")
}
}
}
}
Test Result
Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (Text + EditableText contains 'Close' (ignoreCase: false))
java.lang.AssertionError: Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (Text + EditableText contains 'Close' (ignoreCase: false))
at androidx.compose.ui.test.SemanticsNodeInteraction.fetchOneOrDie(SemanticsNodeInteraction.kt:162)
at androidx.compose.ui.test.SemanticsNodeInteraction.assertExists(SemanticsNodeInteraction.kt:137)
...
Screenshots (directly run from main()
)
Version Info
- Kotlin: 1.6.10
- Jetpack Compose: 1.1.1
- JVM Target: 11