2018-10-01 18:15:56 +02:00
# react-native-testing-library
2018-10-06 19:40:29 +02:00
Simple React Native testing utilities helping you write better tests with less effort
2018-10-06 19:39:44 +02:00
[![Build Status][build-badge]][build]
[![Version][version-badge]][package]
[![MIT License][license-badge]][license]
[![PRs Welcome][prs-welcome-badge]][prs-welcome]
[![Chat][chat-badge]][chat]
2018-10-07 19:02:48 +02:00
[![Greenkeeper badge][greenkeeper-badge]][greenkeeper]
2018-10-06 19:39:44 +02:00
2018-10-04 23:44:59 +02:00
_ Appreciation notice: This project is heavily inspired by [react-testing-library](https://github.com/kentcdodds/react-testing-library). Go check it out and use it to test your web React apps. _
2018-10-03 19:15:36 +02:00
## The problem
2018-10-25 05:45:53 -04:00
You want to write maintainable tests for your React Native components without testing implementation details, but then you're told to use Enzyme, which you learn has no React Native adapter, meaning only shallow rendering is supported. And you want to render deep! But deep rendering may otherwise require jsdom (React Native isn't the web!), while doing deep rendering with `react-test-renderer` is so painful.
2018-10-03 19:15:36 +02:00
You would also like to use the newest React features, but you need to wait for your testing library's abstractions to catch up and it takes a while.
2018-10-25 05:45:53 -04:00
You finally want to approach testing using only best practices, while Enzyme may encourage assertions on implementation details.
2018-10-03 19:15:36 +02:00
## This solution
The `react-native-testing-library` is a lightweight solution for testing your React Native components. It provides light utility functions on top of `react-test-renderer` letting you always be up to date with latest React features and write any component tests you like, be it shallow or deeply rendered ones. But really not any, it prevents you from testing implementation details because we stand this is a very bad practice.
2018-12-21 01:10:19 -06:00
This library is a replacement for [Enzyme ](http://airbnb.io/enzyme/ ). It is tested to work with Jest, but it should work with other test runners as well.
2018-10-03 19:15:36 +02:00
## Example
2018-10-03 20:21:46 +02:00
``` jsx
2018-10-07 08:23:21 +02:00
import { render , fireEvent } from 'react-native-testing-library' ;
2018-10-03 20:21:46 +02:00
import { QuestionsBoard } from '../QuestionsBoard' ;
2018-11-17 19:13:44 +01:00
import { Question } from '../Question' ;
2018-10-03 20:21:46 +02:00
2018-10-04 09:17:15 +02:00
function setAnswer ( question , answer ) {
2018-10-07 08:23:21 +02:00
fireEvent . changeText ( question , answer ) ;
2018-10-03 20:21:46 +02:00
}
test ( 'should verify two questions' , ( ) => {
2018-11-19 22:28:06 +01:00
const { getAllByType , getByText } = render ( < QuestionsBoard { ...props } / > ) ;
const allQuestions = getAllByType ( Question ) ;
2018-10-03 20:21:46 +02:00
2018-10-04 09:17:15 +02:00
setAnswer ( allQuestions [ 0 ] , 'a1' ) ;
setAnswer ( allQuestions [ 1 ] , 'a2' ) ;
2018-10-03 20:21:46 +02:00
2018-10-07 08:23:21 +02:00
fireEvent . press ( getByText ( 'submit' ) ) ;
2018-10-03 20:21:46 +02:00
expect ( props . verifyQuestions ) . toBeCalledWith ( {
'1' : { q : 'q1' , a : 'a1' } ,
'2' : { q : 'q2' , a : 'a2' } ,
} ) ;
} ) ;
```
2018-10-03 19:15:36 +02:00
## Installation
Open a Terminal in your project's folder and run:
``` sh
2018-10-03 21:33:29 +02:00
yarn add --dev react-native-testing-library
2018-10-03 19:15:36 +02:00
```
2018-10-17 22:12:18 +02:00
This library has a peerDependencies listing for `react-test-renderer` and, of course, `react` . Make sure to install them too!
2018-10-03 20:21:46 +02:00
As you may have noticed, it's not tied to React Native at all – you can safely use it in your React components if you feel like not interacting directly with DOM.
2018-10-03 19:15:36 +02:00
2018-11-23 15:03:22 +01:00
## API / Usage
2018-10-03 19:15:36 +02:00
2018-12-15 12:36:59 +01:00
The [public API ](docs/API.md ) of `react-native-testing-library` is focused around these essential methods:
2018-10-03 19:15:36 +02:00
2018-12-11 12:58:37 -06:00
- [`render` ](docs/API.md#render ) – deeply renders given React element and returns helpers to query the output components.
- [`shallow` ](docs/API.md#shallow ) – shallowly renders given React component. It doesn't return any helpers to query the output.
- [`fireEvent` ](docs/API.md#fireevent ) - invokes named event handler on the element.
- [`waitForElement` ](docs/API.md#waitforelement ) - waits for non-deterministic periods of time until your element appears or times out.
- [`flushMicrotasksQueue` ](docs/API.md#flushmicrotasksqueue ) - waits for microtasks queue to flush.
2018-10-07 10:59:27 +02:00
2019-01-24 16:46:17 +01:00
---
Supported and used by [Rally Health ](https://www.rallyhealth.com/company/careers ).
2018-10-06 19:39:44 +02:00
<!-- badges -->
2018-10-07 08:23:21 +02:00
2018-10-06 19:39:44 +02:00
[build-badge]: https://img.shields.io/circleci/project/github/callstack/react-native-testing-library/master.svg?style=flat-square
[build]: https://circleci.com/gh/callstack/react-native-testing-library
[version-badge]: https://img.shields.io/npm/v/react-native-testing-library.svg?style=flat-square
[package]: https://www.npmjs.com/package/react-native-testing-library
[license-badge]: https://img.shields.io/npm/l/react-native-testing-library.svg?style=flat-square
[license]: https://opensource.org/licenses/MIT
[prs-welcome-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[prs-welcome]: http://makeapullrequest.com
[chat-badge]: https://img.shields.io/discord/426714625279524876.svg?style=flat-square&colorB=758ED3
[chat]: https://discord.gg/QbGezWe
2018-10-07 19:02:48 +02:00
[greenkeeper-badge]: https://badges.greenkeeper.io/callstack/react-native-testing-library.svg?style=flat-square
[greenkeeper]: https://greenkeeper.io/