|
1 | 1 | <div align="center"> |
2 | | - <h1>native-testing-library (react native)</h1> |
| 2 | + <h1>(React) Native Testing Library</h1> |
| 3 | + <img |
| 4 | + height="80" |
| 5 | + width="80" |
| 6 | + alt="goat" |
| 7 | + src="https://raw.githubusercontent.com/brandonvcarroll/native-testing-library/master/other/mascot.png" |
| 8 | + /> |
| 9 | + |
3 | 10 | <p>Simple and complete React Native testing utilities that encourage good testing practices.</p> |
4 | 11 |
|
5 | 12 | [**Read The Docs**](https://native-testing-library.com/docs/intro) | |
|
10 | 17 |
|
11 | 18 | [](https://travis-ci.org/bcarroll22/native-testing-library) |
12 | 19 | [](https://codecov.io/github/bcarroll22/native-testing-library) |
13 | | -[](https://www.npmjs.com/package/native-testing-library) |
| 20 | +[](https://www.npmjs.com/package/native-testing-library) |
14 | 21 | [](http://www.npmtrends.com/native-testing-library) |
15 | 22 | [](https://github.com/bcarroll22/native-testing-library/blob/master/LICENSE) |
16 | | -[](http://makeapullrequest.com) |
| 23 | +[](http://makeapullrequest.com) |
17 | 24 |
|
18 | 25 | [](https://github.com/bcarroll22/native-testing-library/watchers) |
19 | 26 | [](https://github.com/bcarroll22/native-testing-library/stargazers) |
| 27 | + |
| 28 | +## The problem |
| 29 | + |
| 30 | +You want to write maintainable tests for your React Native application. You love Kent Dodds' testing |
| 31 | +library, and you want to be able to write maintainable tests for your React Native application. You |
| 32 | +don't want to use a library that renders components to a fake DOM, and you've had a hard time |
| 33 | +finding what you need to write tests using that philosophy in React Native. |
| 34 | + |
| 35 | +## This solution |
| 36 | + |
| 37 | +`native-testing-library` is an implementation of the well-known testing-library API that works for |
| 38 | +React Native. The primary goal is to mimic the testing library API as closely as possible while |
| 39 | +still accounting for the differences in the platforms. |
| 40 | + |
| 41 | +## Example |
| 42 | + |
| 43 | +```javascript |
| 44 | +import React from 'react'; |
| 45 | +import { Button, Text, TextInput, View } from 'react-native'; |
| 46 | +import { act, fireEvent, render, wait } from 'native-testing-library'; |
| 47 | + |
| 48 | +function Example() { |
| 49 | + const [name, setUser] = React.useState(''); |
| 50 | + const [show, setShow] = React.useState(false); |
| 51 | + |
| 52 | + return ( |
| 53 | + <View> |
| 54 | + <TextInput value={name} onChangeText={setUser} testID="input" /> |
| 55 | + <Button |
| 56 | + title="Print Username" |
| 57 | + onPress={() => { |
| 58 | + // let's pretend this is making a server request, so it's async |
| 59 | + // (you'd want to mock this imaginary request in your unit tests)... |
| 60 | + setTimeout(() => { |
| 61 | + setShow(!show); |
| 62 | + }, Math.floor(Math.random() * 200)); |
| 63 | + }} |
| 64 | + /> |
| 65 | + {show && <Text testID="printed-username">{name}</Text>} |
| 66 | + </View> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +test('examples of some things', async () => { |
| 71 | + const { getByTestId, getByText, queryByTestId, rootInstance } = render(<Example />); |
| 72 | + const famousWomanInHistory = 'Ada Lovelace'; |
| 73 | + |
| 74 | + const input = getByTestId('input'); |
| 75 | + fireEvent.changeText(input, famousWomanInHistory); |
| 76 | + |
| 77 | + const button = getByText('Print Username'); |
| 78 | + fireEvent.press(button); |
| 79 | + |
| 80 | + await wait(() => expect(queryByTestId('printed-username')).toBeTruthy()); |
| 81 | + |
| 82 | + expect(getByTestId('printed-username').props.children).toBe(famousWomanInHistory); |
| 83 | + expect(rootInstance).toMatchSnapshot(); |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +> [The more your tests resemble the way your software is used, the more confidence they can give you.](https://twitter.com/kentcdodds/status/977018512689455106) |
| 88 | +
|
| 89 | +We try to only expose methods and utilities that encourage you to write tests that closely resemble |
| 90 | +how your apps are used. |
| 91 | + |
| 92 | +Utilities are included in this project based on the following guiding principles: |
| 93 | + |
| 94 | +1. Rendering React Native components ultimately creates native views, and those views should be |
| 95 | + what you test rather than the React component instances you rendered to make them. |
| 96 | +2. In general, test the way your users use your app. There are instances where you'll need to write |
| 97 | + unit tests, but try your best to write with this first -- the more your tests resemble the way |
| 98 | + your app works, the more confident you'll be with your app. |
| 99 | +3. Be responsible, and remember that testing exists to serve you, not the other way around. If the |
| 100 | + library isn't working for you, contribute to make it work or do something more intuitive. Make |
| 101 | + your tests work for you and your team! |
| 102 | + |
| 103 | +In summary, we believe in the principles of `dom-testing-library` and its companion libraries, and |
| 104 | +try to adhere to them as closely as possible. Changes to this library should always consider how |
| 105 | +they relate to what's happening in the other libraries in this family of tools. |
| 106 | + |
| 107 | +## Installation |
| 108 | + |
| 109 | +This module should be install as in your project's `devDependencies`: |
| 110 | + |
| 111 | +``` |
| 112 | +npm install --save-dev native-testing-library |
| 113 | +``` |
| 114 | + |
| 115 | +You will need `react` and `react-native` installed as _dependencies_ in order to run this project. |
| 116 | + |
| 117 | +## Hooks |
| 118 | + |
| 119 | +You can test hooks out of the box with this package as follows: |
| 120 | + |
| 121 | +```javascript |
| 122 | +import { renderHook } from 'native-testing-library'; |
| 123 | +``` |
| 124 | + |
| 125 | +Reads more about hooks on the [docs site](https://native-testing-library.com/docs/api-render-hook). |
| 126 | + |
| 127 | +## Prior art |
| 128 | + |
| 129 | +Huge thanks to Kent C. Dodds for evangelizing this approach to testing. We could have never come up |
| 130 | +with this library without him 🙏. Check out his awesome work and learn more about testing with |
| 131 | +confidence at [testinghavascript.com](https://testingjavascript.com/) (you won't regret purchasing |
| 132 | +it), and of course, use this library's big brother, `react-testing-library` for your DOM |
| 133 | +applications as well! |
| 134 | + |
| 135 | +Another huge source of inspiration was the original |
| 136 | +[`react-native-testing-library`](https://github.com/callstack/react-native-testing-library) made by |
| 137 | +the awesome engineers at [Callstack](https://callstack.com/). Seeing their implementation and how |
| 138 | +they solved some of the big gotchas with testing React Native using this philosphy was extremely |
| 139 | +helpful. (And if you know anyone looking for some React Native development, be sure to send them |
| 140 | +their way 😉.) |
| 141 | + |
| 142 | +Last but not least, the hook testing ability of this library is quite literally the same as |
| 143 | +[react-hooks-testing-library](https://github.com/mpeyper/react-hooks-testing-library). The only |
| 144 | +reason it was included in this package is because we need you to import render from us, not the |
| 145 | +`dom-testing-library`, and that's an important blocker. Some day, maybe we'll try to allow use of |
| 146 | +that library with this one somehow. |
0 commit comments