Skip to main content

Assertions

It's possible to use AssertJ-style APIs to assert what gets rendered by a Component or LithoView. All of the Component and LithoView assertions are exposed in LithoAssertions.assertThat() methods.

If you need to get a handle of a component that is inside the ComponentTree of the one you rendered using LithoViewRule, you can use the findComponent() method.

note

All of the following examples use the TestComponent that has one direct child, InnerComponent, and one non-direct child, Text.

  class TestComponent() : KComponent() {
override fun ComponentScope.render(): Component {
return InnerComponent()
}
}
}

class InnerComponent(
@VisibleForTesting val style: Style = Style.height(100.dp).width(100.dp),
@VisibleForTesting val value: String = "some_value"
) : KComponent() {
override fun ComponentScope.render(): Component {
return Row(style = style) { child(Text(text = value)) }
}
}

Assertions against the component hierarchy​

For component hierarchy assertions, there two main use-cases:

  • How to assert a component directly renders a certain component.
  • How to assert a component renders a certain component somewhere in its children.

These use cases are detailed in the following sub-sections.

Use Case: how to assert my component directly renders a certain component​

If you want to find out only the direct children, which for the TestComponent is InnerComponent, you can use findDirectComponent(Class<out Component?>):

val testLithoView = lithoViewRule.render { TestComponent() }
val innerComponent = testLithoView.findDirectComponent(InnerComponent::class)
assertThat(innerComponent).isNotNull()

Use Case: how to assert my component renders a certain component somewhere in its children​

If you want to find a component that is not a direct child of your component (or if the hierarchy level is not important in your test), you can use findComponent(Class<out Component?>):

val textComponent = testLithoView.findComponent(Text::class)
assertThat(textComponent).isNotNull()

If you want to confirm that given component classes were rendered, but you don't need a component instance itself, you can use either of the following:

  • LithoAssertions.assertThat(lithoViewRule.lithoView).containsComponent()
  • LithoAssertions.assertThat(lithoViewRule.lithoView).containsExactly()

Both are used in the following snippet:

val testLithoView = lithoViewRule.render { TestComponent() }
assertThat(testLithoView)
.containsExactly(2, InnerComponent::class)
.containsComponents(DeepComponent::class, InnerSecondComponent::class)

You can use containsComponent() hasExactly() providing different parameters such as 'the number of times the component class should be present in the component tree' or 'the varargs of all of the classes you would like to check'.

In addition, you can check if the view doesn't contain a given class by using doesNotContainComponents():

  • containsComponent(KClass<? extends Component>)
  • hasExactly(int, KClass<? extends Component>)
  • containsExactlyOne(KClass<? extends Component>)
  • doesNotContainComponents(KClass<? extends Component>...)

Props checking​

It's possible to check if your component requires Props, as shown in the following snippet:

val testLithoView = lithoViewRule.render { TestComponent() }
assertThat(testLithoView).willRenderContent()
val component = testLithoView.findComponent(InnerComponent::class)

assertThat(component)
.hasProps(
InnerComponent::value to "some_value",
InnerComponent::style to Style.height(100.dp).width(100.dp))
.hasPropsMatching(InnerComponent::value to IsInstanceOf.instanceOf(String::class.java))

Assertions against the view hierarchy​

LithoViewRule.render(Component) returns a LithoView, which can be used for assertions that can be found in the LithoViewAssert class.

With this assertion, you can, for example, check if a given text or drawable is visible or if the view contains a given test key, as shown in the following snippet:

val testLithoView = lithoViewRule.render { TestComponent() }
assertThat(testLithoView).hasVisibleText("some_value")
/** can use all of the assertions from: [LithoViewAssert] class */
  • hasContentDescription(String)
  • hasVisibleDrawable(@DrawableRes int)
  • hasVisibleDrawable(Drawable)
  • doesNotHaveVisibleDrawable(@DrawableRes int)
  • doesNotHaveVisibleDrawable(Drawable)
  • hasVisibleText(String)
  • hasVisibleText(@StringRes int)
  • doesNotHaveVisibleText(@StringRes int)
  • hasVisibleTextMatching(String)
  • hasMeasuredHeightOf(int)
note

Row and Column are not KComponents, they are just made to look like it. Assertions and hasProps won't work on them.