WhoBuilt

What Is React Native Testing Library

Aug 18, 2020· 12 min· 1.4K views·Watch on YouTube

Mike from Callstack explains what React Native Testing Library does and demonstrates how its queries and events help teams test apps through user-facing behavior.

How the library started

React Native Testing Library reimplements APIs from React Testing Library for React Native, which has no DOM. It uses React Test Renderer in a custom Node.js environment. The team created it after finding that direct use of the renderer was cumbersome and Enzyme tests often depended on component internals, especially as React Hooks approached.

Test user-facing behavior

The library aims to make tests that assert on component internals difficult. Its queries find elements by text, accessibility labels, or input values, while avoiding direct state changes and lookups by component name. The goal is for tests to resemble how people use the app.

Render components and query elements

Mike walks through a test that imports `render` and `fireEvent`, sets up data and mocks, and renders a component with chosen props. The returned helpers can find elements by accessibility label or text. Finding an input by its accessibility label can also help developers check that it is usable with tools such as VoiceOver.

Simulate interactions and check results

Tests can use `fireEvent` to emulate user actions, including changing text, pressing, and scrolling. Mike demonstrates finding a submit control, pressing it, and checking whether `onSubmit` received the expected data.

Takeaways

  • Prefer queries based on text and accessibility labels to keep tests focused on what users can find.
  • Avoid tests that change component state or depend on internal properties, since implementation changes can make them brittle.
  • Use `fireEvent` to simulate common interactions and verify the resulting behavior.
  • Accessible labels can help both assistive technology users and test authors locate interactive elements.

Mentioned in this talk

Search inside this talk

What was claimed 8

Related talks

What Is React Native Testing Library · WhoBuilt