# Component & UI Testing

> Render screens in a test environment and interact with them the way users do, with React Native Testing Library, widget tests, Compose or XCUITest. Free notes,...

Canonical: https://www.internhack.in/roadmaps/tracks/mobile/component-testing
Last updated: 2026-11-28

Render screens in a test environment and interact with them the way users do, with React Native Testing Library, widget tests, Compose or XCUITest.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [Unit Testing App Logic](https://www.internhack.in/roadmaps/tracks/mobile/unit-testing)

Component tests render a real screen or widget with fake data and assert on what the user would see and do. They catch broken layouts, missing error states and wiring bugs that unit tests cannot.

### Test like a user

Query by what users perceive (visible text, accessibility labels and roles), not by internal ids or component names. Tests written this way survive refactors and double as accessibility checks.

```
render(<LoginScreen />);
fireEvent.changeText(screen.getByLabelText("Email"), "a@b.co");
fireEvent.press(screen.getByRole("button", { name: "Sign in" }));
expect(await screen.findByText("Welcome back")).toBeOnTheScreen();
```

### Tools by stack

- **React Native:** **React Native Testing Library** on Jest. Mock the network with MSW or a fake API client, wrap components in the same providers (query client, navigation, theme) as the app.
- **Flutter:** **widget tests** with `testWidgets`, `tester.pumpWidget`, `find.text` and `tester.tap`. Golden tests compare pixels against a stored image.
- **Android:** Compose testing with `createComposeRule`, `onNodeWithText` and `performClick`; Espresso for View-based UIs.
- **iOS:** SwiftUI previews for manual checks, **XCUITest** for automated UI tests, snapshot testing libraries for visual regression.

### What to cover

- Loading, empty, error and success states of each important screen.
- Form validation messages and disabled buttons.
- Navigation triggers (the right route is pushed with the right params).

### Snapshot caution

Large snapshot tests break on every small change and get approved without review. Use them narrowly for visual components, and prefer explicit assertions elsewhere.

**Good enough to move on:** each key screen has tests for its main states, written with user-facing queries.

## Key concepts

- **Querying by text, role and label**
- **React Native Testing Library**: React Native
- **Flutter widget tests**: Flutter
- **Compose and Espresso tests**: Android
- **XCUITest**: iOS
- **Mocking the network with MSW or fakes**
- **Testing loading, empty and error states**
- **Golden and snapshot tests**

## Mini project

Write component tests for a login screen and a product list covering validation errors, a failed request, an empty list and a successful load.

## Check yourself

Why is querying a button by its accessible name more robust than querying it by a testID or component type?

## Free resources

- [React Native Testing Library](https://callstack.github.io/react-native-testing-library/) (Callstack)
- [Flutter: Widget testing](https://docs.flutter.dev/cookbook/testing/widget/introduction) (Flutter)
- [Compose: Testing](https://developer.android.com/develop/ui/compose/testing) (Android Developers)

Previous: [Unit Testing App Logic](https://www.internhack.in/roadmaps/tracks/mobile/unit-testing) · Next: [End-to-End Testing with Maestro & Detox](https://www.internhack.in/roadmaps/tracks/mobile/e2e-testing)

## More in Testing Mobile Apps

- [Unit Testing App Logic](https://www.internhack.in/roadmaps/tracks/mobile/unit-testing)
- [Component & UI Testing](https://www.internhack.in/roadmaps/tracks/mobile/component-testing)
- [End-to-End Testing with Maestro & Detox](https://www.internhack.in/roadmaps/tracks/mobile/e2e-testing)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Mobile App Developer Roadmap](https://www.internhack.in/roadmaps/tracks/mobile)
