Skip to main content

Matching @TreeProp

Tips

For help with setting up the test environment, see the Getting Started page.

Before learning about @TreeProp matching, it's recommended you become familiar with sub-component testing.

Testing components with @TreeProp​

While @Props are received from the immediate parent, @TreeProps can be passed down to a component from any of its ancestors in the current component hierarchy. When testing hierarchies containing components that contain @TreeProp, the TreeProp should be passed down to the components as expected. Following is an example LayoutSpec:

@LayoutSpec
class ComponentWithTreePropSpec {
@OnCreateLayout
static Component onCreateLayout(
ComponentContext c,
@Prop String normalProp,
@TreeProp UserContext userContext) { // <- Passed down by an ancestor.
// ...
}
}

To test a component that uses @TreeProps, you can use LithoViewRule#setTreeProp(Class, Object). This will set @TreeProps on the hierachy, making them available to the components:

@RunWith(LithoTestRunner.class)
public class ComponentWithTreePropTest {

public final @Rule LithoViewRule mLithoViewRule = new LithoViewRule();

@Test
public void test() {
final ComponentContext c = mLithoViewRule.getContext();
final Component component = ComponentWithTreeProp.create(c).build();

mLithoViewRule
.attachToWindow()
.setTreeProp(UserContext.class, new UserContext()) // setting tree props for the hierarchy.
.setRoot(component)
.measure()
.layout();

// test assertions as usual
}
}
info

@TestSpec does not support matching @TreeProp.