3

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? 
}
Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
Chris
  • 4,662
  • 2
  • 19
  • 27

1 Answers1

4

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",
    )
}
Abhimanyu
  • 11,351
  • 7
  • 51
  • 121