@testing-library/react-native 12.1.1 → 12.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.DS_Store +0 -0
- package/.codecov.yml +9 -0
- package/.eslintcache +1 -0
- package/.eslintignore +2 -0
- package/.eslintrc +19 -0
- package/.flowconfig +63 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +14 -0
- package/.github/ISSUE_TEMPLATE/question.md +9 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +10 -0
- package/.github/actions/setup-deps/action.yml +22 -0
- package/.github/actions/setup-website-deps/action.yml +22 -0
- package/.github/dependabot.yml +10 -0
- package/.github/workflows/deploy-website.yml +36 -0
- package/.github/workflows/example-apps.yml +25 -0
- package/.github/workflows/main.yml +103 -0
- package/.gitignore +11 -0
- package/.prettierrc.js +5 -0
- package/CODE_OF_CONDUCT.md +73 -0
- package/CONTRIBUTING.md +64 -0
- package/README.md +8 -7
- package/babel.config.js +22 -0
- package/build/act.js.map +1 -1
- package/build/cleanup.js.map +1 -1
- package/build/config.d.ts +1 -0
- package/build/config.js.map +1 -1
- package/build/fireEvent.d.ts +13 -5
- package/build/fireEvent.js +57 -48
- package/build/fireEvent.js.map +1 -1
- package/build/flush-micro-tasks.d.ts +19 -0
- package/build/flush-micro-tasks.js +36 -0
- package/build/flush-micro-tasks.js.map +1 -0
- package/build/helpers/accessiblity.js +3 -1
- package/build/helpers/accessiblity.js.map +1 -1
- package/build/helpers/component-tree.d.ts +0 -15
- package/build/helpers/component-tree.js +0 -45
- package/build/helpers/component-tree.js.map +1 -1
- package/build/helpers/deprecation.d.ts +1 -0
- package/build/helpers/deprecation.js +16 -0
- package/build/helpers/deprecation.js.map +1 -1
- package/build/helpers/errors.d.ts +0 -4
- package/build/helpers/errors.js +1 -25
- package/build/helpers/errors.js.map +1 -1
- package/build/helpers/findAll.js.map +1 -1
- package/build/helpers/format-default.js.map +1 -1
- package/build/helpers/format.js.map +1 -1
- package/build/helpers/host-component-names.js +4 -1
- package/build/helpers/host-component-names.js.map +1 -1
- package/build/helpers/matchers/matchLabelText.js.map +1 -1
- package/build/helpers/pointer-events.d.ts +9 -0
- package/build/helpers/pointer-events.js +25 -0
- package/build/helpers/pointer-events.js.map +1 -0
- package/build/helpers/stringValidation.js.map +1 -1
- package/build/helpers/timers.js.map +1 -1
- package/build/index.js +2 -2
- package/build/index.js.map +1 -1
- package/build/matches.js.map +1 -1
- package/build/pure.d.ts +1 -1
- package/build/pure.js.map +1 -1
- package/build/queries/a11yState.js.map +1 -1
- package/build/queries/a11yValue.js.map +1 -1
- package/build/queries/displayValue.js.map +1 -1
- package/build/queries/hintText.js.map +1 -1
- package/build/queries/labelText.js.map +1 -1
- package/build/queries/makeQueries.js.map +1 -1
- package/build/queries/placeholderText.js.map +1 -1
- package/build/queries/role.js.map +1 -1
- package/build/queries/testId.js.map +1 -1
- package/build/queries/text.js.map +1 -1
- package/build/queries/unsafeProps.js.map +1 -1
- package/build/render.d.ts +171 -12
- package/build/render.js +11 -4
- package/build/render.js.map +1 -1
- package/build/renderHook.d.ts +3 -6
- package/build/renderHook.js +4 -3
- package/build/renderHook.js.map +1 -1
- package/build/shallow.js.map +1 -1
- package/build/test-utils/events.d.ts +10 -0
- package/build/test-utils/events.js +27 -0
- package/build/test-utils/events.js.map +1 -0
- package/build/test-utils/index.d.ts +1 -0
- package/build/test-utils/index.js +17 -0
- package/build/test-utils/index.js.map +1 -0
- package/build/user-event/event-builder/common.d.ts +45 -0
- package/build/user-event/event-builder/common.js +58 -0
- package/build/user-event/event-builder/common.js.map +1 -0
- package/build/user-event/event-builder/index.d.ts +32 -0
- package/build/user-event/event-builder/index.js +12 -0
- package/build/user-event/event-builder/index.js.map +1 -0
- package/build/user-event/index.d.ts +9 -0
- package/build/user-event/index.js +16 -0
- package/build/user-event/index.js.map +1 -0
- package/build/user-event/press/constants.d.ts +2 -0
- package/build/user-event/press/constants.js +16 -0
- package/build/user-event/press/constants.js.map +1 -0
- package/build/user-event/press/index.d.ts +1 -0
- package/build/user-event/press/index.js +19 -0
- package/build/user-event/press/index.js.map +1 -0
- package/build/user-event/press/press.d.ts +7 -0
- package/build/user-event/press/press.js +106 -0
- package/build/user-event/press/press.js.map +1 -0
- package/build/user-event/press/utils/warnAboutRealTimers.d.ts +1 -0
- package/build/user-event/press/utils/warnAboutRealTimers.js +14 -0
- package/build/user-event/press/utils/warnAboutRealTimers.js.map +1 -0
- package/build/user-event/setup/index.d.ts +2 -0
- package/build/user-event/setup/index.js +13 -0
- package/build/user-event/setup/index.js.map +1 -0
- package/build/user-event/setup/setup.d.ts +39 -0
- package/build/user-event/setup/setup.js +56 -0
- package/build/user-event/setup/setup.js.map +1 -0
- package/build/user-event/type/index.d.ts +1 -0
- package/build/user-event/type/index.js +13 -0
- package/build/user-event/type/index.js.map +1 -0
- package/build/user-event/type/type.d.ts +3 -0
- package/build/user-event/type/type.js +18 -0
- package/build/user-event/type/type.js.map +1 -0
- package/build/user-event/utils/events.d.ts +9 -0
- package/build/user-event/utils/events.js +44 -0
- package/build/user-event/utils/events.js.map +1 -0
- package/build/user-event/utils/index.d.ts +2 -0
- package/build/user-event/utils/index.js +28 -0
- package/build/user-event/utils/index.js.map +1 -0
- package/build/user-event/utils/wait.d.ts +2 -0
- package/build/user-event/utils/wait.js +14 -0
- package/build/user-event/utils/wait.js.map +1 -0
- package/build/waitFor.js +3 -3
- package/build/waitFor.js.map +1 -1
- package/build/waitForElementToBeRemoved.js.map +1 -1
- package/examples/basic/.expo-shared/assets.json +4 -0
- package/examples/basic/.gitignore +14 -0
- package/examples/basic/App.tsx +20 -0
- package/examples/basic/README.md +11 -0
- package/examples/basic/__tests__/App.test.tsx +119 -0
- package/examples/basic/app.json +31 -0
- package/examples/basic/assets/adaptive-icon.png +0 -0
- package/examples/basic/assets/favicon.png +0 -0
- package/examples/basic/assets/icon.png +0 -0
- package/examples/basic/assets/splash.png +0 -0
- package/examples/basic/babel.config.js +6 -0
- package/examples/basic/components/Home.tsx +28 -0
- package/examples/basic/components/LoginForm.tsx +138 -0
- package/examples/basic/jest-setup.ts +7 -0
- package/examples/basic/jest.config.js +5 -0
- package/examples/basic/package.json +30 -0
- package/examples/basic/tsconfig.json +7 -0
- package/examples/react-navigation/README.md +14 -0
- package/examples/react-navigation/babel.config.js +4 -0
- package/examples/react-navigation/jest-setup.js +11 -0
- package/examples/react-navigation/jest.config.js +10 -0
- package/examples/react-navigation/package.json +31 -0
- package/examples/react-navigation/src/App.js +21 -0
- package/examples/react-navigation/src/DrawerNavigator.js +15 -0
- package/examples/react-navigation/src/DrawerNavigator.test.js +42 -0
- package/examples/react-navigation/src/NativeStackNavigator.js +15 -0
- package/examples/react-navigation/src/NativeStackNavigator.test.js +34 -0
- package/examples/react-navigation/src/StackNavigator.js +15 -0
- package/examples/react-navigation/src/StackNavigator.test.js +34 -0
- package/examples/react-navigation/src/TabNavigator.js +15 -0
- package/examples/react-navigation/src/TabNavigator.test.js +21 -0
- package/examples/react-navigation/src/screens/DetailsScreen.js +43 -0
- package/examples/react-navigation/src/screens/DetailsScreen.test.js +27 -0
- package/examples/react-navigation/src/screens/DrawerHomeScreen.js +26 -0
- package/examples/react-navigation/src/screens/HomeScreen.js +48 -0
- package/examples/react-navigation/src/screens/SettingsScreen.js +20 -0
- package/examples/react-navigation/src/test-utils.js +12 -0
- package/examples/redux/App.js +27 -0
- package/examples/redux/actions/todoActions.js +25 -0
- package/examples/redux/babel.config.js +6 -0
- package/examples/redux/components/AddTodo.js +73 -0
- package/examples/redux/components/AddTodo.test.js +27 -0
- package/examples/redux/components/TodoElem.js +25 -0
- package/examples/redux/components/TodoList.js +29 -0
- package/examples/redux/components/TodoList.test.js +34 -0
- package/examples/redux/index.js +8 -0
- package/examples/redux/jest-setup.js +2 -0
- package/examples/redux/jest.config.js +4 -0
- package/examples/redux/package.json +23 -0
- package/examples/redux/reducers/index.js +6 -0
- package/examples/redux/reducers/todoReducer.js +27 -0
- package/examples/redux/store.js +10 -0
- package/examples/redux/test-utils.js +11 -0
- package/experiments-app/.expo/README.md +15 -0
- package/experiments-app/.expo/devices.json +3 -0
- package/experiments-app/.expo/packager-info.json +9 -0
- package/experiments-app/.expo/settings.json +9 -0
- package/experiments-app/.gitignore +17 -0
- package/experiments-app/.prettierrc.js +5 -0
- package/experiments-app/app.json +30 -0
- package/experiments-app/assets/adaptive-icon.png +0 -0
- package/experiments-app/assets/favicon.png +0 -0
- package/experiments-app/assets/icon.png +0 -0
- package/experiments-app/assets/splash.png +0 -0
- package/experiments-app/babel.config.js +6 -0
- package/experiments-app/index.js +4 -0
- package/experiments-app/package.json +31 -0
- package/experiments-app/src/App.tsx +31 -0
- package/experiments-app/src/MainScreen.tsx +51 -0
- package/experiments-app/src/experiments.ts +17 -0
- package/experiments-app/src/screens/TextInputEventPropagation.tsx +54 -0
- package/experiments-app/src/screens/TextInputEvents.tsx +50 -0
- package/experiments-app/src/utils/helpers.ts +8 -0
- package/experiments-app/tsconfig.json +6 -0
- package/experiments-app/yarn.lock +6913 -0
- package/flow-typed/npm/jest_v26.x.x.js +1218 -0
- package/flow-typed/npm/react-test-renderer_v16.x.x.js +81 -0
- package/jest-setup.ts +10 -0
- package/package.json +6 -6
- package/renovate.json +19 -0
- package/scripts/test_react_17 +12 -0
- package/src/__tests__/__snapshots__/render-debug.test.tsx.snap +548 -0
- package/src/__tests__/__snapshots__/render.test.tsx.snap +39 -0
- package/src/__tests__/act.test.tsx +52 -0
- package/src/__tests__/auto-cleanup-skip.test.tsx +39 -0
- package/src/__tests__/auto-cleanup.test.tsx +50 -0
- package/src/__tests__/cleanup.test.tsx +26 -0
- package/src/__tests__/config.test.ts +55 -0
- package/src/__tests__/fireEvent-textInput.test.tsx +154 -0
- package/src/__tests__/fireEvent.test.tsx +485 -0
- package/src/__tests__/host-component-names.test.tsx +109 -0
- package/src/__tests__/host-text-nesting.test.tsx +90 -0
- package/src/__tests__/jest-native.test.tsx +84 -0
- package/src/__tests__/questionsBoard.test.tsx +62 -0
- package/src/__tests__/react-native-api.test.tsx +126 -0
- package/src/__tests__/render-debug.test.tsx +207 -0
- package/src/__tests__/render-stringValidation.test.tsx +157 -0
- package/src/__tests__/render.test.tsx +256 -0
- package/src/__tests__/renderHook.test.tsx +114 -0
- package/src/__tests__/screen.test.tsx +66 -0
- package/src/__tests__/timerUtils.ts +7 -0
- package/src/__tests__/timers.test.ts +27 -0
- package/src/__tests__/waitFor.test.tsx +327 -0
- package/src/__tests__/waitForElementToBeRemoved.test.tsx +151 -0
- package/src/__tests__/within.test.tsx +96 -0
- package/src/act.ts +86 -0
- package/src/cleanup.ts +15 -0
- package/src/config.ts +72 -0
- package/src/fireEvent.ts +163 -0
- package/src/flush-micro-tasks.ts +30 -0
- package/src/helpers/__tests__/accessiblity.test.tsx +373 -0
- package/src/helpers/__tests__/component-tree.test.tsx +226 -0
- package/src/helpers/__tests__/format-default.tsx +114 -0
- package/src/helpers/__tests__/getTextContent.test.tsx +49 -0
- package/src/helpers/__tests__/includeHiddenElements.test.tsx +39 -0
- package/src/helpers/__tests__/query-name.test.ts +10 -0
- package/src/helpers/__tests__/timers.test.ts +8 -0
- package/src/helpers/accessiblity.ts +108 -0
- package/src/helpers/component-tree.ts +89 -0
- package/src/helpers/debugDeep.ts +27 -0
- package/src/helpers/debugShallow.ts +22 -0
- package/src/helpers/deprecation.ts +53 -0
- package/src/helpers/errors.ts +66 -0
- package/src/helpers/filterNodeByType.ts +7 -0
- package/src/helpers/findAll.ts +68 -0
- package/src/helpers/format-default.ts +72 -0
- package/src/helpers/format.ts +47 -0
- package/src/helpers/getTextContent.ts +20 -0
- package/src/helpers/host-component-names.tsx +67 -0
- package/src/helpers/matchers/__tests__/matchArrayValue.test.ts +34 -0
- package/src/helpers/matchers/__tests__/matchObject.test.ts +37 -0
- package/src/helpers/matchers/__tests__/matchStringValue.test.ts +15 -0
- package/src/helpers/matchers/accessibilityState.ts +48 -0
- package/src/helpers/matchers/accessibilityValue.ts +24 -0
- package/src/helpers/matchers/matchArrayProp.ts +21 -0
- package/src/helpers/matchers/matchLabelText.ts +51 -0
- package/src/helpers/matchers/matchObjectProp.ts +25 -0
- package/src/helpers/matchers/matchStringProp.ts +23 -0
- package/src/helpers/matchers/matchTextContent.ts +20 -0
- package/src/helpers/pointer-events.ts +27 -0
- package/src/helpers/query-name.ts +4 -0
- package/src/helpers/stringValidation.ts +36 -0
- package/src/helpers/timers.ts +98 -0
- package/src/index.ts +33 -0
- package/src/matches.ts +49 -0
- package/src/pure.ts +25 -0
- package/src/queries/__tests__/a11yState.test.tsx +439 -0
- package/src/queries/__tests__/a11yValue.test.tsx +309 -0
- package/src/queries/__tests__/displayValue.test.tsx +221 -0
- package/src/queries/__tests__/hintText.test.tsx +177 -0
- package/src/queries/__tests__/labelText.test.tsx +242 -0
- package/src/queries/__tests__/makeQueries.test.tsx +235 -0
- package/src/queries/__tests__/placeholderText.test.tsx +136 -0
- package/src/queries/__tests__/role-value.test.tsx +176 -0
- package/src/queries/__tests__/role.test.tsx +824 -0
- package/src/queries/__tests__/testId.test.tsx +200 -0
- package/src/queries/__tests__/text.test.tsx +556 -0
- package/src/queries/a11yState.ts +131 -0
- package/src/queries/a11yValue.ts +131 -0
- package/src/queries/displayValue.ts +78 -0
- package/src/queries/hintText.ts +112 -0
- package/src/queries/labelText.ts +59 -0
- package/src/queries/makeQueries.ts +255 -0
- package/src/queries/options.ts +14 -0
- package/src/queries/placeholderText.ts +79 -0
- package/src/queries/role.ts +132 -0
- package/src/queries/testId.ts +71 -0
- package/src/queries/text.ts +66 -0
- package/src/queries/unsafeProps.ts +76 -0
- package/src/queries/unsafeType.ts +73 -0
- package/src/react-versions.ts +11 -0
- package/src/render-act.ts +19 -0
- package/src/render.tsx +183 -0
- package/src/renderHook.tsx +56 -0
- package/src/screen.ts +123 -0
- package/src/shallow.ts +18 -0
- package/src/test-utils/events.ts +24 -0
- package/src/test-utils/index.ts +1 -0
- package/src/user-event/event-builder/common.ts +50 -0
- package/src/user-event/event-builder/index.ts +5 -0
- package/src/user-event/index.ts +14 -0
- package/src/user-event/press/__tests__/longPress.real-timers.test.tsx +115 -0
- package/src/user-event/press/__tests__/longPress.test.tsx +157 -0
- package/src/user-event/press/__tests__/press.real-timers.test.tsx +318 -0
- package/src/user-event/press/__tests__/press.test.tsx +422 -0
- package/src/user-event/press/constants.ts +7 -0
- package/src/user-event/press/index.ts +1 -0
- package/src/user-event/press/press.ts +134 -0
- package/src/user-event/press/utils/warnAboutRealTimers.ts +6 -0
- package/src/user-event/setup/index.ts +2 -0
- package/src/user-event/setup/setup.ts +93 -0
- package/src/user-event/type/__tests__/__snapshots__/type.test.tsx.snap +26 -0
- package/src/user-event/type/__tests__/type.test.tsx +63 -0
- package/src/user-event/type/index.ts +1 -0
- package/src/user-event/type/type.ts +20 -0
- package/src/user-event/utils/__tests__/wait.test.ts +63 -0
- package/src/user-event/utils/events.ts +54 -0
- package/src/user-event/utils/index.ts +2 -0
- package/src/user-event/utils/wait.ts +15 -0
- package/src/waitFor.ts +228 -0
- package/src/waitForElementToBeRemoved.ts +42 -0
- package/src/within.ts +30 -0
- package/tsconfig.json +17 -0
- package/tsconfig.release.json +8 -0
- package/website/.gitignore +20 -0
- package/website/README.md +33 -0
- package/website/docker/.dockerignore +3 -0
- package/website/docker/Dockerfile +9 -0
- package/website/docker/docker-compose.yml +11 -0
- package/website/docs/API.md +946 -0
- package/website/docs/EslintPLluginTestingLibrary.md +28 -0
- package/website/docs/FAQ.md +44 -0
- package/website/docs/GettingStarted.md +100 -0
- package/website/docs/HowShouldIQuery.md +21 -0
- package/website/docs/MigrationV11.md +64 -0
- package/website/docs/MigrationV12.md +67 -0
- package/website/docs/MigrationV2.md +126 -0
- package/website/docs/MigrationV7.md +119 -0
- package/website/docs/MigrationV9.md +67 -0
- package/website/docs/Queries.md +567 -0
- package/website/docs/ReactNavigation.md +371 -0
- package/website/docs/ReduxIntegration.md +137 -0
- package/website/docs/TestingEnvironment.md +154 -0
- package/website/docs/Troubleshooting.md +44 -0
- package/website/docs/UnderstandingAct.md +227 -0
- package/website/docs/UserEvent.md +66 -0
- package/website/docusaurus.config.js +114 -0
- package/website/package.json +31 -0
- package/website/sidebars.js +20 -0
- package/website/src/components/Feature.js +31 -0
- package/website/src/css/custom.css +13 -0
- package/website/src/css/index.module.css +77 -0
- package/website/src/pages/index.js +82 -0
- package/website/static/.nojekyll +0 -0
- package/website/static/css/custom.css +28 -0
- package/website/static/img/hit.png +0 -0
- package/website/static/img/locomotive.png +0 -0
- package/website/static/img/owl.png +0 -0
- package/website/static/img/tools.png +0 -0
- package/website/yarn.lock +7669 -0
- package/yarn.lock +7765 -0
- package/build/flushMicroTasks.d.ts +0 -5
- package/build/flushMicroTasks.js +0 -17
- package/build/flushMicroTasks.js.map +0 -1
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: react-navigation
|
|
3
|
+
title: React Navigation
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This section deals with integrating `@testing-library/react-native` with `react-navigation`, using Jest.
|
|
7
|
+
|
|
8
|
+
## Stack Navigator
|
|
9
|
+
|
|
10
|
+
### Setting up
|
|
11
|
+
|
|
12
|
+
Install the packages required for React Navigation. For this example, we will use a [stack navigator](https://reactnavigation.org/docs/stack-navigator/) to transition to the second page when any of the items are clicked on.
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
$ yarn add @react-native-community/masked-view @react-navigation/native @react-navigation/stack react-native-gesture-handler react-native-reanimated react-native-safe-area-context react-native-screens
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Create an [`./AppNavigator.js`](https://github.com/callstack/react-native-testing-library/blob/main/examples/reactnavigation/src/AppNavigator.js) component which will list the navigation stack:
|
|
19
|
+
|
|
20
|
+
```jsx
|
|
21
|
+
import 'react-native-gesture-handler';
|
|
22
|
+
import * as React from 'react';
|
|
23
|
+
import { createStackNavigator } from '@react-navigation/stack';
|
|
24
|
+
|
|
25
|
+
import HomeScreen from './screens/HomeScreen';
|
|
26
|
+
import DetailsScreen from './screens/DetailsScreen';
|
|
27
|
+
|
|
28
|
+
const { Screen, Navigator } = createStackNavigator();
|
|
29
|
+
|
|
30
|
+
export default function Navigation() {
|
|
31
|
+
const options = {};
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<Navigator>
|
|
35
|
+
<Screen name="Home" component={HomeScreen} />
|
|
36
|
+
<Screen options={options} name="Details" component={DetailsScreen} />
|
|
37
|
+
</Navigator>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Create your two screens which we will transition to and from them. The homescreen, found in [`./screens/HomeScreen.js`](https://github.com/callstack/react-native-testing-library/blob/main/examples/reactnavigation/src/screens/HomeScreen.js), contains a list of elements presented in a list view. On tap of any of these items will move to the details screen with the item number:
|
|
43
|
+
|
|
44
|
+
```jsx
|
|
45
|
+
import * as React from 'react';
|
|
46
|
+
import {
|
|
47
|
+
Text,
|
|
48
|
+
View,
|
|
49
|
+
FlatList,
|
|
50
|
+
TouchableOpacity,
|
|
51
|
+
StyleSheet,
|
|
52
|
+
} from 'react-native';
|
|
53
|
+
|
|
54
|
+
export default function HomeScreen({ navigation }) {
|
|
55
|
+
const [items] = React.useState(
|
|
56
|
+
new Array(20).fill(null).map((_, idx) => idx + 1)
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const onOpacityPress = (item) => navigation.navigate('Details', item);
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<View>
|
|
63
|
+
<Text style={styles.header}>List of numbers from 1 to 20</Text>
|
|
64
|
+
<FlatList
|
|
65
|
+
keyExtractor={(_, idx) => `${idx}`}
|
|
66
|
+
data={items}
|
|
67
|
+
renderItem={({ item }) => (
|
|
68
|
+
<TouchableOpacity
|
|
69
|
+
onPress={() => onOpacityPress(item)}
|
|
70
|
+
style={styles.row}
|
|
71
|
+
>
|
|
72
|
+
<Text>Item number {item}</Text>
|
|
73
|
+
</TouchableOpacity>
|
|
74
|
+
)}
|
|
75
|
+
/>
|
|
76
|
+
</View>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const divider = '#DDDDDD';
|
|
81
|
+
|
|
82
|
+
const styles = StyleSheet.create({
|
|
83
|
+
header: {
|
|
84
|
+
fontSize: 20,
|
|
85
|
+
textAlign: 'center',
|
|
86
|
+
marginVertical: 16,
|
|
87
|
+
},
|
|
88
|
+
row: {
|
|
89
|
+
paddingVertical: 16,
|
|
90
|
+
paddingHorizontal: 24,
|
|
91
|
+
borderBottomColor: divider,
|
|
92
|
+
borderBottomWidth: 1,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The details screen, found in [`./screens/DetailsScreen.js`](https://github.com/callstack/react-native-testing-library/blob/main/examples/reactnavigation/src/screens/DetailsScreen.js), contains a header with the item number passed from the home screen:
|
|
98
|
+
|
|
99
|
+
```jsx
|
|
100
|
+
// ./screens/DetailsScreen.js
|
|
101
|
+
import * as React from 'react';
|
|
102
|
+
import { Text, StyleSheet, View } from 'react-native';
|
|
103
|
+
|
|
104
|
+
export default function DetailsScreen(props) {
|
|
105
|
+
const item = Number.parseInt(props.route.params, 10);
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<View>
|
|
109
|
+
<Text style={styles.header}>Showing details for {item}</Text>
|
|
110
|
+
<Text style={styles.body}>the number you have chosen is {item}</Text>
|
|
111
|
+
</View>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const styles = StyleSheet.create({
|
|
116
|
+
header: {
|
|
117
|
+
fontSize: 20,
|
|
118
|
+
textAlign: 'center',
|
|
119
|
+
marginVertical: 16,
|
|
120
|
+
},
|
|
121
|
+
body: {
|
|
122
|
+
textAlign: 'center',
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Setting up the test environment
|
|
128
|
+
|
|
129
|
+
Install required dev dependencies:
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
$ yarn add -D jest @testing-library/react-native
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Create your `jest.config.js` file (or place the following properties in your `package.json` as a "jest" property)
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
module.exports = {
|
|
139
|
+
preset: 'react-native',
|
|
140
|
+
setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'],
|
|
141
|
+
transformIgnorePatterns: [
|
|
142
|
+
'node_modules/(?!(jest-)?@?react-native|@react-native-community|@react-navigation)',
|
|
143
|
+
|
|
144
|
+
// For pnpm you need to use inlcude `(?!(?:.pnpm/)?` part like this:
|
|
145
|
+
// 'node_modules/(?!(?:.pnpm/)?((jest-)?@?react-native|@react-native-community|@react-navigation))',
|
|
146
|
+
],
|
|
147
|
+
};
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Notice the 2 entries that don't come with the default React Native project:
|
|
151
|
+
|
|
152
|
+
- `setupFiles` – an array of files that Jest is going to execute before running your tests. In this case, we run `react-native-gesture-handler/jestSetup.js` which sets up necessary mocks for `react-native-gesture-handler` native module
|
|
153
|
+
- `transformIgnorePatterns` – an array of paths that Jest ignores when transforming code. In this case, the negative lookahead regular expression is used, to tell Jest to transform (with Babel) every package inside `node_modules/` that starts with `react-native`, `@react-native-community` or `@react-navigation` (added by us, the rest is in `react-native` preset by default, so you don't have to worry about it).
|
|
154
|
+
|
|
155
|
+
### Example tests
|
|
156
|
+
|
|
157
|
+
For this example, we are going to test out two things. The first thing is that the page is laid out as expected. The second, and most important, is that the page will transition to the detail screen when any item is tapped on.
|
|
158
|
+
|
|
159
|
+
Let's add a [`AppNavigator.test.js`](https://github.com/callstack/react-native-testing-library/blob/main/examples/reactnavigation/src/__tests__/AppNavigator.js) file in `src/__tests__` directory:
|
|
160
|
+
|
|
161
|
+
```jsx
|
|
162
|
+
import * as React from 'react';
|
|
163
|
+
import { NavigationContainer } from '@react-navigation/native';
|
|
164
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
165
|
+
|
|
166
|
+
import AppNavigator from '../AppNavigator';
|
|
167
|
+
|
|
168
|
+
// Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
|
|
169
|
+
// Use with React Native <= 0.63
|
|
170
|
+
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
|
|
171
|
+
|
|
172
|
+
// Use this instead with React Native >= 0.64
|
|
173
|
+
// jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
|
|
174
|
+
|
|
175
|
+
describe('Testing react navigation', () => {
|
|
176
|
+
test('page contains the header and 10 items', async () => {
|
|
177
|
+
const component = (
|
|
178
|
+
<NavigationContainer>
|
|
179
|
+
<AppNavigator />
|
|
180
|
+
</NavigationContainer>
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
render(component);
|
|
184
|
+
|
|
185
|
+
const header = await screen.findByText('List of numbers from 1 to 20');
|
|
186
|
+
const items = await screen.findAllByText(/Item number/);
|
|
187
|
+
|
|
188
|
+
expect(header).toBeOnTheScreen();
|
|
189
|
+
expect(items.length).toBe(10);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('clicking on one item takes you to the details screen', async () => {
|
|
193
|
+
const component = (
|
|
194
|
+
<NavigationContainer>
|
|
195
|
+
<AppNavigator />
|
|
196
|
+
</NavigationContainer>
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
render(component);
|
|
200
|
+
const toClick = await screen.findByText('Item number 5');
|
|
201
|
+
|
|
202
|
+
fireEvent(toClick, 'press');
|
|
203
|
+
const newHeader = await screen.findByText('Showing details for 5');
|
|
204
|
+
const newBody = await screen.findByText('the number you have chosen is 5');
|
|
205
|
+
|
|
206
|
+
expect(newHeader).toBeOnTheScreen();
|
|
207
|
+
expect(newBody).toBeOnTheScreen();
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Drawer Navigator
|
|
213
|
+
|
|
214
|
+
Testing the Drawer Navigation requires an additional setup step for mocking the Reanimated library.
|
|
215
|
+
|
|
216
|
+
### Setting up
|
|
217
|
+
|
|
218
|
+
Install the packages required for React Navigation. For this example, we will use a [drawer navigator](https://reactnavigation.org/docs/drawer-navigator/) to transition between a home screen and an additional screen.
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
$ yarn add @react-native-community/masked-view @react-navigation/native @react-navigation/drawer react-native-gesture-handler react-native-reanimated react-native-safe-area-context react-native-screens
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Create a [`./DrawerAppNavigator.js`](https://github.com/callstack/react-native-testing-library/blob/main/examples/reactnavigation/src/DrawerAppNavigator.js) component which will list the navigation stack:
|
|
225
|
+
|
|
226
|
+
```jsx
|
|
227
|
+
import 'react-native-gesture-handler';
|
|
228
|
+
import React from 'react';
|
|
229
|
+
import { createDrawerNavigator } from '@react-navigation/drawer';
|
|
230
|
+
|
|
231
|
+
const { Screen, Navigator } = createDrawerNavigator();
|
|
232
|
+
|
|
233
|
+
export default function Navigation() {
|
|
234
|
+
return (
|
|
235
|
+
<Navigator>
|
|
236
|
+
<Screen name="Home" component={HomeScreen} />
|
|
237
|
+
<Screen name="Notifications" component={NotificationsScreen} />
|
|
238
|
+
</Navigator>
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Create your two screens which we will transition to and from:
|
|
244
|
+
|
|
245
|
+
```jsx
|
|
246
|
+
function HomeScreen({ navigation }) {
|
|
247
|
+
return (
|
|
248
|
+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
|
|
249
|
+
<Text>Welcome!</Text>
|
|
250
|
+
<Button
|
|
251
|
+
onPress={() => navigation.navigate('Notifications')}
|
|
252
|
+
title="Go to notifications"
|
|
253
|
+
/>
|
|
254
|
+
</View>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function NotificationsScreen({ navigation }) {
|
|
259
|
+
return (
|
|
260
|
+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
|
|
261
|
+
<Text>This is the notifications screen</Text>
|
|
262
|
+
<Button onPress={() => navigation.goBack()} title="Go back home" />
|
|
263
|
+
</View>
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Setting up the test environment
|
|
269
|
+
|
|
270
|
+
Install required dev dependencies:
|
|
271
|
+
|
|
272
|
+
```
|
|
273
|
+
$ yarn add -D jest @testing-library/react-native
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
Create a [`mock file`](https://github.com/callstack/react-native-testing-library/blob/main/examples/reactnavigation/jest-setup.js) necessary for your tests:
|
|
277
|
+
|
|
278
|
+
```jsx
|
|
279
|
+
import 'react-native-gesture-handler/jestSetup';
|
|
280
|
+
|
|
281
|
+
jest.mock('react-native-reanimated', () => {
|
|
282
|
+
const Reanimated = require('react-native-reanimated/mock');
|
|
283
|
+
|
|
284
|
+
// The mock for `call` immediately calls the callback which is incorrect
|
|
285
|
+
// So we override it with a no-op
|
|
286
|
+
Reanimated.default.call = () => {};
|
|
287
|
+
|
|
288
|
+
return Reanimated;
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
|
|
292
|
+
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
Create your `jest.config.js` file (or place the following properties in your `package.json` as a "jest" property)
|
|
296
|
+
|
|
297
|
+
```js
|
|
298
|
+
module.exports = {
|
|
299
|
+
preset: 'react-native',
|
|
300
|
+
setupFiles: ['./jest-setup.js'],
|
|
301
|
+
transformIgnorePatterns: [
|
|
302
|
+
'node_modules/(?!(jest-)?react-native|@react-native-community|@react-navigation)',
|
|
303
|
+
],
|
|
304
|
+
};
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
Make sure that the path to the file in `setupFiles` is correct. Jest will run these files before running your tests, so it's the best place to put your global mocks.
|
|
308
|
+
|
|
309
|
+
This setup is copied from the [React Navigation documentation](https://reactnavigation.org/docs/testing/).
|
|
310
|
+
|
|
311
|
+
### Example tests
|
|
312
|
+
|
|
313
|
+
For this example, we are going to test out two things. The first thing is that the screen is loaded correctly. The second, and most important, is that the page will transition to the notifications screen when the button is tapped on.
|
|
314
|
+
|
|
315
|
+
Let's add a [`DrawerAppNavigator.test.js`](https://github.com/callstack/react-native-testing-library/blob/main/examples/reactnavigation/src/__tests__/DrawerAppNavigator.js) file in `src/__tests__` directory:
|
|
316
|
+
|
|
317
|
+
```jsx
|
|
318
|
+
import React from 'react';
|
|
319
|
+
import { NavigationContainer } from '@react-navigation/native';
|
|
320
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
321
|
+
|
|
322
|
+
import DrawerAppNavigator from '../DrawerAppNavigator';
|
|
323
|
+
|
|
324
|
+
describe('Testing react navigation', () => {
|
|
325
|
+
test('screen contains a button linking to the notifications page', async () => {
|
|
326
|
+
const component = (
|
|
327
|
+
<NavigationContainer>
|
|
328
|
+
<DrawerAppNavigator />
|
|
329
|
+
</NavigationContainer>
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
render(component);
|
|
333
|
+
const button = await screen.findByText('Go to notifications');
|
|
334
|
+
|
|
335
|
+
expect(button).toBeOnTheScreen();
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
test('clicking on the button takes you to the notifications screen', async () => {
|
|
339
|
+
const component = (
|
|
340
|
+
<NavigationContainer>
|
|
341
|
+
<DrawerAppNavigator />
|
|
342
|
+
</NavigationContainer>
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
render(component);
|
|
346
|
+
const oldScreen = screen.queryByText('Welcome!');
|
|
347
|
+
const button = await screen.findByText('Go to notifications');
|
|
348
|
+
|
|
349
|
+
expect(oldScreen).toBeOnTheScreen();
|
|
350
|
+
|
|
351
|
+
fireEvent(button, 'press');
|
|
352
|
+
const newScreen = await screen.findByText('This is the notifications screen');
|
|
353
|
+
|
|
354
|
+
expect(newScreen).toBeOnTheScreen();
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
## Running tests
|
|
360
|
+
|
|
361
|
+
To run the tests, place a test script inside your `package.json`
|
|
362
|
+
|
|
363
|
+
```json
|
|
364
|
+
{
|
|
365
|
+
"scripts": {
|
|
366
|
+
"test": "jest"
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
And run the `test` script with `npm test` or `yarn test`.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: redux-integration
|
|
3
|
+
title: Redux Integration
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This section deals with testing RN applications developed with Redux. We will be developing a simple TODO application capable of adding and removing an item. Once included, the timestamp is included.
|
|
7
|
+
|
|
8
|
+
## Setting up
|
|
9
|
+
|
|
10
|
+
An example of setting up can be found [here](https://github.com/callstack/react-native-testing-library/tree/main/examples/redux).
|
|
11
|
+
|
|
12
|
+
## Test cases
|
|
13
|
+
|
|
14
|
+
Our test is on the components that either dispatch actions on the redux store or read some data from the redux store. This means we will test `./components/AddTodo.js` and `./components/TodoList.js`. Thus we will create `./components/AddTodo.test.js` and `./components/TodoList.test.js`
|
|
15
|
+
|
|
16
|
+
For `./components/AddTodo.test.js`
|
|
17
|
+
|
|
18
|
+
```jsx
|
|
19
|
+
import * as React from 'react';
|
|
20
|
+
import { Provider } from 'react-redux';
|
|
21
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
22
|
+
import configureStore from '../store';
|
|
23
|
+
import AddTodo from './AddTodo';
|
|
24
|
+
|
|
25
|
+
describe('AddTodo component test', () => {
|
|
26
|
+
test('adds a new TODO when the button is pressed', () => {
|
|
27
|
+
const store = configureStore();
|
|
28
|
+
|
|
29
|
+
const component = (
|
|
30
|
+
<Provider store={store}>
|
|
31
|
+
<AddTodo />
|
|
32
|
+
</Provider>
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
render(component);
|
|
36
|
+
|
|
37
|
+
// There is a TextInput.
|
|
38
|
+
// https://github.com/callstack/react-native-testing-library/blob/ae3d4af370487e1e8fedd8219f77225690aefc59/examples/redux/components/AddTodo.js#L24
|
|
39
|
+
const input = screen.getByPlaceholderText(/repository/i);
|
|
40
|
+
expect(input).toBeOnTheScreen();
|
|
41
|
+
|
|
42
|
+
const textToEnter = 'This is a random element';
|
|
43
|
+
fireEvent.changeText(input, textToEnter);
|
|
44
|
+
fireEvent.press(screen.getByText('Submit form'));
|
|
45
|
+
|
|
46
|
+
const todosState = store.getState().todos;
|
|
47
|
+
|
|
48
|
+
expect(todosState.length).toEqual(1);
|
|
49
|
+
|
|
50
|
+
expect(todosState).toEqual(
|
|
51
|
+
expect.arrayContaining([
|
|
52
|
+
expect.objectContaining({
|
|
53
|
+
id: 1,
|
|
54
|
+
text: textToEnter,
|
|
55
|
+
date: expect.any(Date),
|
|
56
|
+
}),
|
|
57
|
+
])
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
For `./components/TodoList.test.js`
|
|
64
|
+
|
|
65
|
+
```jsx
|
|
66
|
+
import * as React from 'react';
|
|
67
|
+
import { Provider } from 'react-redux';
|
|
68
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
69
|
+
import configureStore from '../store';
|
|
70
|
+
import TodoList from './TodoList';
|
|
71
|
+
|
|
72
|
+
describe('TodoList component test', () => {
|
|
73
|
+
test('it should execute with a store with 4 elements', () => {
|
|
74
|
+
const initialState = {
|
|
75
|
+
todos: [
|
|
76
|
+
{ id: 1, text: 'Sing something', date: new Date() },
|
|
77
|
+
{ id: 2, text: 'Dance something', date: new Date() },
|
|
78
|
+
{ id: 3, text: 'Sleep something', date: new Date() },
|
|
79
|
+
{ id: 4, text: 'Sleep something', date: new Date() },
|
|
80
|
+
],
|
|
81
|
+
};
|
|
82
|
+
const store = configureStore(initialState);
|
|
83
|
+
|
|
84
|
+
const component = (
|
|
85
|
+
<Provider store={store}>
|
|
86
|
+
<TodoList />
|
|
87
|
+
</Provider>
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
render(component);
|
|
91
|
+
const todoElems = screen.getAllByText(/something/i);
|
|
92
|
+
|
|
93
|
+
expect(todoElems.length).toEqual(4);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('should execute with 2 elements and end up with 1 after delete', () => {
|
|
97
|
+
const initialState = {
|
|
98
|
+
todos: [
|
|
99
|
+
{ id: 1, text: 'Sing something', date: new Date() },
|
|
100
|
+
{ id: 2, text: 'Dance something', date: new Date() },
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
const store = configureStore(initialState);
|
|
104
|
+
|
|
105
|
+
const component = (
|
|
106
|
+
<Provider store={store}>
|
|
107
|
+
<TodoList />
|
|
108
|
+
</Provider>
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
render(component);
|
|
112
|
+
const todoElems = screen.getAllByText(/something/i);
|
|
113
|
+
|
|
114
|
+
expect(todoElems.length).toBe(2);
|
|
115
|
+
|
|
116
|
+
const buttons = screen.getAllByText('Delete');
|
|
117
|
+
expect(buttons.length).toBe(2);
|
|
118
|
+
|
|
119
|
+
fireEvent.press(buttons[0]);
|
|
120
|
+
expect(screen.getAllByText('Delete').length).toBe(1);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Running tests
|
|
126
|
+
|
|
127
|
+
To run the tests, place a test script inside your package.json
|
|
128
|
+
|
|
129
|
+
```json
|
|
130
|
+
{
|
|
131
|
+
"scripts": {
|
|
132
|
+
"test": "jest"
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
And run the test script with npm test or yarn test.
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: testing-env
|
|
3
|
+
title: Testing Environment
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
:::info
|
|
7
|
+
This document is intended for more advanced audience. You should be able to write integration or component tests without reading this.
|
|
8
|
+
It is intended for people who want to better understand internals of our testing environment, e.g. in order to contribute to the codebase.
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
## Testing Environment
|
|
12
|
+
|
|
13
|
+
React Native Testing Library allows you to write integration and component tests for your React Native app or library. While the JSX code used in tests closely resembles your React Native app, the things are not quite as simple as they might appear. In this document we will describe the key elements of our testing environment and highlight things to be aware of when writing more advanced tests or diagnosing issues.
|
|
14
|
+
|
|
15
|
+
### React renderers
|
|
16
|
+
|
|
17
|
+
React allows you to write declarative code using JSX, write function or class components, or use hooks like `useState`. In order to output the results of your components it needs to work with a renderer. Every React app uses some type of renderer: React Native is a renderer for mobile apps, web apps use React DOM, and there are other more [specialised renderers](https://github.com/chentsulin/awesome-react-renderer) that can e.g. render to console or HTML canvas.
|
|
18
|
+
|
|
19
|
+
When you run your tests in React Native Testing Library, somewhat contrary to what the name suggest, they are actually **not** using React Native renderer. This is because this renderer needs to be run on iOS or Android operating system, so it would need to run on device or simulator.
|
|
20
|
+
|
|
21
|
+
### React Test Renderer
|
|
22
|
+
|
|
23
|
+
Instead, RNTL uses React Test Renderer which is a specialised renderer that allows rendering to pure JavaScript objects without access to mobile OS, and that can run in a Node.js environment using Jest (or any other JavaScript test runner).
|
|
24
|
+
|
|
25
|
+
Using React Test Renderer has pros and cons.
|
|
26
|
+
|
|
27
|
+
Benefits:
|
|
28
|
+
|
|
29
|
+
- tests can run on most CIs (linux, etc) and do not require a mobile device or emulator
|
|
30
|
+
- faster test execution
|
|
31
|
+
- light runtime environment
|
|
32
|
+
|
|
33
|
+
Disadvantages:
|
|
34
|
+
|
|
35
|
+
- Tests do not execute native code
|
|
36
|
+
- Tests are not aware of view state that would be managed by native components, e.g. focus, unmanaged text boxes, etc.
|
|
37
|
+
- Assertions do not operate on native view hierarchy
|
|
38
|
+
- Runtime behaviours are simulated, sometimes imperfectly
|
|
39
|
+
|
|
40
|
+
It’s worth noting that React Testing Library (web one), works a bit different. While RTL also runs in Jest, it also has access to simulated browser DOM environment from jsdom package, so it can use a regular React DOM renderer. Unfortunately, there is no similar React Native runtime environment package. This is probably due to to the fact that while browser environment is well defined and highly standardised, the React Native environment is in constant evolution, in sync with the evolution of underlying OS-es. Maintaining such environment would require duplicating countless React Native behaviours, and keeping that in sync as React Native evolves.
|
|
41
|
+
|
|
42
|
+
### Element tree
|
|
43
|
+
|
|
44
|
+
Invoking `render()` function results in creation of an element tree. This is done internally by invoking `TestRenderer.create()` method. The output tree represents your React Native component tree, each node of that tree is an “instance” of some React component (to be more precise: each node represents a React fiber, and only class components have instances, while function components store the hook state using fiber).
|
|
45
|
+
|
|
46
|
+
These tree elements are represented by `ReactTestInstance` type:
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
interface ReactTestInstance {
|
|
50
|
+
type: ElementType;
|
|
51
|
+
props: { [propName: string]: any };
|
|
52
|
+
parent: ReactTestInstance | null;
|
|
53
|
+
children: Array<ReactTestInstance | string>;
|
|
54
|
+
|
|
55
|
+
// Other props and methods
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Based on: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-test-renderer/index.d.ts
|
|
60
|
+
|
|
61
|
+
### Host and composite components
|
|
62
|
+
|
|
63
|
+
One of the most important aspects of the element tree is that it is composed of both host and composite components:
|
|
64
|
+
|
|
65
|
+
- [Host components](https://reactnative.dev/architecture/glossary#react-host-components-or-host-components) are components that will have direct counterparts in the native view tree. Typical examples are `<View>`, `<Text>` , `<TextInput>`, and `<Image>` from React Native. You can think of these as analogue of `<div>`, `<span>` etc on the Web. You can also create your own host views as native modules or import them from 3rd party libraries, like React Navigation or React Native Gesture Handler.
|
|
66
|
+
- [Composite components](https://reactnative.dev/architecture/glossary#react-composite-components) are React code organisation units that exist only on the JavaScript side of your app. Typical examples are components you create (both function and class components), components imported from React Native (`View`, `Text`, etc) or from 3rd party packages.
|
|
67
|
+
|
|
68
|
+
That might sound a bit confusing at first, since we put React Native’s `View` in both categories. There are actually two `View` components: composite one and host one. The relation between them is as follows:
|
|
69
|
+
|
|
70
|
+
- composite `View` is the type imported from `react-native` package. It’s a JavaScript component, which renders host `View` as its only child in the element tree.
|
|
71
|
+
- host `View` , which you do not render directly. React Native takes the props you pass to the composite `View`, does some processing on them and passes them to host `View`.
|
|
72
|
+
|
|
73
|
+
The part of the tree looks as follows:
|
|
74
|
+
|
|
75
|
+
```jsx
|
|
76
|
+
* <View> (composite)
|
|
77
|
+
* <View> (host)
|
|
78
|
+
* children prop passed in JSX
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Similar relation exists between other composite and host pairs: e.g. `Text` , `TextInput` and `Image` components:
|
|
82
|
+
|
|
83
|
+
```jsx
|
|
84
|
+
* <Text> (composite)
|
|
85
|
+
* <Text> (host)
|
|
86
|
+
* string (or mixed) content
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Not all React Native components are organised this way, e.g. when you use `Pressable` (or `TouchableOpacity`) there is no host `Pressable`, but composite `Pressable` is rendering a host `View` with certain props being set:
|
|
90
|
+
|
|
91
|
+
```jsx
|
|
92
|
+
* <Pressable> (composite)
|
|
93
|
+
* <View accessible={true} {...}> (host)
|
|
94
|
+
* children prop passed in JSX
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Differentiating between host and composite elements
|
|
98
|
+
|
|
99
|
+
Any easy way to differentiate between host and composite elements is the `type` prop of `ReactTestInstance`:
|
|
100
|
+
|
|
101
|
+
- for host components it’s always a string value representing component name, e.g. `"View"`
|
|
102
|
+
- for composite components it’s a function or class corresponding to the component
|
|
103
|
+
|
|
104
|
+
You can use the following code to check if given element is a host one:
|
|
105
|
+
|
|
106
|
+
```jsx
|
|
107
|
+
function isHostElement(element: ReactTestInstance) {
|
|
108
|
+
return typeof element.type === 'string';
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Tree nodes
|
|
113
|
+
|
|
114
|
+
We encourage you to only assert values on host views in your tests, because they represent the user interface view and controls that the user will be able to see and interact with. Users cannot see, or interact with, composite views as they exist purely in JavaScript domain and do not generate any visible UI.
|
|
115
|
+
|
|
116
|
+
#### Asserting props
|
|
117
|
+
|
|
118
|
+
As an example, if you make assertions on a `style` prop of a composite element, there is no guarantee that the style will be visible to the user, as the component author can forget to pass this prop to some underlying `View` or other host component. Similarly `onPress` event handler on a composite prop can be unreachable by the user.
|
|
119
|
+
|
|
120
|
+
```jsx
|
|
121
|
+
function ForgotToPassPropsButton({ title, onPress, style }) {
|
|
122
|
+
return (
|
|
123
|
+
<Pressable>
|
|
124
|
+
<Text>{title}</Text>
|
|
125
|
+
</Pressable>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
In the above example user defined components accepts both `onPress` and `style` props but does not pass it (through `Pressable`) to host views, so these props will not affect the user interface. Additionally, React Native and other libraries might pass some of the props under different names or transform their values between composite and host components.
|
|
131
|
+
|
|
132
|
+
### Tree navigation
|
|
133
|
+
|
|
134
|
+
:::caution
|
|
135
|
+
You should avoid navigating over element tree, as this makes your testing code fragile and may result in false positives. This section is more relevant for people how want to contribute to our codebase.
|
|
136
|
+
:::
|
|
137
|
+
|
|
138
|
+
When navigating a tree of react elements using `parent` or `children` props of a `ReactTestInstance` element, you will encounter both host and composite elements. You should be careful when navigating the element tree, as the tree structure for 3rd party components and change independently from your code and cause unexpected test failures.
|
|
139
|
+
|
|
140
|
+
Inside RNTL we have various tree navigation helpers: `getHostParent`, `getHostChildren`, etc. These are intentionally not exported as using them is not a recommended practice.
|
|
141
|
+
|
|
142
|
+
### Queries
|
|
143
|
+
|
|
144
|
+
Most of the Testing Library queries return host components, in order to encourage best practices described above.
|
|
145
|
+
|
|
146
|
+
At this stage, there are some noteworthy exceptions:
|
|
147
|
+
|
|
148
|
+
- `*ByText` queries returns composite `Text` element
|
|
149
|
+
- `*ByDisplayValue` queries returns composite `TextInput` element
|
|
150
|
+
- `*ByPlaceholderText` queries returns composite `TextInput` element
|
|
151
|
+
|
|
152
|
+
This will change in the near future, as we make efforts for all queries to return host components. Meanwhile it shouldn't be a huge issue, as composite `Text` and `TextInput`generally pass their props down to host counterparts.
|
|
153
|
+
|
|
154
|
+
Additionally, `UNSAFE_*ByType` and `UNSAFE_*ByProps` queries can return both host and composite components depending on used predicates. They are marked as unsafe precisely because testing composite components makes your test more fragile.
|