Hi all, I am trying to test a simple @Composable ...
# compose
a
Hi all, I am trying to test a simple @Composable
someComposable()
with a
BasicTextField
,
Text
and
LaunchedEffect
but its showing weird behaviour. When I call
someComposable()
,
onFocusChanged
event occurs first and then
LaunchedEffect
is called and hence Text does not show. But during testing,
LaunchedEffect
is called first and then
onFocusChanged
is called and Text shows up. I can’t figure out why the order is different in simple calling and in testing. Kindly guide me if someone has any idea what is going on. Code attached in the thread.
Copy code
@Composable
fun someComposable() {

    var text by remember {
        mutableStateOf("")
    }

    var show by remember {
        mutableStateOf(false)
    }

    LaunchedEffect(key1 = Unit, block =     {
        Log.d(TAG, "someComposable(): LaunchedEffect()")
        show = false
    })

    BasicTextField(
        value = text,
        onValueChange = { text = it },
        modifier = Modifier.onFocusChanged {
            Log.d(TAG, "someComposable(): onFocusChanged()")
            show = true
        })

    if (show) {
        Text(text = "Text", Modifier.testTag("text_tag"))
    }
}


@Test
fun testSomeComposable() {
    composeTestRule.setContent {
        someComposable()
    }
    composeTestRule.onNodeWithTag("text_tag", true).assertExists()
}
z
What version of compose is this?
This is almost certainly because until the very latest 1.4.0 alpha, the order in which coroutine dispatch happened relative to applyChanges was very different in production than in the test runtime. It’s partially fixed in the latest alpha and should be completely fixed in the next alpha (January)
a
Thanks