Content will be updated
This page was moved from the old website without any change and might be updated
This document provides a quick example of how to write tests for your event handlers. You should be familiar with TestSpecs before diving into this topic.
The package is shipped as a separate module. It is available via maven as
com.facebook.litho:litho-testing. To include it in your gradle build, add this
line to your
We are going to work with this spec in our example:
When testing event handlers, it is important to remember what you actually want to validate in your unit test. You may be getting this inkling to ensure that a click event you issue triggers the callback you pass in as your prop. When you do this, you are actually testing the framework. This is not what you want to spend your time on. While writing high-level end-to-end tests ensuring that your touch events have the right effects, this is not what you should concern yourself with for unit tests.
Instead, let's focus on the actual logic that we have in our spec. Whether or
not we have a click handler depends on the prop
canClick. It is very common
for handlers to be set conditionally based on a prop. In our test, we are going
to limit ourselves to checking if a handler is set or not. For that we can use
the TestSpec matchers we have learned about before.
As you can see here, we can match against a click handler just like any other
prop set on a sub-component. Matching against a specific instance of an
EventHandler is currently not supported.