:test_tube: Compose Testing: When to use `assertIs...
# compose
t
🧪 Compose Testing: When to use
assertIs[Not]Displayed
and
assert[DoesNot]Exists
? 🤔
z
assertIsNotDisplayed
can pass if a composable is present but not within screen bounds, or maybe clipped, and I think alpha 0?
assertDoesNotExist
asserts if the composable doesn’t exist in the tree at all.
👍 1
t
Okay. Once a composable got entered into the tree, and removed later,
assertIsDisplayed
passing for that composable. In other words,
assertIsDisplayed
is working like “assertIfEverDisplayedInTheTree”. Is this also expected?
z
No, that doesn’t sound right. Can you post your code?
t
@Zach Klippenstein (he/him) [MOD] sorry, it works now. probably a mistake from my side 🤦. another question, would there be a scenario where both
assertIsDisplayed
and
assertIsNotDisplayed
will be failed ? 🤔
Copy code
class SampleTest {

    @get:Rule
    val composeRule = createComposeRule()

    @Test
    fun test() {
        // Setup content
        val labelHideMe = "HIDE ME"
        composeRule.setContent {
            var isShow by remember {
                mutableStateOf(true)
            }

            if(isShow){
                Text(text = labelHideMe, modifier = Modifier.clickable {isShow = false })
            }
        }

        composeRule.onNodeWithText(labelHideMe).performClick()
        composeRule.onNodeWithText(labelHideMe).assertIsNotDisplayed() // ❌ this will fail
    }
}
z
It fails because the node doesn’t exist, right? To your question, I would imagine if two assertions that are by definition mutually exclusive are both passing, without any actual changes happening to your UI, there’s a bug, but this code snippet seems unrelated.