I'm trying to write test for a compose component w...
# compose
a
I'm trying to write test for a compose component which has a LazyColumn inside I expect I can assert the count of the items in the LazyColumn, but it seems the children of onChildren() includes some of the sub-hierarchies. For example,
Copy code
val testData = listOf("item1", "item2", "item3")
composeTestRule.setContent {
            LazyColumn(Modifier.testTag(TEST_TAG)) {
                itemsIndexed(testData) { index, item ->
                    Row {
                        Text("index $index: ")
                        Spacer(modifier = Modifier.height(10.dp))
                        Text(item)
                    }
                }
            }
        }
composeTestRule.onNodeWithTag(TEST_TAG).onChildren().assertCountEquals(3)
It turns out the actual count of the children is 6 rather than 3, which counts the
Text("index $index: ")
and
Text(item)
inside the Row but not the
Spacer()
But what I actually want is to count how many rows in the LazyColumn. So how can I do it?
I end up adding a testTag in the composable I want to count and filter the tag
But I'm keen to understand how the nodes being count. like what is considered a 'node' what is not? If I write a Composable fun can I make it counted as a note?
j
I see you got to the answer Alex - I find testing the LazyXXXX components by observing the LazyListLayoutInfo is also a useful tool to have in your bag. E.g. https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:wear/com[…]ayoutInfoTest.kt?q=f:test%20f:layoutinfo%20f:wear&ss=androidx
👍 1