Skip to content

Commit

Permalink
Rename LithoViewRule to LithoTestRule
Browse files Browse the repository at this point in the history
Summary:
- Rename all `LithoViewRule` to `LithoTestRule`
- Delete typealias

Reviewed By: adityasharat

Differential Revision: D63834338

fbshipit-source-id: cf97ce71ac3f4581ab8afa3c3524807239cc44a9
  • Loading branch information
Andrew Wang authored and facebook-github-bot committed Oct 3, 2024
1 parent 2f6d0bd commit b237490
Show file tree
Hide file tree
Showing 104 changed files with 1,227 additions and 1,229 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ For more details, see the [full diff](https://github.com/facebook/litho/compare/
_2022-10-27_

* New APIs for debugging.
* **Breaking**: `ComponentContext.withComponentScope` is now package-private: it was @VisibleForTesting before, but we are now enforcing the privacy. If you were using it in tests, you can replace it with `ComponentTestHelper.createScopedComponentContextWithStateForTest` (however you should really write tests against LithoViewRule for kt tests and LegacyLithoViewRule for java tests)
* **Breaking**: `ComponentContext.withComponentScope` is now package-private: it was @VisibleForTesting before, but we are now enforcing the privacy. If you were using it in tests, you can replace it with `ComponentTestHelper.createScopedComponentContextWithStateForTest` (however you should really write tests against LithoTestRule for kt tests and LegacyLithoTestRule for java tests)

For more details, see the [full diff](https://github.com/facebook/litho/compare/v0.43.0...v0.44.0).

Expand Down Expand Up @@ -272,7 +272,7 @@ _2020-05-01_
* New: Update accessibility utils to support newer version of Talkback.
* New: Replace Litho's `MountItem` with RenderCore's and wrap `LayoutOutput` with `RenderTreeNode`.
* New: Litho tests are now migrated to Robolectric 4 and Mockito 2!
* New: New testing utilities: `LithoViewRule`, `LithoStatsRule`, `BackgroundLayoutLooperRule`.
* New: New testing utilities: `LithoTestRule`, `LithoStatsRule`, `BackgroundLayoutLooperRule`.
* Fix: Remove 1px white margin between content and shadow for `CardSpec` [00f2bdb](https://github.com/facebook/litho/commit/00f2bdb852a7e0ded29494b909f0c65d1c7c7dc8).
* Fix: Fix Sections not updating layout for sticky items with indices outside range ratio.
* Fix: Fix `RecyclerSpec` not respecting RTL for padding.
Expand Down
6 changes: 3 additions & 3 deletions docs/kotlin/testing-actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ For example, it may be needed if a component defines a visibility event that tri
```kotlin file=litho-core/src/test/kotlin/com/facebook/litho/examples/LithoTestRuleExampleTest.kt start=idle_component_start end=idle_component_end
```

## Interactions with LithoViewRule
## Interactions with LithoTestRule

### How to test a click action

Expand All @@ -72,7 +72,7 @@ The test case performs three steps:

1. Confirm that the `Component` is not being rendered if there is no click on the `Row`.
* In order to do this, find the `Component` based on the Text or its Class by using either: `findViewWithTextOrNull(String)` or `findComponent(Class<out Component?>)` and assert that it's null.
2. Perform an action on the `LithoView` with the help of `LithoViewRule.act{}`.
2. Perform an action on the `LithoView` with the help of `LithoTestRule.act{}`.
* Clicking on the content description of the row triggers the state update.
3. Confirm that the `Component` is being rendered and is not null.
* Any of the available methods can be used to find the `Component`.
Expand All @@ -84,7 +84,7 @@ This test case is satisfied with the assertions shown in the following snippet:

### How to test `VisiblityEvent`

In order to test `VisiblityEvent` instead of using `LithoViewRule.render(Component)`, separate the methods that are being called under the render call.
In order to test `VisiblityEvent` instead of using `LithoTestRule.render(Component)`, separate the methods that are being called under the render call.

This enables the state of the component to be checked before and after the visibility event is triggered:

Expand Down
4 changes: 2 additions & 2 deletions docs/kotlin/testing-assertions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import VersionedCodeBlock from '@theme/VersionedCodeBlock';

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.
If you need to get a handle of a component that is inside the ComponentTree of the one you rendered using `LithoTestRule`, 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`.
Expand Down Expand Up @@ -69,7 +69,7 @@ It's possible to check if your component requires Props, as shown in the followi

## Assertions against the view hierarchy

`LithoViewRule.render(Component)` returns a `LithoView`, which can be used for assertions that can be found in the [LithoViewAssert](pathname:///javadoc/com/facebook/litho/testing/assertj/LithoViewAssert.html) class.
`LithoTestRule.render(Component)` returns a `LithoView`, which can be used for assertions that can be found in the [LithoViewAssert](pathname:///javadoc/com/facebook/litho/testing/assertj/LithoViewAssert.html) 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:

Expand Down
6 changes: 3 additions & 3 deletions docs/kotlin/testing-getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import VersionedCodeBlock from '@theme/VersionedCodeBlock';

The **Litho Testing API** is presented through the `LithoViewRule` class, it enables the following:
The **Litho Testing API** is presented through the `LithoTestRule` class, it enables the following:

- Make assertions against the Component hierarchy.
- Mark assertions against the View hierarchy.
- Provides access to utility functions to interact with the View hierarchy (such as by clicking).

`LithoViewRule` uses the Junit 'TestRule', which provides a flexible mechanism to execute code before and after a test method. As a result, Litho is able to prepare the environment then clean it up after testing has taken place, so there's no need to be concerned about it.
`LithoTestRule` uses the Junit 'TestRule', which provides a flexible mechanism to execute code before and after a test method. As a result, Litho is able to prepare the environment then clean it up after testing has taken place, so there's no need to be concerned about it.

For more information about the TestRules, refer to the official [JUnit documentation](https://junit.org/junit4/javadoc/4.12/org/junit/rules/TestRule.html).

Expand All @@ -34,7 +34,7 @@ In order to use the @Rule inside of a test class, it needs to be initialised:
```kotlin file=litho-core/src/test/kotlin/com/facebook/litho/examples/LithoTestRuleExampleTest.kt start=start_example end=end_example
```

Then, within a test, render the component with the help of the `LithoViewRule.render(Component)` method and perform assertions or other actions on the `LithoView` returned by the `render` call:
Then, within a test, render the component with the help of the `LithoTestRule.render(Component)` method and perform assertions or other actions on the `LithoView` returned by the `render` call:

```kotlin file=litho-core/src/test/kotlin/com/facebook/litho/examples/LithoTestRuleExampleTest.kt start=start_render_example end=end_render_example
```
Expand Down
6 changes: 3 additions & 3 deletions docs/testing/event-handler-testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ the TestSpec matchers we have learned about before.
```java
@RunWith(LithoTestRunner.class)
public class LearningStateComponentSpecTest {
@Rule public LithoViewRule mLithoViewRule = new LithoViewRule();
@Rule public LithoTestRule mLithoTestRule = new LithoTestRule();

@Test
public void testComponentOnClick() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final Component component = LearningStateComponent.create(
c)
.canClick(true)
Expand All @@ -110,7 +110,7 @@ public class LearningStateComponentSpecTest {

@Test
public void testNoComponentOnClick() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final Component component = LearningStateComponent.create(
c)
.canClick(false)
Expand Down
12 changes: 6 additions & 6 deletions docs/testing/prop-matching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ The `FeedItemComponent` contains the logic for populating our complex props whic
```java
@Test
public void testComplexSubComponent() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final Component<FeedItemComponent> component = makeComponent("Two Brothers");

assertThat(c, component)
Expand All @@ -86,7 +86,7 @@ If it's not possible to provide these props in your tests, or if you we don't wa
```java
@Test
public void testComplexSpecIsPresent() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final Component<FeedItemComponent> component = makeComponent("Rixty Minutes");

assertThat(c, component)
Expand Down Expand Up @@ -129,7 +129,7 @@ It does take the same props as the underlying component but enable you to omit n
```java
@Test
public void testComplexTestSpecProps() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final Component<FeedItemComponent> component = makeComponent("Two Brothers");

assertThat(c, component)
Expand Down Expand Up @@ -171,7 +171,7 @@ Instead of just matching against partial props, you can also provide 'hamcrest'
```
@Test
public void testComplexTestSpecAdvancedProps() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final Component<FeedItemComponent> component =
makeComponent("Rixty Minutes");
Expand Down Expand Up @@ -201,7 +201,7 @@ Continuing with the given example, suppose that the `FeedItemComponent` wraps th
```java
@Test
public void testComplexTestSpecProps() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final Component<FeedItemComponent> component = makeComponent("Ricksy Business");
assertThat(c, component)
Expand Down Expand Up @@ -312,7 +312,7 @@ public interface TestMyLayoutSpec {}
```java
@Test
public void testComplexTestSpecAdvancedProps() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final Component<MyWrapperComponent> component = ...;
assertThat(c, component)
Expand Down
16 changes: 8 additions & 8 deletions docs/testing/subcomponent-testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Whenever possible, test your business logic in isolation.
### Setup

* Add `@RunWith(LithoTestRunner.class)` to the top of the test class.
* Add a JUnit [`@Rule`](https://github.com/junit-team/junit4/wiki/Rules) `LithoViewRule`.
* Add a JUnit [`@Rule`](https://github.com/junit-team/junit4/wiki/Rules) `LithoTestRule`.
* Add a check to ensure that tests are run in debug mode.
`ComponentsConfiguration.IS_INTERNAL_BUILD` must be true.

Expand All @@ -49,7 +49,7 @@ The test class should look like the following:
@RunWith(RobolectricTestRunner.class)
public class TruncatingComponentTest {

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

@Before
public void assumeInDebugMode() {
Expand All @@ -67,7 +67,7 @@ public class TruncatingComponentTest {
```java
@Test
public void whenTextLengthIsLessThan16_shouldContainTextComponentWithFullText() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final TruncatingComponent component = TruncatingComponent.create(c)
.text("Mr. Meeseeks").build();

Expand Down Expand Up @@ -125,7 +125,7 @@ public class TruncatingComponentSpec {
```java
@Test
public void whenTextLengthIsLessThan16_shouldContainTextComponentWithFullText() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final TruncatingComponent component = TruncatingComponent.create(c).text("Mr. Meeseeks").build();

LegacyLithoAssertions.assertThat(c, component)
Expand Down Expand Up @@ -154,7 +154,7 @@ This test can be fixed by using a different Condition API called `deepSubCompone
```java
@Test
public void whenTextLengthIsLessThan16_shouldContainTextComponentWithFullText() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final TruncatingComponent component = TruncatingComponent.create(c).text("Mr. Meeseeks").build();

LegacyLithoAssertions.assertThat(c, component)
Expand Down Expand Up @@ -193,7 +193,7 @@ public static Condition<InspectableComponent> hasBackground() {
```java
@Test
public void whenTruncatingComponentIsRendered_shouldHaveBackground() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final TruncatingComponent component = TruncatingComponent.create(c).text("Mr. Meeseeks").build();

LegacyLithoAssertions.assertThat(c, component)
Expand All @@ -216,7 +216,7 @@ public class StoryComponentTest {

@Test
public void whenStoryComponentIsRendered_shouldContainAllSubcomponents() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final StoryComponent.Builder builder = StoryComponent.create(c).build();

assertThat(builder).hasSubComponents(
Expand All @@ -238,7 +238,7 @@ Litho provides a bridge interface [legacySubComponent](pathname:///javadoc/com/f
```java
@Test
public void testSubComponentLegacyBridge() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();

assertThat(c, mComponent)
.has(
Expand Down
8 changes: 4 additions & 4 deletions docs/testing/testing-treeprops.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ class ComponentWithTreePropSpec {
}
```

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

```java
@RunWith(LithoTestRunner.class)
public class ComponentWithTreePropTest {

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

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

mLithoViewRule
mLithoTestRule
.attachToWindow()
.setTreeProp(UserContext.class, new UserContext()) // setting tree props for the hierarchy.
.setRoot(component)
Expand Down
10 changes: 5 additions & 5 deletions docs/testing/unit-testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ To verify the rendering of the text and the icon.

* Create a new test class; `LikersComponentTest`.
* Add `@RunWith(RobolectricTestRunner.class)` to the top of the test class.
* Add a `LithoViewRule` JUnit [`@Rule`](https://github.com/junit-team/junit4/wiki/Rules) that sets up overrides for [Styleables](https://developer.android.com/reference/android/R.styleable.html)
* Add a `LithoTestRule` JUnit [`@Rule`](https://github.com/junit-team/junit4/wiki/Rules) that sets up overrides for [Styleables](https://developer.android.com/reference/android/R.styleable.html)
and exposes some useful APIs.

The test class should look like the following:
Expand All @@ -79,7 +79,7 @@ The test class should look like the following:
@RunWith(RobolectricTestRunner.class)
public class LikersComponentTest {

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

}
```
Expand All @@ -92,10 +92,10 @@ public class LikersComponentTest {
@RunWith(RobolectricTestRunner.class)
public class LikersComponentTest {

public final @Rule LithoViewRule mLithoViewRule = new LithoViewRule();
public final @Rule LithoTestRule mLithoTestRule = new LithoTestRule();
@Test
public void whenTwoUsersLike_shouldShowBothUserNames() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final ImmutableList<User> likers = ImmutableList.of(
new User("Jane"), new User("Mike")
);
Expand All @@ -108,7 +108,7 @@ public class LikersComponentTest {

@Test
public void whenUsersLike_shouldShowLikeIcon() {
final ComponentContext c = mLithoViewRule.getContext();
final ComponentContext c = mLithoTestRule.getContext();
final ImmutableList<User> likers = ImmutableList.of(
new User("Jane"), new User("Mike")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* <pre>
* <code>{@literal @}Test
* public void testMyLayoutSpec() {
* final ComponentContext c = mLithoViewRule.getContext();
* final ComponentContext c = mLithoTestRule.getContext();
* assertThat(c, mComponent)
* .has(subComponentWith(c,
* TestMyLayoutSpec.matcher(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.ComposeView
import com.facebook.litho.testing.LithoViewRule
import com.facebook.litho.testing.LithoTestRule
import com.facebook.litho.testing.testrunner.LithoTestRunner
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
Expand All @@ -40,7 +40,7 @@ import org.robolectric.annotation.LooperMode
@RunWith(LithoTestRunner::class)
class ComposeComponentTest {

@Rule @JvmField val lithoViewRule = LithoViewRule()
@Rule @JvmField val lithoViewRule = LithoTestRule()

lateinit var lithoView: LithoView

Expand Down
4 changes: 2 additions & 2 deletions litho-core/src/test/kotlin/com/facebook/litho/KEffectsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.facebook.litho
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import com.facebook.litho.kotlin.widget.Text
import com.facebook.litho.testing.LegacyLithoViewRule
import com.facebook.litho.testing.LegacyLithoTestRule
import com.facebook.litho.testing.exactly
import com.facebook.litho.testing.setRoot
import com.facebook.litho.testing.testrunner.LithoTestRunner
Expand All @@ -38,7 +38,7 @@ import org.robolectric.annotation.LooperMode
@RunWith(LithoTestRunner::class)
class KEffectsTest {

@Rule @JvmField val lithoViewRule = LegacyLithoViewRule()
@Rule @JvmField val lithoViewRule = LegacyLithoTestRule()

@Test
fun useEffect_createdThenReleased_callbacksAreInvoked() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.facebook.litho

import android.graphics.Color
import com.facebook.litho.testing.LegacyLithoViewRule
import com.facebook.litho.testing.LegacyLithoTestRule
import com.facebook.litho.testing.assertMatches
import com.facebook.litho.testing.child
import com.facebook.litho.testing.match
Expand All @@ -36,7 +36,7 @@ import org.junit.runner.RunWith
@RunWith(LithoTestRunner::class)
class StyleCompatTest {

@Rule @JvmField val lithoViewRule = LegacyLithoViewRule()
@Rule @JvmField val lithoViewRule = LegacyLithoTestRule()

@Test
fun widthAndHeight_whenSet_isRespected() {
Expand Down Expand Up @@ -405,7 +405,7 @@ class StyleCompatTest {
}
}

val node = LegacyLithoViewRule.getRootLayout(lithoViewRule, ComponentWithBorder())?.node
val node = LegacyLithoTestRule.getRootLayout(lithoViewRule, ComponentWithBorder())?.node
assertThat(node?.borderColors)
.isEqualTo(intArrayOf(Color.BLUE, Color.RED, Color.BLACK, Color.WHITE))
assertThat(node?.borderRadius).isEqualTo(floatArrayOf(5f, 6f, 7f, 8f))
Expand All @@ -423,7 +423,7 @@ class StyleCompatTest {
}
}

val node = LegacyLithoViewRule.getRootLayout(lithoViewRule, ComponentWithTransition())?.node
val node = LegacyLithoTestRule.getRootLayout(lithoViewRule, ComponentWithTransition())?.node
assertThat(node?.transitionKeyType).isEqualTo(Transition.TransitionKeyType.GLOBAL)
assertThat(node?.transitionKey).isEqualTo("test")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.robolectric.annotation.LooperMode

/** Examples of LithoViewRule usage */
/** Examples of LithoTestRule usage */
@LooperMode(LooperMode.Mode.LEGACY)
@RunWith(LithoTestRunner::class)
class LithoTestRuleExampleTest {
Expand Down
Loading

0 comments on commit b237490

Please sign in to comment.