Event Handler Testing
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.
Prerequisites
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 dependencies
block:
testImplementation 'com.facebook.litho:litho-testing:0.36.0'
What to test for
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.
Testing handler presence
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.
Testing event handlers
We have now verified that the event handler is properly set, but not what
actually happens when the callback is invoked. How can we do that? Remember that
Component Specs ideally consist of only pure and hence static functions. This
means that you can test every function in isolation. For our example here, we
can directly test the incrementClickCount
method, completely independent of
its use within the click handler.
Note how we don't use a component instance or even the generated code from the
spec. You can also see how the StateValue
can be compared directly with
LithoAssertions.assertThat
, which has a special method for peeking inside the
container to compare values.
Next
Either head back to the testing overview or continue with the next section, Espresso.