Hi, I have updated to the 0.4.0 release of Decompo...
# mvikotlin
m
Hi, I have updated to the 0.4.0 release of Decompose. The
ComponentContext
now only exposes a
BackPressedHandler
which can be used to register handlers for the back-button. I have several unit tests that verify the correct behaviour of the back button action which I have initiated with
onBackPressed()
which is now missing. How can I simulate a back-button pressed in a unit test? Thanks.
Okay, writing it down helped me find a solution by myself. 🙂 I use a special
TestBackPressedHandler
in my code that mimicks the
DefaultBackPressedDispatcher
of Decompose that exposes the `onBackPressed()`method.
Copy code
private val backPressedHandler = TestBackPressedHandler()
    private val componentContext = DefaultComponentContext(lifecycle, backPressedHandler = backPressedHandler)
    private val componentUnderTest = MyComponent(componentContext, ...)
a
I think you should be able to use DefaultBackPressedDispatcher in your test? Just store it in a variable, pass it down to the DefaultComponentContext, and use the variable to call the dispatchet.
m
I would like to, but the
DefaultBackPressedDispatcher
is
internal
and thus cannot be accessed from my code.
a
m
Good to know. Works like a charm. Thanks!
🎉 1