Hi Guys, Problem: We are testing a lazy column w...
# compose
v
Hi Guys, Problem: We are testing a lazy column with list of values to check if all the values are present at their indexes. Like,
onNodeWithText("LazyColumnTestTag").onChildAt(index).assert(hasAnyDescendant(hasText("text at index")))
But when the list is scrolled the indexes of the list item changes. is there any other way I could achieve this? Previously, in the view system’s Recycler view this type of testing is working fine. Attempted solution: - scroll for each list item so that, it will come at the 0th index and test with 0th index. But when the list scrolls to the end the list can’t be scrolled after some index. So there the index changes.
m
You are on the right track I believe. I believe you have to use the VerticalScrollAxis semantic property to figure out what the current top index is in the LazyColumn after you scroll each time. From that you can figure out the index of the particular item in the child tree.
Copy code
@Test
    fun testScrolling() {
        rule.setContent {
            LazyColumn(modifier = Modifier.fillMaxSize().testTag("LazyColumn")) {
                items((0 .. 99).toList()) {
                    Text(text=it.toString())
                }
            }
        }

        println(rule.onRoot().printToString())

        rule.onNodeWithTag("LazyColumn")
            .assert(SemanticsMatcher.keyIsDefined(SemanticsProperties.VerticalScrollAxisRange))

        (0 until 100).forEach {
            rule.onNodeWithTag("LazyColumn")
                .performScrollToIndex(it)

            val scrollAxisRange = rule.onNodeWithTag("LazyColumn")
                .fetchSemanticsNode()
                .config[SemanticsProperties.VerticalScrollAxisRange]

            val firstIndex = scrollAxisRange.value().toInt()

            rule.onNodeWithTag("LazyColumn")
                .onChildAt(
                    maxOf(0, it-firstIndex)
                )
                .assert(hasText(it.toString()))
        }
    }
v
It works! Thanks a lot!
Also, is there any other ways to assert the total number of list items in LazyColumn. The
onChildren().assertCount()
only matches with the composed children count. I also tried with collectioninfo's row and column count i got the row count as -1 for a LazyColumn
312 Views