Hi. I wonder if there is a way to retrieve a seman...
# compose
s
Hi. I wonder if there is a way to retrieve a semantic object within a test and assert its values.
Say I have a composable with a text like this:
Copy code
val ItemKey = SemanticsPropertyKey<Item>("Item")
var SemanticsPropertyReceiver.item by ItemKey

data class Item(val id: Int, val name: String)

Text(
    modifier = Modifier.semantics {
        this.testTag = "test"
        this.item = Item(99, "TestXXX")
    },
    text = "My text"
)
And now in my test, I want to retrieve the
Item
object and assert its values
Copy code
composeTestRule.onNodeWithTag("test").assert(???, 99)
Anyone has a tip for how to do that?
There are other SemanticsMatchers for other things, and you can write your own assertions - just look at how the ones in the stdlib are implemented
s
Nice, that's easier than I thought for once ­čÖé Thanks for the help, I appreciate it
For reference if someone else is looking for this. you can create your own assert helper method. for above example it could look like this
Copy code
fun SemanticsNodeInteraction.assertItem(description: String, check: (Item) -> Boolean) {
    assert(SemanticsMatcher(description) {
        check(it.config[ItemKey])
    })
}
and then i can write my tests like this
Copy code
composeTestRule.onNodeWithTag("test").assertItem("Checking id") {
    it.id == 99
}