https://kotlinlang.org logo
#compose
Title
# compose
o

Omkar Amberkar

10/03/2023, 10:13 PM
I am trying to test the modifier extensions in isolation but facing some issues right now. The assertion to check if the composable was displayed fails as well as need a way to check if my callbacks were invoked. 🧵
Copy code
@OptIn(ExperimentalTestApi::class)
@RunWith(AndroidJUnit4::class)
class TvPlayerKeyEventModifiersKtTest {

    @get:Rule
    val rule = createComposeRule()

    @Test
    fun `It maps player key events correctly`() {
        val isActionUpDpadUpInvoked = MutableStateFlow(false)
        rule.setContent {
            val focusRequester = FocusRequester()

            Box(
                modifier = Modifier
                    .handlePlayerKeyEvent(
                     
                        onActionUpDpadUp = { isActionUpDpadUpInvoked.value = true }
                    )
                    .focusRequester(focusRequester)
                    .focusable()
                    .testTag("test")
            )

            LaunchedEffect(Unit) { focusRequester.requestFocus() }
        }

        rule.onNodeWithTag("test")
            .assertIsDisplayed()
            .assertIsFocused()

        rule.onRoot().performKeyInput { pressKey(Key.DirectionUp) }

        assertThat(isActionUpDpadUpInvoked.value).isTrue()
    }
}
Copy code
fun Modifier.handlePlayerKeyEvent(
    onActionUpDpadUp: (() -> Unit)? = null,
)
Copy code
java.lang.AssertionError: Assert failed: The component is not displayed!
z

Zach Klippenstein (he/him) [MOD]

10/03/2023, 10:41 PM
You probably need a non-zero size. Add a
.size(1.dp)
to your modifier chain
o

Omkar Amberkar

10/03/2023, 11:35 PM
ahh, that worked. thanks zach