@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,946 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: api
|
|
3
|
+
title: API
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
### Table of contents:
|
|
7
|
+
|
|
8
|
+
- [`render`](#render)
|
|
9
|
+
- [`render` options](#render-options)
|
|
10
|
+
- [`wrapper` option](#wrapper-option)
|
|
11
|
+
- [`createNodeMock` option](#createnodemock-option)
|
|
12
|
+
- [`unstable_validateStringsRenderedWithinText` option](#unstable_validatestringsrenderedwithintext-option)
|
|
13
|
+
- [`...queries`](#queries)
|
|
14
|
+
- [Example](#example)
|
|
15
|
+
- [`update`](#update)
|
|
16
|
+
- [`unmount`](#unmount)
|
|
17
|
+
- [`debug`](#debug)
|
|
18
|
+
- [`message` option](#message-option)
|
|
19
|
+
- [`mapProps` option](#mapprops-option)
|
|
20
|
+
- [`debug.shallow`](#debugshallow)
|
|
21
|
+
- [`toJSON`](#tojson)
|
|
22
|
+
- [`root`](#root)
|
|
23
|
+
- [`UNSAFE_root`](#unsafe_root)
|
|
24
|
+
- [`screen`](#screen)
|
|
25
|
+
- [`cleanup`](#cleanup)
|
|
26
|
+
- [`fireEvent`](#fireevent)
|
|
27
|
+
- [`fireEvent[eventName]`](#fireeventeventname)
|
|
28
|
+
- [`fireEvent.press`](#fireeventpress)
|
|
29
|
+
- [`fireEvent.changeText`](#fireeventchangetext)
|
|
30
|
+
- [`fireEvent.scroll`](#fireeventscroll)
|
|
31
|
+
- [On a `ScrollView`](#on-a-scrollview)
|
|
32
|
+
- [On a `FlatList`](#on-a-flatlist)
|
|
33
|
+
- [`waitFor`](#waitfor)
|
|
34
|
+
- [Using Jest fake timers](#using-jest-fake-timers)
|
|
35
|
+
- [`waitForElementToBeRemoved`](#waitforelementtoberemoved)
|
|
36
|
+
- [`within`, `getQueriesForElement`](#within-getqueriesforelement)
|
|
37
|
+
- [`queryBy*` APIs](#queryby-apis)
|
|
38
|
+
- [`queryAll*` APIs](#queryall-apis)
|
|
39
|
+
- [`act`](#act)
|
|
40
|
+
- [`renderHook`](#renderhook)
|
|
41
|
+
- [`callback`](#callback)
|
|
42
|
+
- [`options` (Optional)](#options-optional)
|
|
43
|
+
- [`initialProps`](#initialprops)
|
|
44
|
+
- [`wrapper`](#wrapper)
|
|
45
|
+
- [`RenderHookResult` object](#renderhookresult-object)
|
|
46
|
+
- [`result`](#result)
|
|
47
|
+
- [`rerender`](#rerender)
|
|
48
|
+
- [`unmount`](#unmount-1)
|
|
49
|
+
- [Examples](#examples)
|
|
50
|
+
- [With `initialProps`](#with-initialprops)
|
|
51
|
+
- [With `wrapper`](#with-wrapper)
|
|
52
|
+
- [Configuration](#configuration)
|
|
53
|
+
- [`configure`](#configure)
|
|
54
|
+
- [`asyncUtilTimeout` option](#asyncutiltimeout-option)
|
|
55
|
+
- [`defaultIncludeHiddenElements` option](#defaultincludehiddenelements-option)
|
|
56
|
+
- [`defaultDebugOptions` option](#defaultdebugoptions-option)
|
|
57
|
+
- [`resetToDefaults()`](#resettodefaults)
|
|
58
|
+
- [Environment variables](#environment-variables)
|
|
59
|
+
- [`RNTL_SKIP_AUTO_CLEANUP`](#rntl_skip_auto_cleanup)
|
|
60
|
+
- [`RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS`](#rntl_skip_auto_detect_fake_timers)
|
|
61
|
+
- [Accessibility](#accessibility)
|
|
62
|
+
- [`isHiddenFromAccessibility`](#ishiddenfromaccessibility)
|
|
63
|
+
|
|
64
|
+
This page gathers public API of React Native Testing Library along with usage examples.
|
|
65
|
+
|
|
66
|
+
## `render`
|
|
67
|
+
|
|
68
|
+
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/main/src/__tests__/render.test.tsx)
|
|
69
|
+
|
|
70
|
+
Defined as:
|
|
71
|
+
|
|
72
|
+
```jsx
|
|
73
|
+
function render(
|
|
74
|
+
component: React.Element<any>,
|
|
75
|
+
options?: RenderOptions
|
|
76
|
+
): RenderResult {}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Deeply renders given React element and returns helpers to query the output components structure.
|
|
80
|
+
|
|
81
|
+
```jsx
|
|
82
|
+
import { render } from '@testing-library/react-native';
|
|
83
|
+
import { QuestionsBoard } from '../QuestionsBoard';
|
|
84
|
+
|
|
85
|
+
test('should verify two questions', () => {
|
|
86
|
+
render(<QuestionsBoard {...props} />);
|
|
87
|
+
const allQuestions = screen.queryAllByRole('header');
|
|
88
|
+
|
|
89
|
+
expect(allQuestions).toHaveLength(2);
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
> When using React context providers, like Redux Provider, you'll likely want to wrap rendered component with them. In such cases it's convenient to create your custom `render` method. [Follow this great guide on how to set this up](https://testing-library.com/docs/react-testing-library/setup#custom-render).
|
|
94
|
+
|
|
95
|
+
The `render` method returns a `RenderResult` object having properties described below.
|
|
96
|
+
|
|
97
|
+
:::info
|
|
98
|
+
Latest `render` result is kept in [`screen`](#screen) variable that can be imported from `@testing-library/react-native` package.
|
|
99
|
+
|
|
100
|
+
Using `screen` instead of destructuring `render` result is recommended approach. See [this article](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-screen) from Kent C. Dodds for more details.
|
|
101
|
+
:::
|
|
102
|
+
|
|
103
|
+
### `render` options
|
|
104
|
+
|
|
105
|
+
The behavior of `render` method can be customized by passing various options as a second argument of `RenderOptions` type:
|
|
106
|
+
|
|
107
|
+
#### `wrapper` option
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
wrapper?: React.ComponentType<any>,
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
This options allows you to wrap tested component, passed as the first option to the `render()` function, in additional wrapper component. This is most useful for creating reusable custom render functions for common React Context providers.
|
|
114
|
+
|
|
115
|
+
#### `createNodeMock` option
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
createNodeMock?: (element: React.Element<any>) => any,
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
This options allows you to pass `createNodeMock` option to `ReactTestRenderer.create()` method in order to allow for custom mock refs. You can learn more about this options from [React Test Renderer documentation](https://reactjs.org/docs/test-renderer.html#ideas).
|
|
122
|
+
|
|
123
|
+
#### `unstable_validateStringsRenderedWithinText` option
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
unstable_validateStringsRenderedWithinText?: boolean;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
:::note
|
|
130
|
+
This options is experimental, in some cases it might not work as intended, and its behavior might change without observing [SemVer](https://semver.org/) requirements for breaking changes.
|
|
131
|
+
:::
|
|
132
|
+
|
|
133
|
+
This **experimental** option allows you to replicate React Native behavior of throwing `Invariant Violation: Text strings must be rendered within a <Text> component` error when you try to render `string` value under components different than `<Text>`, e.g. under `<View>`.
|
|
134
|
+
|
|
135
|
+
This check is not enforced by React Test Renderer and hence by default React Native Testing Library also does not check this. That might result in runtime errors when running your code on a device, while the code works without errors in tests.
|
|
136
|
+
|
|
137
|
+
### `...queries`
|
|
138
|
+
|
|
139
|
+
The most important feature of `render` is providing a set of helpful queries that allow you to find certain elements in the view hierarchy.
|
|
140
|
+
|
|
141
|
+
See [Queries](./Queries.md) for a complete list.
|
|
142
|
+
|
|
143
|
+
#### Example
|
|
144
|
+
|
|
145
|
+
```jsx
|
|
146
|
+
import { render } from '@testing-library/react-native';
|
|
147
|
+
|
|
148
|
+
const { getByText, queryByA11yState } = render(<Component />);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### `update`
|
|
152
|
+
|
|
153
|
+
_Also available under `rerender` alias_
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
update(element: React.Element<any>): void
|
|
157
|
+
rerender(element: React.Element<any>): void
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Re-render the in-memory tree with a new root element. This simulates a React update at the root. If the new element has the same type and key as the previous element, the tree will be updated; otherwise, it will re-mount a new tree. This is useful when testing for `componentDidUpdate` behavior, by passing updated props to the component.
|
|
161
|
+
|
|
162
|
+
[Example code](https://github.com/callstack/react-native-testing-library/blob/f96d782d26dd4815dbfd01de6ef7a647efd1f693/src/__tests__/act.test.js#L31-L37)
|
|
163
|
+
|
|
164
|
+
### `unmount`
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
unmount(): void
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Unmount the in-memory tree, triggering the appropriate lifecycle events.
|
|
171
|
+
|
|
172
|
+
:::note
|
|
173
|
+
Usually you should not need to call `unmount` as it is done automatically if your test runner supports `afterEach` hook (like Jest, mocha, Jasmine).
|
|
174
|
+
:::
|
|
175
|
+
|
|
176
|
+
### `debug`
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
interface DebugOptions {
|
|
180
|
+
message?: string;
|
|
181
|
+
mapProps?: MapPropsFunction;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
debug(options?: DebugOptions | string): void
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Pretty prints deeply rendered component passed to `render`.
|
|
188
|
+
|
|
189
|
+
#### `message` option
|
|
190
|
+
|
|
191
|
+
You can provide a message that will be printed on top.
|
|
192
|
+
|
|
193
|
+
```jsx
|
|
194
|
+
render(<Component />);
|
|
195
|
+
screen.debug({ message: 'optional message' });
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
logs optional message and colored JSX:
|
|
199
|
+
|
|
200
|
+
```jsx
|
|
201
|
+
optional message
|
|
202
|
+
|
|
203
|
+
<View
|
|
204
|
+
onPress={[Function bound fn]}
|
|
205
|
+
>
|
|
206
|
+
<Text>Press me</Text>
|
|
207
|
+
</View>
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
#### `mapProps` option
|
|
211
|
+
|
|
212
|
+
You can use the `mapProps` option to transform the props that will be printed :
|
|
213
|
+
|
|
214
|
+
```jsx
|
|
215
|
+
render(<View style={{ backgroundColor: 'red' }} />);
|
|
216
|
+
debug({ mapProps: ({ style, ...props }) => ({ props }) });
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
This will log the rendered JSX without the `style` props.
|
|
220
|
+
|
|
221
|
+
The `children` prop cannot be filtered out so the following will print all rendered components with all props but `children` filtered out.
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
debug({ mapProps: (props) => ({}) });
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
This option can be used to target specific props when debugging a query (for instance keeping only `children` prop when debugging a `getByText` query).
|
|
228
|
+
|
|
229
|
+
You can also transform prop values so that they are more readable (e.g. flatten styles).
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
import { StyleSheet } from 'react-native';
|
|
233
|
+
|
|
234
|
+
debug({ mapProps : {({ style, ...props })} => ({ style : StyleSheet.flatten(style), ...props }) });
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Or remove props that have little value when debugging tests, e.g. path prop for svgs
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
debug({ mapProps: ({ path, ...props }) => ({ ...props }) });
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
#### `debug.shallow`
|
|
244
|
+
|
|
245
|
+
Pretty prints shallowly rendered component passed to `render` with optional message on top.
|
|
246
|
+
|
|
247
|
+
### `toJSON`
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
toJSON(): ReactTestRendererJSON | null
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Get the rendered component JSON representation, e.g. for snapshot testing.
|
|
254
|
+
|
|
255
|
+
### `root`
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
root: ReactTestInstance;
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Returns the rendered root [host element](testing-env#host-and-composite-components).
|
|
262
|
+
|
|
263
|
+
This API is primarily useful in component tests, as it allows you to access root host view without using `*ByTestId` queries or similar methods.
|
|
264
|
+
|
|
265
|
+
### `UNSAFE_root`
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
UNSAFE_root: ReactTestInstance;
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Returns the rendered [composite root element](testing-env#host-and-composite-components).
|
|
272
|
+
|
|
273
|
+
:::caution
|
|
274
|
+
This API typically will return a composite view which goes against recommended testing practices. This API is primarily available for legacy test suites that rely on such testing.
|
|
275
|
+
:::
|
|
276
|
+
|
|
277
|
+
:::note
|
|
278
|
+
This API has been previously named `container` for compatibility with [React Testing Library](https://testing-library.com/docs/react-testing-library/api#container-1). However, despite the same name, the actual behavior has been signficantly different, hence the name change to `UNSAFE_root`.
|
|
279
|
+
:::
|
|
280
|
+
|
|
281
|
+
## `screen`
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
let screen: RenderResult;
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Hold the value of latest render call for easier access to query and other functions returned by [`render`](#render).
|
|
288
|
+
|
|
289
|
+
Its value is automatically cleared after each test by calling [`cleanup`](#cleanup). If no `render` call has been made in a given test then it holds a special object that implements `RenderResult` but throws a helpful error on each property and method access.
|
|
290
|
+
|
|
291
|
+
This can also be used to build test utils that would normally require to be in render scope, either in a test file or globally for your project. For instance:
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
// Prints the rendered components omitting all props except children.
|
|
295
|
+
const debugText = () => screen.debug({ mapProps: (props) => ({}) });
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## `cleanup`
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
const cleanup: () => void;
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Unmounts React trees that were mounted with `render` and clears `screen` variable that holds latest `render` output.
|
|
305
|
+
|
|
306
|
+
:::info
|
|
307
|
+
Please note that this is done automatically if the testing framework you're using supports the `afterEach` global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups after each test.
|
|
308
|
+
:::
|
|
309
|
+
|
|
310
|
+
For example, if you're using the `jest` testing framework, then you would need to use the `afterEach` hook like so:
|
|
311
|
+
|
|
312
|
+
```jsx
|
|
313
|
+
import { cleanup, render } from '@testing-library/react-native/pure';
|
|
314
|
+
import { View } from 'react-native';
|
|
315
|
+
|
|
316
|
+
afterEach(cleanup);
|
|
317
|
+
|
|
318
|
+
it('renders a view', () => {
|
|
319
|
+
render(<View />);
|
|
320
|
+
// ...
|
|
321
|
+
});
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
The `afterEach(cleanup)` call also works in `describe` blocks:
|
|
325
|
+
|
|
326
|
+
```jsx
|
|
327
|
+
describe('when logged in', () => {
|
|
328
|
+
afterEach(cleanup);
|
|
329
|
+
|
|
330
|
+
it('renders the user', () => {
|
|
331
|
+
render(<SiteHeader />);
|
|
332
|
+
// ...
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
Failing to call `cleanup` when you've called `render` could result in a memory leak and tests which are not "idempotent" (which can lead to difficult to debug errors in your tests).
|
|
338
|
+
|
|
339
|
+
## `fireEvent`
|
|
340
|
+
|
|
341
|
+
```ts
|
|
342
|
+
function fireEvent(
|
|
343
|
+
element: ReactTestInstance,
|
|
344
|
+
eventName: string,
|
|
345
|
+
...data: Array<any>
|
|
346
|
+
): void {}
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
Fires native-like event with data.
|
|
350
|
+
|
|
351
|
+
Invokes a given event handler (whether native or custom) on the element, bubbling to the root of the rendered tree.
|
|
352
|
+
|
|
353
|
+
```jsx
|
|
354
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
355
|
+
|
|
356
|
+
test('fire changeText event', () => {
|
|
357
|
+
const onEventMock = jest.fn();
|
|
358
|
+
render(
|
|
359
|
+
// MyComponent renders TextInput which has a placeholder 'Enter details'
|
|
360
|
+
// and with `onChangeText` bound to handleChangeText
|
|
361
|
+
<MyComponent handleChangeText={onEventMock} />
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
fireEvent(screen.getByPlaceholderText('change'), 'onChangeText', 'ab');
|
|
365
|
+
expect(onEventMock).toHaveBeenCalledWith('ab');
|
|
366
|
+
});
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
:::note
|
|
370
|
+
Please note that from version `7.0` `fireEvent` performs checks that should prevent events firing on disabled elements.
|
|
371
|
+
:::
|
|
372
|
+
|
|
373
|
+
An example using `fireEvent` with native events that aren't already aliased by the `fireEvent` api.
|
|
374
|
+
|
|
375
|
+
```jsx
|
|
376
|
+
import { TextInput, View } from 'react-native';
|
|
377
|
+
import { fireEvent, render } from '@testing-library/react-native';
|
|
378
|
+
|
|
379
|
+
const onBlurMock = jest.fn();
|
|
380
|
+
|
|
381
|
+
render(
|
|
382
|
+
<View>
|
|
383
|
+
<TextInput placeholder="my placeholder" onBlur={onBlurMock} />
|
|
384
|
+
</View>
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
// you can omit the `on` prefix
|
|
388
|
+
fireEvent(screen.getByPlaceholderText('my placeholder'), 'blur');
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
## `fireEvent[eventName]`
|
|
392
|
+
|
|
393
|
+
```ts
|
|
394
|
+
fireEvent[eventName](element: ReactTestInstance, ...data: Array<any>): void
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
Convenience methods for common events like: `press`, `changeText`, `scroll`.
|
|
398
|
+
|
|
399
|
+
### `fireEvent.press`
|
|
400
|
+
|
|
401
|
+
```
|
|
402
|
+
fireEvent.press: (element: ReactTestInstance, ...data: Array<any>) => void
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
Invokes `press` event handler on the element or parent element in the tree.
|
|
406
|
+
|
|
407
|
+
```jsx
|
|
408
|
+
import { View, Text, TouchableOpacity } from 'react-native';
|
|
409
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
410
|
+
|
|
411
|
+
const onPressMock = jest.fn();
|
|
412
|
+
const eventData = {
|
|
413
|
+
nativeEvent: {
|
|
414
|
+
pageX: 20,
|
|
415
|
+
pageY: 30,
|
|
416
|
+
},
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
render(
|
|
420
|
+
<View>
|
|
421
|
+
<TouchableOpacity onPress={onPressMock}>
|
|
422
|
+
<Text>Press me</Text>
|
|
423
|
+
</TouchableOpacity>
|
|
424
|
+
</View>
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
fireEvent.press(screen.getByText('Press me'), eventData);
|
|
428
|
+
expect(onPressMock).toHaveBeenCalledWith(eventData);
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
### `fireEvent.changeText`
|
|
432
|
+
|
|
433
|
+
```
|
|
434
|
+
fireEvent.changeText: (element: ReactTestInstance, ...data: Array<any>) => void
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Invokes `changeText` event handler on the element or parent element in the tree.
|
|
438
|
+
|
|
439
|
+
```jsx
|
|
440
|
+
import { View, TextInput } from 'react-native';
|
|
441
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
442
|
+
|
|
443
|
+
const onChangeTextMock = jest.fn();
|
|
444
|
+
const CHANGE_TEXT = 'content';
|
|
445
|
+
|
|
446
|
+
render(
|
|
447
|
+
<View>
|
|
448
|
+
<TextInput placeholder="Enter data" onChangeText={onChangeTextMock} />
|
|
449
|
+
</View>
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
fireEvent.changeText(screen.getByPlaceholderText('Enter data'), CHANGE_TEXT);
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
### `fireEvent.scroll`
|
|
456
|
+
|
|
457
|
+
```
|
|
458
|
+
fireEvent.scroll: (element: ReactTestInstance, ...data: Array<any>) => void
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
Invokes `scroll` event handler on the element or parent element in the tree.
|
|
462
|
+
|
|
463
|
+
#### On a `ScrollView`
|
|
464
|
+
|
|
465
|
+
```jsx
|
|
466
|
+
import { ScrollView, Text } from 'react-native';
|
|
467
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
468
|
+
|
|
469
|
+
const onScrollMock = jest.fn();
|
|
470
|
+
const eventData = {
|
|
471
|
+
nativeEvent: {
|
|
472
|
+
contentOffset: {
|
|
473
|
+
y: 200,
|
|
474
|
+
},
|
|
475
|
+
},
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
render(
|
|
479
|
+
<ScrollView onScroll={onScrollMock}>
|
|
480
|
+
<Text>XD</Text>
|
|
481
|
+
</ScrollView>
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
fireEvent.scroll(screen.getByText('scroll-view'), eventData);
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
#### On a `FlatList`
|
|
488
|
+
|
|
489
|
+
```jsx
|
|
490
|
+
import { FlatList, View } from 'react-native';
|
|
491
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
492
|
+
|
|
493
|
+
const onEndReached = jest.fn();
|
|
494
|
+
render(
|
|
495
|
+
<FlatList
|
|
496
|
+
data={Array.from({ length: 10 }, (_, key) => ({ key: `${key}` }))}
|
|
497
|
+
renderItem={() => <View style={{ height: 500, width: 100 }} />}
|
|
498
|
+
onEndReached={onEndReached}
|
|
499
|
+
onEndReachedThreshold={0.2}
|
|
500
|
+
testID="flat-list"
|
|
501
|
+
/>
|
|
502
|
+
);
|
|
503
|
+
const eventData = {
|
|
504
|
+
nativeEvent: {
|
|
505
|
+
contentOffset: {
|
|
506
|
+
y: 500,
|
|
507
|
+
},
|
|
508
|
+
contentSize: {
|
|
509
|
+
// Dimensions of the scrollable content
|
|
510
|
+
height: 500,
|
|
511
|
+
width: 100,
|
|
512
|
+
},
|
|
513
|
+
layoutMeasurement: {
|
|
514
|
+
// Dimensions of the device
|
|
515
|
+
height: 100,
|
|
516
|
+
width: 100,
|
|
517
|
+
},
|
|
518
|
+
},
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
fireEvent.scroll(screen.getByTestId('flat-list'), eventData);
|
|
522
|
+
expect(onEndReached).toHaveBeenCalled();
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
:::note
|
|
526
|
+
If you're noticing that components are not being found on a list, even after mocking a scroll event, try changing the [`initialNumToRender`](https://reactnative.dev/docs/flatlist#initialnumtorender) that you have set. If you aren't comfortable changing the code to accept this prop from the unit test, try using an e2e test that might better suit what use case you're attempting to replicate.
|
|
527
|
+
:::
|
|
528
|
+
|
|
529
|
+
## `waitFor`
|
|
530
|
+
|
|
531
|
+
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/main/src/__tests__/waitFor.test.tsx)
|
|
532
|
+
|
|
533
|
+
Defined as:
|
|
534
|
+
|
|
535
|
+
```jsx
|
|
536
|
+
function waitFor<T>(
|
|
537
|
+
expectation: () => T,
|
|
538
|
+
{ timeout: number = 1000, interval: number = 50 }
|
|
539
|
+
): Promise<T> {}
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
Waits for a period of time for the `expectation` callback to pass. `waitFor` may run the callback a number of times until timeout is reached, as specified by the `timeout` and `interval` options. The callback must throw an error when the expectation is not met. Returning any value, including a falsy one, will be treated as meeting the expectation, and the callback result will be returned to the caller of `waitFor` function.
|
|
543
|
+
|
|
544
|
+
```tsx
|
|
545
|
+
await waitFor(() => expect(mockFunction).toHaveBeenCalledWith()))
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
`waitFor` function will be executing `expectation` callback every `interval` (default: every 50 ms) until `timeout` (default: 1000 ms) is reached. The repeated execution of callback is stopped as soon as it does not throw an error, in such case the value returned by the callback is returned to `waitFor` caller. Otherwise, when it reaches the timeout, the final error thrown by `expectation` will be re-thrown by `waitFor` to the calling code.
|
|
549
|
+
|
|
550
|
+
```tsx
|
|
551
|
+
// ❌ `waitFor` will return immediately because callback does not throw
|
|
552
|
+
await waitFor(() => false);
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
`waitFor` is an async function so you need to `await` the result to pause test execution.
|
|
556
|
+
|
|
557
|
+
```jsx
|
|
558
|
+
// ❌ missing `await`: `waitFor` will just return Promise that will be rejected when the timeout is reached
|
|
559
|
+
waitFor(() => expect(1).toBe(2))
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
:::note
|
|
563
|
+
You can enforce awaiting `waitFor` by using the [await-async-utils](https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/await-async-utils.md) rule from [eslint-plugin-testing-library](https://github.com/testing-library/eslint-plugin-testing-library).
|
|
564
|
+
:::
|
|
565
|
+
|
|
566
|
+
Since `waitFor` is likely to run `expectation` callback multiple times, it is highly recommended for it [not to perform any side effects](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#performing-side-effects-in-waitfor) in `waitFor`.
|
|
567
|
+
|
|
568
|
+
```jsx
|
|
569
|
+
await waitFor(() => {
|
|
570
|
+
// ❌ button will be pressed on each waitFor iteration
|
|
571
|
+
fireEvent.press(screen.getByText('press me'))
|
|
572
|
+
expect(mockOnPress).toHaveBeenCalled()
|
|
573
|
+
})
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
:::note
|
|
577
|
+
Avoiding side effects in `expectation` callback can be partially enforced with the [`no-wait-for-side-effects` rule](https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/no-wait-for-side-effects.md).
|
|
578
|
+
:::
|
|
579
|
+
|
|
580
|
+
It is also recommended to have a [single assertion per each `waitFor`](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#having-multiple-assertions-in-a-single-waitfor-callback) for more consistency and faster failing tests. If you want to make several assertions, then they should be in seperate `waitFor` calls. In many cases you won't actually need to wrap the second assertion in `waitFor` since the first one will do the waiting required for asynchronous change to happen.
|
|
581
|
+
|
|
582
|
+
### Using a React Native version < 0.71 with Jest fake timers
|
|
583
|
+
|
|
584
|
+
:::caution
|
|
585
|
+
When using a version of React Native < 0.71 and modern fake timers (the default for `Jest` >= 27), `waitFor` won't work (it will always timeout even if `expectation()` doesn't throw) unless you use the custom [@testing-library/react-native preset](https://github.com/callstack/react-native-testing-library#custom-jest-preset).
|
|
586
|
+
:::
|
|
587
|
+
|
|
588
|
+
`waitFor` checks whether Jest fake timers are enabled and adapts its behavior in such case. The following snippet is a simplified version of how it behaves when fake timers are enabled:
|
|
589
|
+
|
|
590
|
+
```tsx
|
|
591
|
+
let fakeTimeRemaining = timeout;
|
|
592
|
+
let lastError;
|
|
593
|
+
|
|
594
|
+
while(fakeTimeRemaining > 0) {
|
|
595
|
+
fakeTimeRemaining = fakeTimeRemaining - interval;
|
|
596
|
+
jest.advanceTimersByTime(interval);
|
|
597
|
+
try {
|
|
598
|
+
// resolve
|
|
599
|
+
return expectation();
|
|
600
|
+
} catch (error) {
|
|
601
|
+
lastError = error;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// reject
|
|
606
|
+
throw lastError
|
|
607
|
+
```
|
|
608
|
+
|
|
609
|
+
In the following example we test that a function is called after 10 seconds using fake timers. Since we're using fake timers, the test won't depend on real time passing and thus be much faster and more reliable. Also we don't have to advance fake timers through Jest fake timers API because `waitFor` already does this for us.
|
|
610
|
+
|
|
611
|
+
```tsx
|
|
612
|
+
// in component
|
|
613
|
+
setTimeout(() => {
|
|
614
|
+
someFunction();
|
|
615
|
+
}, 10000)
|
|
616
|
+
|
|
617
|
+
// in test
|
|
618
|
+
jest.useFakeTimers();
|
|
619
|
+
|
|
620
|
+
await waitFor(() => {
|
|
621
|
+
expect(someFunction).toHaveBeenCalledWith();
|
|
622
|
+
}, 10000)
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
:::info
|
|
626
|
+
In order to properly use `waitFor` you need at least React >=16.9.0 (featuring async `act`) or React Native >=0.61 (which comes with React >=16.9.0).
|
|
627
|
+
:::
|
|
628
|
+
|
|
629
|
+
:::note
|
|
630
|
+
If you receive warnings related to `act()` function consult our [Undestanding Act](./UnderstandingAct.md) function document.
|
|
631
|
+
:::
|
|
632
|
+
|
|
633
|
+
## `waitForElementToBeRemoved`
|
|
634
|
+
|
|
635
|
+
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/main/src/__tests__/waitForElementToBeRemoved.test.tsx)
|
|
636
|
+
|
|
637
|
+
Defined as:
|
|
638
|
+
|
|
639
|
+
```ts
|
|
640
|
+
function waitForElementToBeRemoved<T>(
|
|
641
|
+
expectation: () => T,
|
|
642
|
+
{ timeout: number = 4500, interval: number = 50 }
|
|
643
|
+
): Promise<T> {}
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
Waits for non-deterministic periods of time until queried element is removed or times out. `waitForElementToBeRemoved` periodically calls `expectation` every `interval` milliseconds to determine whether the element has been removed or not.
|
|
647
|
+
|
|
648
|
+
```jsx
|
|
649
|
+
import {
|
|
650
|
+
render,
|
|
651
|
+
screen,
|
|
652
|
+
waitForElementToBeRemoved,
|
|
653
|
+
} from '@testing-library/react-native';
|
|
654
|
+
|
|
655
|
+
test('waiting for an Banana to be removed', async () => {
|
|
656
|
+
render(<Banana />);
|
|
657
|
+
|
|
658
|
+
await waitForElementToBeRemoved(() => screen.getByText('Banana ready'));
|
|
659
|
+
});
|
|
660
|
+
```
|
|
661
|
+
|
|
662
|
+
This method expects that the element is initially present in the render tree and then is removed from it. If the element is not present when you call this method it throws an error.
|
|
663
|
+
|
|
664
|
+
You can use any of `getBy`, `getAllBy`, `queryBy` and `queryAllBy` queries for `expectation` parameter.
|
|
665
|
+
|
|
666
|
+
:::info
|
|
667
|
+
In order to properly use `waitForElementToBeRemoved` you need at least React >=16.9.0 (featuring async `act`) or React Native >=0.61 (which comes with React >=16.9.0).
|
|
668
|
+
:::
|
|
669
|
+
|
|
670
|
+
:::note
|
|
671
|
+
If you receive warnings related to `act()` function consult our [Undestanding Act](./UnderstandingAct.md) function document.
|
|
672
|
+
:::
|
|
673
|
+
|
|
674
|
+
## `within`, `getQueriesForElement`
|
|
675
|
+
|
|
676
|
+
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/main/src/__tests__/within.test.tsx)
|
|
677
|
+
|
|
678
|
+
Defined as:
|
|
679
|
+
|
|
680
|
+
```jsx
|
|
681
|
+
function within(element: ReactTestInstance): Queries {}
|
|
682
|
+
|
|
683
|
+
function getQueriesForElement(element: ReactTestInstance): Queries {}
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
`within` (also available as `getQueriesForElement` alias) performs [queries](./Queries.md) scoped to given element.
|
|
687
|
+
|
|
688
|
+
:::note
|
|
689
|
+
Please note that additional `render` specific operations like `update`, `unmount`, `debug`, `toJSON` are _not_ included.
|
|
690
|
+
:::
|
|
691
|
+
|
|
692
|
+
```jsx
|
|
693
|
+
const detailsScreen = within(screen.getByA11yHint('Details Screen'));
|
|
694
|
+
expect(detailsScreen.getByText('Some Text')).toBeOnTheScreen();
|
|
695
|
+
expect(detailsScreen.getByDisplayValue('Some Value')).toBeOnTheScreen();
|
|
696
|
+
expect(detailsScreen.queryByLabelText('Some Label')).toBeOnTheScreen();
|
|
697
|
+
await expect(detailsScreen.findByA11yHint('Some Label')).resolves.toBeOnTheScreen();
|
|
698
|
+
```
|
|
699
|
+
|
|
700
|
+
Use cases for scoped queries include:
|
|
701
|
+
|
|
702
|
+
- queries scoped to a single item inside a FlatList containing many items
|
|
703
|
+
- queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation)
|
|
704
|
+
|
|
705
|
+
## `queryBy*` APIs
|
|
706
|
+
|
|
707
|
+
Each of the `getBy*` APIs listed in the render section above have a complimentary `queryBy*` API. The `getBy*` APIs will throw errors if a proper node cannot be found. This is normally the desired effect. However, if you want to make an assertion that an element is not present in the hierarchy, then you can use the `queryBy*` API instead:
|
|
708
|
+
|
|
709
|
+
```jsx
|
|
710
|
+
import { render, screen } from '@testing-library/react-native';
|
|
711
|
+
|
|
712
|
+
render(<Form />);
|
|
713
|
+
const submitButton = screen.queryByText('submit');
|
|
714
|
+
expect(submitButton).not.toBeOnTheScreen(); // it doesn't exist
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
## `queryAll*` APIs
|
|
718
|
+
|
|
719
|
+
Each of the query APIs have a corresponding `queryAll*` version that always returns an array of matching nodes. `getAll*` is the same but throws when the array has a length of 0.
|
|
720
|
+
|
|
721
|
+
```jsx
|
|
722
|
+
import { render } from '@testing-library/react-native';
|
|
723
|
+
|
|
724
|
+
render(<Forms />);
|
|
725
|
+
const submitButtons = screen.queryAllByText('submit');
|
|
726
|
+
expect(submitButtons).toHaveLength(3); // expect 3 elements
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
## `act`
|
|
730
|
+
|
|
731
|
+
Useful function to help testing components that use hooks API. By default any `render`, `update`, `fireEvent`, and `waitFor` calls are wrapped by this function, so there is no need to wrap it manually. This method is re-exported from [`react-test-renderer`](https://github.com/facebook/react/blob/main/packages/react-test-renderer/src/ReactTestRenderer.js#L567]).
|
|
732
|
+
|
|
733
|
+
Consult our [Undestanding Act function](./UnderstandingAct.md) document for more understanding of its intricacies.
|
|
734
|
+
|
|
735
|
+
## `renderHook`
|
|
736
|
+
|
|
737
|
+
Defined as:
|
|
738
|
+
|
|
739
|
+
```ts
|
|
740
|
+
function renderHook<Result, Props>(
|
|
741
|
+
callback: (props?: Props) => Result,
|
|
742
|
+
options?: RenderHookOptions<Props>
|
|
743
|
+
): RenderHookResult<Result, Props>;
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
Renders a test component that will call the provided `callback`, including any hooks it calls, every time it renders. Returns [`RenderHookResult`](#renderhookresult-object) object, which you can interact with.
|
|
747
|
+
|
|
748
|
+
```ts
|
|
749
|
+
import { renderHook } from '@testing-library/react-native';
|
|
750
|
+
import { useCount } from '../useCount';
|
|
751
|
+
|
|
752
|
+
it('should increment count', () => {
|
|
753
|
+
const { result } = renderHook(() => useCount());
|
|
754
|
+
|
|
755
|
+
expect(result.current.count).toBe(0);
|
|
756
|
+
act(() => {
|
|
757
|
+
// Note that you should wrap the calls to functions your hook returns with `act` if they trigger an update of your hook's state to ensure pending useEffects are run before your next assertion.
|
|
758
|
+
result.current.increment();
|
|
759
|
+
});
|
|
760
|
+
expect(result.current.count).toBe(1);
|
|
761
|
+
});
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
```ts
|
|
765
|
+
// useCount.js
|
|
766
|
+
export const useCount = () => {
|
|
767
|
+
const [count, setCount] = useState(0);
|
|
768
|
+
const increment = () => setCount((previousCount) => previousCount + 1);
|
|
769
|
+
|
|
770
|
+
return { count, increment };
|
|
771
|
+
};
|
|
772
|
+
```
|
|
773
|
+
|
|
774
|
+
The `renderHook` function accepts the following arguments:
|
|
775
|
+
|
|
776
|
+
### `callback`
|
|
777
|
+
|
|
778
|
+
The function that is called each `render` of the test component. This function should call one or more hooks for testing.
|
|
779
|
+
|
|
780
|
+
The `props` passed into the callback will be the `initialProps` provided in the `options` to `renderHook`, unless new props are provided by a subsequent `rerender` call.
|
|
781
|
+
|
|
782
|
+
### `options` (Optional)
|
|
783
|
+
|
|
784
|
+
A `RenderHookOptions<Props>` object to modify the execution of the `callback` function, containing the following properties:
|
|
785
|
+
|
|
786
|
+
#### `initialProps`
|
|
787
|
+
|
|
788
|
+
The initial values to pass as `props` to the `callback` function of `renderHook`. The `Props` type is determined by the type passed to or inferred by the `renderHook` call.
|
|
789
|
+
|
|
790
|
+
#### `wrapper`
|
|
791
|
+
|
|
792
|
+
A React component to wrap the test component in when rendering. This is usually used to add context providers from `React.createContext` for the hook to access with `useContext`.
|
|
793
|
+
|
|
794
|
+
### `RenderHookResult` object
|
|
795
|
+
|
|
796
|
+
```ts
|
|
797
|
+
interface RenderHookResult<Result, Props> {
|
|
798
|
+
result: { current: Result };
|
|
799
|
+
rerender: (props: Props) => void;
|
|
800
|
+
unmount: () => void;
|
|
801
|
+
}
|
|
802
|
+
```
|
|
803
|
+
|
|
804
|
+
The `renderHook` function returns an object that has the following properties:
|
|
805
|
+
|
|
806
|
+
#### `result`
|
|
807
|
+
|
|
808
|
+
The `current` value of the `result` will reflect the latest of whatever is returned from the `callback` passed to `renderHook`. The `Result` type is determined by the type passed to or inferred by the `renderHook` call.
|
|
809
|
+
|
|
810
|
+
#### `rerender`
|
|
811
|
+
|
|
812
|
+
A function to rerender the test component, causing any hooks to be recalculated. If `newProps` are passed, they will replace the `callback` function's `initialProps` for subsequent rerenders. The `Props` type is determined by the type passed to or inferred by the `renderHook` call.
|
|
813
|
+
|
|
814
|
+
#### `unmount`
|
|
815
|
+
|
|
816
|
+
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
|
|
817
|
+
|
|
818
|
+
### Examples
|
|
819
|
+
|
|
820
|
+
Here we present some extra examples of using `renderHook` API.
|
|
821
|
+
|
|
822
|
+
#### With `initialProps`
|
|
823
|
+
|
|
824
|
+
```ts
|
|
825
|
+
const useCount = (initialCount: number) => {
|
|
826
|
+
const [count, setCount] = useState(initialCount);
|
|
827
|
+
const increment = () => setCount((previousCount) => previousCount + 1);
|
|
828
|
+
|
|
829
|
+
useEffect(() => {
|
|
830
|
+
setCount(initialCount);
|
|
831
|
+
}, [initialCount]);
|
|
832
|
+
|
|
833
|
+
return { count, increment };
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
it('should increment count', () => {
|
|
837
|
+
const { result, rerender } = renderHook(
|
|
838
|
+
(initialCount: number) => useCount(initialCount),
|
|
839
|
+
{ initialProps: 1 }
|
|
840
|
+
);
|
|
841
|
+
|
|
842
|
+
expect(result.current.count).toBe(1);
|
|
843
|
+
|
|
844
|
+
act(() => {
|
|
845
|
+
result.current.increment();
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
expect(result.current.count).toBe(2);
|
|
849
|
+
rerender(5);
|
|
850
|
+
expect(result.current.count).toBe(5);
|
|
851
|
+
});
|
|
852
|
+
```
|
|
853
|
+
|
|
854
|
+
#### With `wrapper`
|
|
855
|
+
|
|
856
|
+
```tsx
|
|
857
|
+
it('should use context value', () => {
|
|
858
|
+
function Wrapper({ children }: { children: ReactNode }) {
|
|
859
|
+
return <Context.Provider value="provided">{children}</Context.Provider>;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
const { result } = renderHook(() => useHook(), { wrapper: Wrapper });
|
|
863
|
+
// ...
|
|
864
|
+
});
|
|
865
|
+
```
|
|
866
|
+
|
|
867
|
+
## Configuration
|
|
868
|
+
|
|
869
|
+
### `configure`
|
|
870
|
+
|
|
871
|
+
```ts
|
|
872
|
+
type Config = {
|
|
873
|
+
asyncUtilTimeout: number;
|
|
874
|
+
defaultHidden: boolean;
|
|
875
|
+
defaultDebugOptions: Partial<DebugOptions>;
|
|
876
|
+
};
|
|
877
|
+
|
|
878
|
+
function configure(options: Partial<Config>) {}
|
|
879
|
+
```
|
|
880
|
+
|
|
881
|
+
#### `asyncUtilTimeout` option
|
|
882
|
+
|
|
883
|
+
Default timeout, in ms, for async helper functions (`waitFor`, `waitForElementToBeRemoved`) and `findBy*` queries. Defaults to 1000 ms.
|
|
884
|
+
|
|
885
|
+
#### `defaultIncludeHiddenElements` option
|
|
886
|
+
|
|
887
|
+
Default value for [includeHiddenElements](Queries.md#includehiddenelements-option) query option for all queries. The default value is set to `false`, so all queries will not match [elements hidden from accessibility](#ishiddenfromaccessibility). This is because the users of the app would not be able to see such elements.
|
|
888
|
+
|
|
889
|
+
This option is also available as `defaultHidden` alias for compatibility with [React Testing Library](https://testing-library.com/docs/dom-testing-library/api-configuration/#defaulthidden).
|
|
890
|
+
|
|
891
|
+
#### `defaultDebugOptions` option
|
|
892
|
+
|
|
893
|
+
Default [debug options](#debug) to be used when calling `debug()`. These default options will be overridden by the ones you specify directly when calling `debug()`.
|
|
894
|
+
|
|
895
|
+
### `resetToDefaults()`
|
|
896
|
+
|
|
897
|
+
```ts
|
|
898
|
+
function resetToDefaults() {}
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
### Environment variables
|
|
902
|
+
|
|
903
|
+
#### `RNTL_SKIP_AUTO_CLEANUP`
|
|
904
|
+
|
|
905
|
+
Set to `true` to disable automatic `cleanup()` after each test. It works the same as importing `react-native-testing-library/dont-cleanup-after-each` or using `react-native-testing-library/pure`.
|
|
906
|
+
|
|
907
|
+
```shell
|
|
908
|
+
$ RNTL_SKIP_AUTO_CLEANUP=true jest
|
|
909
|
+
```
|
|
910
|
+
|
|
911
|
+
#### `RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS`
|
|
912
|
+
|
|
913
|
+
Set to `true` to disable auto-detection of fake timers. This might be useful in rare cases when you want to use non-Jest fake timers. See [issue #886](https://github.com/callstack/react-native-testing-library/issues/886) for more details.
|
|
914
|
+
|
|
915
|
+
```shell
|
|
916
|
+
$ RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS=true jest
|
|
917
|
+
```
|
|
918
|
+
|
|
919
|
+
## Accessibility
|
|
920
|
+
|
|
921
|
+
### `isHiddenFromAccessibility`
|
|
922
|
+
|
|
923
|
+
```ts
|
|
924
|
+
function isHiddenFromAccessibility(
|
|
925
|
+
element: ReactTestInstance | null
|
|
926
|
+
): boolean {}
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
Also available as `isInaccessible()` alias for React Testing Library compatibility.
|
|
930
|
+
|
|
931
|
+
Checks if given element is hidden from assistive technology, e.g. screen readers.
|
|
932
|
+
|
|
933
|
+
:::note
|
|
934
|
+
Like [`isInaccessible`](https://testing-library.com/docs/dom-testing-library/api-accessibility/#isinaccessible) function from DOM Testing Library this function considers both accessibility elements and presentational elements (regular `View`s) to be accessible, unless they are hidden in terms of host platform.
|
|
935
|
+
|
|
936
|
+
This covers only part of [ARIA notion of Accessiblity Tree](https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion), as ARIA excludes both hidden and presentational elements from the Accessibility Tree.
|
|
937
|
+
:::
|
|
938
|
+
|
|
939
|
+
For the scope of this function, element is inaccessible when it, or any of its ancestors, meets any of the following conditions:
|
|
940
|
+
|
|
941
|
+
- it has `display: none` style
|
|
942
|
+
- it has [`accessibilityElementsHidden`](https://reactnative.dev/docs/accessibility#accessibilityelementshidden-ios) prop set to `true`
|
|
943
|
+
- it has [`importantForAccessibility`](https://reactnative.dev/docs/accessibility#importantforaccessibility-android) prop set to `no-hide-descendants`
|
|
944
|
+
- it has sibling host element with [`accessibilityViewIsModal`](https://reactnative.dev/docs/accessibility#accessibilityviewismodal-ios) prop set to `true`
|
|
945
|
+
|
|
946
|
+
Specifying `accessible={false}`, `accessiblityRole="none"`, or `importantForAccessibility="no"` props does not cause the element to become inaccessible.
|