I need to assert text color on BasicText in tests ...
# compose
a
I need to assert text color on BasicText in tests is there a way to do this w/o screenshot testing?
Yes. Claude showed me how to create a matcher
Copy code
@Test
    fun assertTextIsRed() = runComposeUiTest {
        setContent {
            Text("Hello World", color = Color.Red)
        }

        onNodeWithText("Hello World")
            .assertTextStyle { it.color == Color.Red }
    }

    private fun SemanticsNodeInteraction.assertTextStyle(matcher: (TextStyle) -> Boolean): SemanticsNodeInteraction {
        return this.assert(
            SemanticsMatcher("TextStyle") { node ->
                val textLayoutResults = mutableListOf<TextLayoutResult>()
                node.config[SemanticsActions.GetTextLayoutResult].action?.invoke(textLayoutResults)

                if (textLayoutResults.isNotEmpty()) {
                    val textStyle = textLayoutResults[0].layoutInput.style
                    matcher(textStyle)
                } else {
                    false
                }
            }
        )
    }