|
| 1 | +--- |
| 2 | +id: version-3.0.0-api-helpers |
| 3 | +title: Helpers |
| 4 | +sidebar_label: Helpers |
| 5 | +original_id: api-helpers |
| 6 | +--- |
| 7 | + |
| 8 | +## Custom Queries |
| 9 | + |
| 10 | +`native-testing-library` exposes some of the helper functions that are used to implement the default |
| 11 | +queries. You can use the helpers to build custom queries. For example, the code below shows a way to |
| 12 | +query your TestInstance by a `style` prop. Note: test files would need to now import `test-utils.js` |
| 13 | +instead of using `native-testing-library` directly. Also note, please never actually implement this |
| 14 | +helper, it's just an example of what's possible. |
| 15 | + |
| 16 | +```javascript |
| 17 | +// test-utils.js |
| 18 | +import * as nativeTestingLib from 'native-testing-library'; |
| 19 | + |
| 20 | +const { queryHelpers } = nativeTestingLib; |
| 21 | + |
| 22 | +export const queryByStyle = queryHelpers.queryByProp.bind(null, 'style'); |
| 23 | +export const queryAllByStyle = queryHelpers.queryByProp.bind(null, 'style'); |
| 24 | + |
| 25 | +export function getAllByStyle(container, styles, ...rest) { |
| 26 | + const els = queryAllByStyle(container, styles, ...rest); |
| 27 | + if (!els.length) { |
| 28 | + throw getElementError(`Unable to find an element by style="${styles}"`, container); |
| 29 | + } |
| 30 | + return els; |
| 31 | +} |
| 32 | + |
| 33 | +export function getByStyle(...args) { |
| 34 | + return queryHelpers.firstResultOrNull(getAllByStyle, ...args); |
| 35 | +} |
| 36 | + |
| 37 | +// re-export with overrides |
| 38 | +export { |
| 39 | + ...nativeTestingLib, |
| 40 | + getByStyle, |
| 41 | + getAllByStyle, |
| 42 | + queryByStyle, |
| 43 | + queryAllByStyle, |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +> **Note** |
| 48 | +> |
| 49 | +> Custom queries can be added to the `render` method by adding `queries` to the options config |
| 50 | +> object. See the render [options](/docs/api-render#render-options). |
| 51 | +
|
| 52 | +## `getNodeText` |
| 53 | + |
| 54 | +```typescript |
| 55 | +getNodeText(node: React.ReactElement<any>) |
| 56 | +``` |
| 57 | + |
| 58 | +Returns the complete text content of an element, removing any extra whitespace, and joining children |
| 59 | +that are an array. The intention is to treat text in nodes exactly as how it is perceived by users |
| 60 | +in a browser, where any extra whitespace within words in the html code is not meaningful when the |
| 61 | +text is rendered, and all text appears as one cohesive string regardless of the code. |
| 62 | + |
| 63 | +```javascript |
| 64 | +getNodeText( |
| 65 | + <Text> |
| 66 | + {` |
| 67 | + Hello |
| 68 | + World ! |
| 69 | + `} |
| 70 | + </Text>, |
| 71 | +); // "Hello World !" |
| 72 | +``` |
| 73 | + |
| 74 | +## `within` and `getQueriesForElement` APIs |
| 75 | + |
| 76 | +`within` (an alias to `getQueriesForElement`) takes a `NativeTestInstance` and binds it to the raw |
| 77 | +query functions, allowing them to be used without manually specifying a container. |
| 78 | + |
| 79 | +Example: To get the username input of a login form within a `<LoginModal />`, you could do: |
| 80 | + |
| 81 | +```js |
| 82 | +import { render, within } from 'native-testing-library'; |
| 83 | + |
| 84 | +const { getByLabelText } = render(<LoginModal />); |
| 85 | +const loginForm = getByLabelText('login-form'); |
| 86 | + |
| 87 | +within(loginForm).getByPlaceholderText('Username'); |
| 88 | +``` |
| 89 | + |
| 90 | +## Debugging |
| 91 | + |
| 92 | +When you use any `get` calls in your test cases, the current contents of the `baseElement` get |
| 93 | +printed on the console. For example: |
| 94 | + |
| 95 | +```javascript |
| 96 | +// <Text>Hello world</Text> |
| 97 | +getByText('Goodbye world'); // will fail by throwing error |
| 98 | +``` |
| 99 | + |
| 100 | +The above test case will fail, however it prints the state of your React tree being tested, so you |
| 101 | +will get to see: |
| 102 | + |
| 103 | +``` |
| 104 | +Unable to find an element with the text: Goodbye world. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. |
| 105 | +Here is the state of your container: |
| 106 | +<Text> |
| 107 | + Hello World! |
| 108 | +</Text> |
| 109 | +``` |
| 110 | + |
| 111 | +Note: Since the debug size can get really large, you can set the limit of debug content to be |
| 112 | +printed via environment variable `DEBUG_PRINT_LIMIT`. The default value is `7000`. You will see |
| 113 | +`...` in the console, when the debug content is stripped off, because of the length you have set or |
| 114 | +due to default size limit. Here's how you might increase this limit when running tests: |
| 115 | + |
| 116 | +``` |
| 117 | +DEBUG_PRINT_LIMIT=10000 npm test |
| 118 | +``` |
| 119 | + |
| 120 | +This works on macOS/linux, you'll need to do something else for windows. If you'd like a solution |
| 121 | +that works for both, see [`cross-env`](https://www.npmjs.com/package/cross-env) |
| 122 | + |
| 123 | +### `prettyPrint` |
| 124 | + |
| 125 | +This helper function can be used to print out readable representation of the React tree of a node. |
| 126 | +This can be helpful for instance when debugging tests. |
| 127 | + |
| 128 | +It is defined as: |
| 129 | + |
| 130 | +```typescript |
| 131 | +function prettyPrint(node: React.ReactElement<any>, maxLength?: number): string; |
| 132 | +``` |
| 133 | + |
| 134 | +It receives the root node to print out, and an optional extra argument to limit the size of the |
| 135 | +resulting string, for cases when it becomes too large. |
| 136 | + |
| 137 | +This function is usually used alongside `console.log` to temporarily print out React trees during |
| 138 | +tests for debugging purposes: |
| 139 | + |
| 140 | +```javascript |
| 141 | +console.log( |
| 142 | + prettyPrint( |
| 143 | + <View> |
| 144 | + <Text>hi</Text> |
| 145 | + </View>, |
| 146 | + ), |
| 147 | +); |
| 148 | +// <View> |
| 149 | +// <Text> |
| 150 | +// Hi |
| 151 | +// </Text> |
| 152 | +// </View> |
| 153 | +``` |
| 154 | + |
| 155 | +This function is what also powers [the automatic debugging output described above](#debugging). |
0 commit comments