Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to assert that a node is scrollable in jetpack compose testing

Is it possible to assert that a jetpack compose node is scrollable when performing tests?

class MyTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun givenEnoughItems_thenAssertListIsScrollable() {
        composeTestRule.setContent {
            BasicTheme {
                ScrollableComponent(items = List(1000) { "$it" })
            }
        }

        composeTestRule.onRoot().fetchSemanticsNode().assertIsScrollable()
    }
}

fun SemanticsNodeInteraction.assertIsScrollable() : SemanticsNodeInteraction {
    // What would go here? 
}
like image 294
Chris Avatar asked Oct 28 '25 10:10

Chris


1 Answers

Use hasScrollAction().

Example

import androidx.compose.ui.test.hasScrollAction
import androidx.compose.ui.test.assert

private fun assertNodeIsScrollable() {
    findNode().assert(hasScrollAction())
}

and findNode() would be something like

private fun findNode(): SemanticsNodeInteraction {
    return composeTestRule.onNodeWithTag(
        testTag = "test_tag",
    )
}
like image 197
Abhimanyu Avatar answered Oct 30 '25 00:10

Abhimanyu