@testing-library/react-native 12.1.3 → 12.2.1

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.
Files changed (194) hide show
  1. package/.eslintcache +1 -1
  2. package/.eslintignore +1 -0
  3. package/build/fireEvent.js +2 -5
  4. package/build/fireEvent.js.map +1 -1
  5. package/build/helpers/component-tree.d.ts +11 -5
  6. package/build/helpers/component-tree.js +5 -1
  7. package/build/helpers/component-tree.js.map +1 -1
  8. package/build/helpers/deprecation.js +1 -1
  9. package/build/helpers/deprecation.js.map +1 -1
  10. package/build/helpers/findAll.d.ts +2 -1
  11. package/build/helpers/findAll.js +2 -1
  12. package/build/helpers/findAll.js.map +1 -1
  13. package/build/helpers/host-component-names.d.ts +12 -0
  14. package/build/helpers/host-component-names.js +18 -0
  15. package/build/helpers/host-component-names.js.map +1 -1
  16. package/build/helpers/matchers/matchLabelText.js +1 -1
  17. package/build/helpers/matchers/matchLabelText.js.map +1 -1
  18. package/build/pure.d.ts +2 -0
  19. package/build/pure.js +7 -0
  20. package/build/pure.js.map +1 -1
  21. package/build/queries/a11yState.js +1 -1
  22. package/build/queries/a11yState.js.map +1 -1
  23. package/build/queries/a11yValue.js +1 -1
  24. package/build/queries/a11yValue.js.map +1 -1
  25. package/build/queries/displayValue.js +5 -6
  26. package/build/queries/displayValue.js.map +1 -1
  27. package/build/queries/hintText.js +1 -1
  28. package/build/queries/hintText.js.map +1 -1
  29. package/build/queries/labelText.js +1 -1
  30. package/build/queries/labelText.js.map +1 -1
  31. package/build/queries/placeholderText.js +3 -4
  32. package/build/queries/placeholderText.js.map +1 -1
  33. package/build/queries/role.js +1 -1
  34. package/build/queries/role.js.map +1 -1
  35. package/build/queries/testId.js +3 -3
  36. package/build/queries/testId.js.map +1 -1
  37. package/build/queries/text.js +1 -2
  38. package/build/queries/text.js.map +1 -1
  39. package/build/render.js.map +1 -1
  40. package/build/user-event/clear.d.ts +3 -0
  41. package/build/user-event/clear.js +41 -0
  42. package/build/user-event/clear.js.map +1 -0
  43. package/build/user-event/event-builder/common.d.ts +48 -6
  44. package/build/user-event/event-builder/common.js +37 -20
  45. package/build/user-event/event-builder/common.js.map +1 -1
  46. package/build/user-event/event-builder/index.d.ts +94 -0
  47. package/build/user-event/event-builder/index.js +3 -1
  48. package/build/user-event/event-builder/index.js.map +1 -1
  49. package/build/user-event/event-builder/text-input.d.ts +91 -0
  50. package/build/user-event/event-builder/text-input.js +117 -0
  51. package/build/user-event/event-builder/text-input.js.map +1 -0
  52. package/build/user-event/index.d.ts +5 -2
  53. package/build/user-event/index.js +8 -1
  54. package/build/user-event/index.js.map +1 -1
  55. package/build/user-event/press/index.d.ts +1 -1
  56. package/build/user-event/press/index.js +6 -0
  57. package/build/user-event/press/index.js.map +1 -1
  58. package/build/user-event/press/press.d.ts +3 -3
  59. package/build/user-event/press/press.js +54 -64
  60. package/build/user-event/press/press.js.map +1 -1
  61. package/build/user-event/setup/setup.d.ts +45 -3
  62. package/build/user-event/setup/setup.js +17 -2
  63. package/build/user-event/setup/setup.js.map +1 -1
  64. package/build/user-event/type/index.d.ts +1 -1
  65. package/build/user-event/type/index.js +6 -0
  66. package/build/user-event/type/index.js.map +1 -1
  67. package/build/user-event/type/parseKeys.d.ts +1 -0
  68. package/build/user-event/type/parseKeys.js +40 -0
  69. package/build/user-event/type/parseKeys.js.map +1 -0
  70. package/build/user-event/type/type.d.ts +7 -2
  71. package/build/user-event/type/type.js +70 -8
  72. package/build/user-event/type/type.js.map +1 -1
  73. package/build/user-event/utils/content-size.d.ts +15 -0
  74. package/build/user-event/utils/content-size.js +26 -0
  75. package/build/user-event/utils/content-size.js.map +1 -0
  76. package/build/user-event/utils/{events.d.ts → dispatch-event.d.ts} +2 -2
  77. package/build/user-event/utils/dispatch-event.js +36 -0
  78. package/build/user-event/utils/dispatch-event.js.map +1 -0
  79. package/build/user-event/utils/host-components.d.ts +2 -0
  80. package/build/user-event/utils/host-components.js +11 -0
  81. package/build/user-event/utils/host-components.js.map +1 -0
  82. package/build/user-event/utils/index.d.ts +5 -1
  83. package/build/user-event/utils/index.js +48 -4
  84. package/build/user-event/utils/index.js.map +1 -1
  85. package/build/user-event/utils/text-range.d.ts +4 -0
  86. package/build/user-event/utils/text-range.js +2 -0
  87. package/build/user-event/utils/text-range.js.map +1 -0
  88. package/build/user-event/utils/warn-about-real-timers.d.ts +1 -0
  89. package/build/user-event/utils/warn-about-real-timers.js +20 -0
  90. package/build/user-event/utils/warn-about-real-timers.js.map +1 -0
  91. package/examples/basic/.expo/README.md +15 -0
  92. package/examples/basic/.expo/packager-info.json +4 -0
  93. package/examples/basic/.expo/settings.json +10 -0
  94. package/examples/basic/__tests__/App.test.tsx +30 -12
  95. package/examples/basic/package.json +7 -7
  96. package/examples/basic/yarn.lock +7499 -0
  97. package/examples/react-navigation/README.md +2 -0
  98. package/examples/react-navigation/package.json +5 -5
  99. package/examples/react-navigation/yarn.lock +5018 -0
  100. package/examples/redux/README.md +5 -0
  101. package/examples/redux/package.json +7 -7
  102. package/examples/redux/yarn.lock +4819 -0
  103. package/experiments-app/.expo/packager-info.json +2 -2
  104. package/experiments-app/package.json +7 -9
  105. package/experiments-app/src/MainScreen.tsx +1 -0
  106. package/experiments-app/src/experiments.ts +20 -2
  107. package/experiments-app/src/screens/FlatListEvents.tsx +57 -0
  108. package/experiments-app/src/screens/ScrollViewEvents.tsx +65 -0
  109. package/experiments-app/src/screens/SectionListEvents.tsx +91 -0
  110. package/experiments-app/src/screens/TextInputEventPropagation.tsx +5 -17
  111. package/experiments-app/src/screens/TextInputEvents.tsx +13 -15
  112. package/experiments-app/src/utils/helpers.ts +13 -3
  113. package/experiments-app/yarn.lock +901 -1105
  114. package/experiments-rtl/.babelrc +8 -0
  115. package/experiments-rtl/.eslintrc.json +3 -0
  116. package/experiments-rtl/.gitignore +35 -0
  117. package/experiments-rtl/README.md +34 -0
  118. package/experiments-rtl/jest-setup.js +1 -0
  119. package/experiments-rtl/jest.config.js +4 -0
  120. package/experiments-rtl/next.config.js +4 -0
  121. package/experiments-rtl/package.json +38 -0
  122. package/experiments-rtl/postcss.config.js +6 -0
  123. package/experiments-rtl/public/next.svg +1 -0
  124. package/experiments-rtl/public/vercel.svg +1 -0
  125. package/experiments-rtl/src/app/__tests__/click.test.tsx +31 -0
  126. package/experiments-rtl/src/app/__tests__/managed-text-input.test.tsx +51 -0
  127. package/experiments-rtl/src/app/globals.css +27 -0
  128. package/experiments-rtl/src/app/layout.tsx +22 -0
  129. package/experiments-rtl/src/app/page.tsx +113 -0
  130. package/experiments-rtl/tailwind.config.ts +20 -0
  131. package/experiments-rtl/tsconfig.json +28 -0
  132. package/experiments-rtl/yarn.lock +5418 -0
  133. package/package.json +4 -2
  134. package/src/__tests__/act.test.tsx +4 -0
  135. package/src/fireEvent.ts +1 -5
  136. package/src/helpers/component-tree.ts +14 -9
  137. package/src/helpers/deprecation.ts +1 -1
  138. package/src/helpers/findAll.ts +6 -4
  139. package/src/helpers/host-component-names.tsx +21 -0
  140. package/src/helpers/matchers/matchLabelText.ts +0 -1
  141. package/src/pure.ts +2 -0
  142. package/src/queries/a11yState.ts +2 -6
  143. package/src/queries/a11yValue.ts +2 -6
  144. package/src/queries/displayValue.ts +7 -14
  145. package/src/queries/hintText.ts +2 -7
  146. package/src/queries/labelText.ts +1 -3
  147. package/src/queries/placeholderText.ts +6 -13
  148. package/src/queries/role.ts +1 -2
  149. package/src/queries/testId.ts +5 -10
  150. package/src/queries/text.ts +3 -6
  151. package/src/render.tsx +1 -1
  152. package/src/user-event/__tests__/__snapshots__/clear.test.tsx.snap +269 -0
  153. package/src/user-event/__tests__/clear.test.tsx +217 -0
  154. package/src/user-event/clear.ts +59 -0
  155. package/src/user-event/event-builder/common.ts +35 -19
  156. package/src/user-event/event-builder/index.ts +2 -0
  157. package/src/user-event/event-builder/text-input.ts +86 -0
  158. package/src/user-event/index.ts +7 -3
  159. package/src/user-event/press/__tests__/longPress.real-timers.test.tsx +4 -2
  160. package/src/user-event/press/__tests__/press.real-timers.test.tsx +4 -2
  161. package/src/user-event/press/__tests__/press.test.tsx +40 -5
  162. package/src/user-event/press/index.ts +1 -1
  163. package/src/user-event/press/press.ts +93 -64
  164. package/src/user-event/setup/setup.ts +54 -5
  165. package/src/user-event/type/__tests__/__snapshots__/type-managed.test.tsx.snap +339 -0
  166. package/src/user-event/type/__tests__/__snapshots__/type.test.tsx.snap +644 -2
  167. package/src/user-event/type/__tests__/parseKeys.test.ts +23 -0
  168. package/src/user-event/type/__tests__/type-managed.test.tsx +120 -0
  169. package/src/user-event/type/__tests__/type.test.tsx +299 -27
  170. package/src/user-event/type/index.ts +1 -1
  171. package/src/user-event/type/parseKeys.ts +41 -0
  172. package/src/user-event/type/type.ts +128 -10
  173. package/src/user-event/utils/__tests__/dispatch-event.test.tsx +41 -0
  174. package/src/user-event/utils/__tests__/wait.test.ts +0 -1
  175. package/src/user-event/utils/content-size.ts +25 -0
  176. package/src/user-event/utils/dispatch-event.ts +38 -0
  177. package/src/user-event/utils/host-components.ts +6 -0
  178. package/src/user-event/utils/index.ts +5 -1
  179. package/src/user-event/utils/text-range.ts +4 -0
  180. package/src/user-event/{press/utils/warnAboutRealTimers.ts → utils/warn-about-real-timers.ts} +8 -1
  181. package/website/docs/API.md +19 -25
  182. package/website/docs/Queries.md +64 -59
  183. package/website/docs/UserEvent.md +134 -9
  184. package/website/sidebars.js +1 -1
  185. package/build/helpers/filterNodeByType.d.ts +0 -3
  186. package/build/helpers/filterNodeByType.js +0 -9
  187. package/build/helpers/filterNodeByType.js.map +0 -1
  188. package/build/user-event/press/utils/warnAboutRealTimers.d.ts +0 -1
  189. package/build/user-event/press/utils/warnAboutRealTimers.js +0 -14
  190. package/build/user-event/press/utils/warnAboutRealTimers.js.map +0 -1
  191. package/build/user-event/utils/events.js +0 -44
  192. package/build/user-event/utils/events.js.map +0 -1
  193. package/src/helpers/filterNodeByType.ts +0 -7
  194. package/src/user-event/utils/events.ts +0 -54
@@ -0,0 +1,41 @@
1
+ import * as React from 'react';
2
+ import { Text } from 'react-native';
3
+ import render from '../../../render';
4
+ import { dispatchEvent } from '../dispatch-event';
5
+ import { EventBuilder } from '../../event-builder';
6
+
7
+ const TOUCH_EVENT = EventBuilder.Common.touch();
8
+
9
+ describe('dispatchEvent', () => {
10
+ it('does dispatch event', () => {
11
+ const onPress = jest.fn();
12
+ const screen = render(<Text testID="text" onPress={onPress} />);
13
+
14
+ dispatchEvent(screen.getByTestId('text'), 'press', TOUCH_EVENT);
15
+ expect(onPress).toHaveBeenCalledTimes(1);
16
+ });
17
+
18
+ it('does not dispatch event to parent host component', () => {
19
+ const onPressParent = jest.fn();
20
+ const screen = render(
21
+ <Text onPress={onPressParent}>
22
+ <Text testID="text" />
23
+ </Text>
24
+ );
25
+
26
+ dispatchEvent(screen.getByTestId('text'), 'press', TOUCH_EVENT);
27
+ expect(onPressParent).not.toHaveBeenCalled();
28
+ });
29
+
30
+ it('does NOT throw if no handler found', () => {
31
+ const screen = render(
32
+ <Text>
33
+ <Text testID="text" />
34
+ </Text>
35
+ );
36
+
37
+ expect(() =>
38
+ dispatchEvent(screen.getByTestId('text'), 'press', TOUCH_EVENT)
39
+ ).not.toThrow();
40
+ });
41
+ });
@@ -2,7 +2,6 @@ import { wait } from '../wait';
2
2
 
3
3
  beforeEach(() => {
4
4
  jest.useRealTimers();
5
- jest.clearAllMocks();
6
5
  });
7
6
 
8
7
  describe('wait()', () => {
@@ -0,0 +1,25 @@
1
+ export interface ContentSize {
2
+ width: number;
3
+ height: number;
4
+ }
5
+
6
+ /**
7
+ * Simple function for getting mock the size of given text.
8
+ *
9
+ * It works by calculating height based on number of lines and width based on
10
+ * the longest line length. It does not take into account font size, font
11
+ * family, as well as different letter sizes.
12
+ *
13
+ * @param text text to be measure
14
+ * @returns width and height of the text
15
+ */
16
+
17
+ export function getTextContentSize(text: string): ContentSize {
18
+ const lines = text.split('\n');
19
+ const maxLineLength = Math.max(...lines.map((line) => line.length));
20
+
21
+ return {
22
+ width: maxLineLength * 5,
23
+ height: lines.length * 16,
24
+ };
25
+ }
@@ -0,0 +1,38 @@
1
+ import { ReactTestInstance } from 'react-test-renderer';
2
+ import act from '../../act';
3
+
4
+ /**
5
+ * Basic dispatch event function used by User Event module.
6
+ *
7
+ * @param element element trigger event on
8
+ * @param eventName name of the event
9
+ * @param event event payload
10
+ */
11
+ export function dispatchEvent(
12
+ element: ReactTestInstance,
13
+ eventName: string,
14
+ event: unknown
15
+ ) {
16
+ const handler = getEventHandler(element, eventName);
17
+ if (!handler) {
18
+ return;
19
+ }
20
+
21
+ act(() => {
22
+ handler(event);
23
+ });
24
+ }
25
+
26
+ function getEventHandler(element: ReactTestInstance, eventName: string) {
27
+ const handleName = getEventHandlerName(eventName);
28
+ const handle = element.props[handleName] as unknown;
29
+ if (typeof handle !== 'function') {
30
+ return undefined;
31
+ }
32
+
33
+ return handle;
34
+ }
35
+
36
+ function getEventHandlerName(eventName: string) {
37
+ return `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`;
38
+ }
@@ -0,0 +1,6 @@
1
+ import { ReactTestInstance } from 'react-test-renderer';
2
+ import { isHostTextInput } from '../../helpers/host-component-names';
3
+
4
+ export function isEditableTextInput(element: ReactTestInstance) {
5
+ return isHostTextInput(element) && element.props.editable !== false;
6
+ }
@@ -1,2 +1,6 @@
1
- export * from './events';
1
+ export * from './content-size';
2
+ export * from './dispatch-event';
3
+ export * from './host-components';
4
+ export * from './text-range';
2
5
  export * from './wait';
6
+ export * from './warn-about-real-timers';
@@ -0,0 +1,4 @@
1
+ export interface TextRange {
2
+ start: number;
3
+ end: number;
4
+ }
@@ -1,4 +1,11 @@
1
- export const warnAboutRealTimers = () => {
1
+ import { jestFakeTimersAreEnabled } from '../../helpers/timers';
2
+
3
+ export const warnAboutRealTimersIfNeeded = () => {
4
+ const areFakeTimersEnabled = jestFakeTimersAreEnabled();
5
+ if (areFakeTimersEnabled) {
6
+ return;
7
+ }
8
+
2
9
  // eslint-disable-next-line no-console
3
10
  console.warn(`It is recommended to use userEvent with fake timers
4
11
  Some events involve duration so your tests may take a long time to run.
@@ -7,20 +7,13 @@ title: API
7
7
 
8
8
  - [`render`](#render)
9
9
  - [`render` options](#render-options)
10
- - [`wrapper` option](#wrapper-option)
11
- - [`createNodeMock` option](#createnodemock-option)
12
- - [`unstable_validateStringsRenderedWithinText` option](#unstable_validatestringsrenderedwithintext-option)
13
10
  - [`...queries`](#queries)
14
- - [Example](#example)
15
11
  - [`update`](#update)
16
12
  - [`unmount`](#unmount)
17
13
  - [`debug`](#debug)
18
- - [`message` option](#message-option)
19
- - [`mapProps` option](#mapprops-option)
20
- - [`debug.shallow`](#debugshallow)
21
14
  - [`toJSON`](#tojson)
22
15
  - [`root`](#root)
23
- - [`UNSAFE_root`](#unsafe_root)
16
+ - [`UNSAFE_root`](#unsaferoot)
24
17
  - [`screen`](#screen)
25
18
  - [`cleanup`](#cleanup)
26
19
  - [`fireEvent`](#fireevent)
@@ -28,10 +21,8 @@ title: API
28
21
  - [`fireEvent.press`](#fireeventpress)
29
22
  - [`fireEvent.changeText`](#fireeventchangetext)
30
23
  - [`fireEvent.scroll`](#fireeventscroll)
31
- - [On a `ScrollView`](#on-a-scrollview)
32
- - [On a `FlatList`](#on-a-flatlist)
33
24
  - [`waitFor`](#waitfor)
34
- - [Using Jest fake timers](#using-jest-fake-timers)
25
+ - [Using a React Native version \< 0.71 with Jest fake timers](#using-a-react-native-version--071-with-jest-fake-timers)
35
26
  - [`waitForElementToBeRemoved`](#waitforelementtoberemoved)
36
27
  - [`within`, `getQueriesForElement`](#within-getqueriesforelement)
37
28
  - [`queryBy*` APIs](#queryby-apis)
@@ -40,24 +31,12 @@ title: API
40
31
  - [`renderHook`](#renderhook)
41
32
  - [`callback`](#callback)
42
33
  - [`options` (Optional)](#options-optional)
43
- - [`initialProps`](#initialprops)
44
- - [`wrapper`](#wrapper)
45
34
  - [`RenderHookResult` object](#renderhookresult-object)
46
- - [`result`](#result)
47
- - [`rerender`](#rerender)
48
- - [`unmount`](#unmount-1)
49
35
  - [Examples](#examples)
50
- - [With `initialProps`](#with-initialprops)
51
- - [With `wrapper`](#with-wrapper)
52
36
  - [Configuration](#configuration)
53
37
  - [`configure`](#configure)
54
- - [`asyncUtilTimeout` option](#asyncutiltimeout-option)
55
- - [`defaultIncludeHiddenElements` option](#defaultincludehiddenelements-option)
56
- - [`defaultDebugOptions` option](#defaultdebugoptions-option)
57
38
  - [`resetToDefaults()`](#resettodefaults)
58
39
  - [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
40
  - [Accessibility](#accessibility)
62
41
  - [`isHiddenFromAccessibility`](#ishiddenfromaccessibility)
63
42
 
@@ -346,9 +325,16 @@ function fireEvent(
346
325
  ): void {}
347
326
  ```
348
327
 
349
- Fires native-like event with data.
328
+ :::note
329
+ For common events like `press` or `type` it's recommended to use [User Event API](UserEvent.md) as it offers
330
+ more realistic event simulation by emitting a sequence of events with proper event objects that mimic React Native runtime behavior.
331
+
332
+ Use Fire Event for cases not supported by User Event and for triggering event handlers on composite components.
333
+ :::
350
334
 
351
- Invokes a given event handler (whether native or custom) on the element, bubbling to the root of the rendered tree.
335
+ `fireEvent` API allows you to trigger all kind of event handlers on both host and composite components. It will try to invoke a single event handler traversing the component tree bottom-up from passed element and trying to find enabled event handler named `onXxx` when `xxx` is the name of the event passed.
336
+
337
+ Unlike User Event, this API does not automatically pass event object to event handler, this is responsibility of the user to construct such object.
352
338
 
353
339
  ```jsx
354
340
  import { render, screen, fireEvent } from '@testing-library/react-native';
@@ -402,6 +388,10 @@ Convenience methods for common events like: `press`, `changeText`, `scroll`.
402
388
  fireEvent.press: (element: ReactTestInstance, ...data: Array<any>) => void
403
389
  ```
404
390
 
391
+ :::note
392
+ It is recommended to use the User Event [`press()`](UserEvent.md#press) helper instead as it offers more realistic simulation of press interaction, including pressable support.
393
+ :::
394
+
405
395
  Invokes `press` event handler on the element or parent element in the tree.
406
396
 
407
397
  ```jsx
@@ -434,6 +424,10 @@ expect(onPressMock).toHaveBeenCalledWith(eventData);
434
424
  fireEvent.changeText: (element: ReactTestInstance, ...data: Array<any>) => void
435
425
  ```
436
426
 
427
+ :::note
428
+ It is recommended to use the User Event [`type()`](UserEvent.md#type) helper instead as it offers more realistic simulation of text change interaction, including key-by-key typing, element focus, and other editing events.
429
+ :::
430
+
437
431
  Invokes `changeText` event handler on the element or parent element in the tree.
438
432
 
439
433
  ```jsx
@@ -14,17 +14,14 @@ title: Queries
14
14
  - [findAllBy](#findallby)
15
15
  - [Queries](#queries)
16
16
  - [Options](#options)
17
+ - [`ByRole`](#byrole)
17
18
  - [`ByText`](#bytext)
18
19
  - [`ByPlaceholderText`](#byplaceholdertext)
19
20
  - [`ByDisplayValue`](#bydisplayvalue)
20
21
  - [`ByTestId`](#bytestid)
21
22
  - [`ByLabelText`](#bylabeltext)
22
23
  - [`ByHintText`, `ByA11yHint`, `ByAccessibilityHint`](#byhinttext-bya11yhint-byaccessibilityhint)
23
- - [`ByRole`](#byrole)
24
- - [Options](#options-1)
25
24
  - [`ByA11yState`, `ByAccessibilityState` (deprecated)](#bya11ystate-byaccessibilitystate-deprecated)
26
- - [Default state for: `disabled`, `selected`, and `busy` keys](#default-state-for-disabled-selected-and-busy-keys)
27
- - [Default state for: `checked` and `expanded` keys](#default-state-for-checked-and-expanded-keys)
28
25
  - [`ByA11yValue`, `ByAccessibilityValue` (deprecated)](#bya11yvalue-byaccessibilityvalue-deprecated)
29
26
  - [Common options](#common-options)
30
27
  - [`includeHiddenElements` option](#includehiddenelements-option)
@@ -32,7 +29,6 @@ title: Queries
32
29
  - [Examples](#examples)
33
30
  - [Precision](#precision)
34
31
  - [Normalization](#normalization)
35
- - [Normalization Examples](#normalization-examples)
36
32
  - [Unit testing helpers](#unit-testing-helpers)
37
33
  - [`UNSAFE_ByType`](#unsafe_bytype)
38
34
  - [`UNSAFE_ByProps`](#unsafe_byprops)
@@ -95,6 +91,69 @@ type ReactTestInstance = {
95
91
 
96
92
  Usually query first argument can be a **string** or a **regex**. All queries take at least the [`hidden`](#hidden-option) option as an optionnal second argument and some queries accept more options which change string matching behaviour. See [TextMatch](#textmatch) for more info.
97
93
 
94
+ ### `ByRole`
95
+
96
+ > getByRole, getAllByRole, queryByRole, queryAllByRole, findByRole, findAllByRole
97
+
98
+ ```ts
99
+ getByRole(
100
+ role: TextMatch,
101
+ options?: {
102
+ name?: TextMatch
103
+ disabled?: boolean,
104
+ selected?: boolean,
105
+ checked?: boolean | 'mixed',
106
+ busy?: boolean,
107
+ expanded?: boolean,
108
+ value: {
109
+ min?: number;
110
+ max?: number;
111
+ now?: number;
112
+ text?: TextMatch;
113
+ },
114
+ includeHiddenElements?: boolean;
115
+ }
116
+ ): ReactTestInstance;
117
+ ```
118
+
119
+ Returns a `ReactTestInstance` with matching `accessibilityRole` prop.
120
+
121
+ :::info
122
+ In order for `*ByRole` queries to match an element it needs to be considered an accessibility element:
123
+ 1. `Text`, `TextInput` and `Switch` host elements are these by default.
124
+ 2. `View` host elements need an explicit [`accessible`](https://reactnative.dev/docs/accessibility#accessible) prop set to `true`
125
+ 3. Some React Native composite components like `Pressable` & `TouchableOpacity` render host `View` element with `accessible` prop already set.
126
+ :::
127
+
128
+ ```jsx
129
+ import { render, screen } from '@testing-library/react-native';
130
+
131
+ render(
132
+ <Pressable accessibilityRole="button" disabled>
133
+ <Text>Hello</Text>
134
+ </Pressable>
135
+ );
136
+ const element = screen.getByRole('button');
137
+ const element2 = screen.getByRole('button', { name: 'Hello' });
138
+ const element3 = screen.getByRole('button', { name: 'Hello', disabled: true });
139
+ ```
140
+
141
+ #### Options
142
+
143
+ `name`: Finds an element with given `accessibilityRole` and an accessible name (equivalent to `byText` or `byLabelText` query).
144
+
145
+ `disabled`: You can filter elements by their disabled state. The possible values are `true` or `false`. Querying `disabled: false` will also match elements with `disabled: undefined` (see the [wiki](https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State) for more details). See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `disabled` state.
146
+
147
+ `selected`: You can filter elements by their selected state. The possible values are `true` or `false`. Querying `selected: false` will also match elements with `selected: undefined` (see the [wiki](https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State) for more details). See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `selected` state.
148
+
149
+ `checked`: You can filter elements by their checked state. The possible values are `true`, `false`, or `"mixed"`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `checked` state.
150
+
151
+ `busy`: You can filter elements by their busy state. The possible values are `true` or `false`. Querying `busy: false` will also match elements with `busy: undefined` (see the [wiki](https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State) for more details). See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `busy` state.
152
+
153
+ `expanded`: You can filter elements by their expanded state. The possible values are `true` or `false`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `expanded` state.
154
+
155
+ `value`: Filter elements by their accessibility, available value entries include numeric `min`, `max` & `now`, as well as string or regex `text` key. See React Native [accessibilityValue](https://reactnative.dev/docs/accessibility#accessibilityvalue) docs to learn more about this prop.
156
+
98
157
  ### `ByText`
99
158
 
100
159
  > getByText, getAllByText, queryByText, queryAllByText, findByText, findAllByText
@@ -253,61 +312,7 @@ const element = screen.getByHintText('Plays a song');
253
312
  Please consult [Apple guidelines on how `accessibilityHint` should be used](https://developer.apple.com/documentation/objectivec/nsobject/1615093-accessibilityhint).
254
313
  :::
255
314
 
256
- ### `ByRole`
257
-
258
- > getByRole, getAllByRole, queryByRole, queryAllByRole, findByRole, findAllByRole
259
-
260
- ```ts
261
- getByRole(
262
- role: TextMatch,
263
- options?: {
264
- name?: TextMatch
265
- disabled?: boolean,
266
- selected?: boolean,
267
- checked?: boolean | 'mixed',
268
- busy?: boolean,
269
- expanded?: boolean,
270
- value: {
271
- min?: number;
272
- max?: number;
273
- now?: number;
274
- text?: TextMatch;
275
- },
276
- includeHiddenElements?: boolean;
277
- }
278
- ): ReactTestInstance;
279
- ```
280
-
281
- Returns a `ReactTestInstance` with matching `accessibilityRole` prop.
282
315
 
283
- ```jsx
284
- import { render, screen } from '@testing-library/react-native';
285
-
286
- render(
287
- <Pressable accessibilityRole="button" disabled>
288
- <Text>Hello</Text>
289
- </Pressable>
290
- );
291
- const element = screen.getByRole('button');
292
- const element2 = screen.getByRole('button', { name: 'Hello' });
293
- const element3 = screen.getByRole('button', { name: 'Hello', disabled: true });
294
- ```
295
-
296
- #### Options
297
-
298
- `name`: Finds an element with given `accessibilityRole` and an accessible name (equivalent to `byText` or `byLabelText` query).
299
-
300
- `disabled`: You can filter elements by their disabled state. The possible values are `true` or `false`. Querying `disabled: false` will also match elements with `disabled: undefined` (see the [wiki](https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State) for more details). See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `disabled` state.
301
-
302
- `selected`: You can filter elements by their selected state. The possible values are `true` or `false`. Querying `selected: false` will also match elements with `selected: undefined` (see the [wiki](https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State) for more details). See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `selected` state.
303
-
304
- `checked`: You can filter elements by their checked state. The possible values are `true`, `false`, or `"mixed"`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `checked` state.
305
-
306
- `busy`: You can filter elements by their busy state. The possible values are `true` or `false`. Querying `busy: false` will also match elements with `busy: undefined` (see the [wiki](https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State) for more details). See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `busy` state.
307
-
308
- `expanded`: You can filter elements by their expanded state. The possible values are `true` or `false`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `expanded` state.
309
-
310
- `value`: Filter elements by their accessibility, available value entries include numeric `min`, `max` & `now`, as well as string or regex `text` key. See React Native [accessibilityValue](https://reactnative.dev/docs/accessibility#accessibilityvalue) docs to learn more about this prop.
311
316
 
312
317
  ### `ByA11yState`, `ByAccessibilityState` (deprecated)
313
318
 
@@ -5,11 +5,33 @@ title: User Event
5
5
 
6
6
  ### Table of contents
7
7
 
8
- - [`userEvent.setup`](#usereventsetup)
8
+ - [Comparison with Fire Event API](#comparison-with-fire-event-api)
9
+ - [`setup()`](#setup)
9
10
  - [Options](#options)
11
+ - [`press()`](#press)
12
+ - [`longPress()`](#longpress)
13
+ - [Options](#options-1)
14
+ - [`type()`](#type)
15
+ - [Options](#options-2)
16
+ - [Sequence of events](#sequence-of-events)
17
+ - [`clear()`](#clear)
18
+ - [Sequence of events](#sequence-of-events-1)
10
19
 
20
+ :::caution
21
+ User Event API is in beta stage.
11
22
 
12
- ## `userEvent.setup`
23
+ This means that we plan to keep the public API signatures to remain stable, but we might introduce breaking behavioural changes, e.g. changing the ordering or timing of emitted events, without a major version update. Hopefully, well written code should not rely on such specific details.
24
+ :::
25
+
26
+ ## Comparison with Fire Event API
27
+
28
+ Fire Event is our original event simulation API. It offers ability to invoke **any event handler** declared on **either host or composite elements**. If the element does not have `onEventName` event handler for passed `eventName` event, or the element is disabled, Fire Event will traverse up the component tree, looking for event handler on both host and composite elements along the way. By default it will **not pass any event data**, but the user might provide it in the last argument.
29
+
30
+ In contrast, User Event provides realistic event simulation for main user interactions like `press` or `type`. Each of the interactions will trigger a **sequence of events** corresponding to React Native runtime behavior. These events will be invoked **only on host elements**, and **will automatically receive event data** corresponding to each event.
31
+
32
+ If User Event supports given interaction you should always prefer it over Fire Event counterpart, as it will make your tests much more realistic and hence reliable. In other cases, e.g. when event is not supported by User Event, or when invoking event handlers on composite elements, you have to use Fire Event as the only available option.
33
+
34
+ ## `setup()`
13
35
 
14
36
  ```ts
15
37
  userEvent.setup(options?: {
@@ -23,11 +45,11 @@ Example
23
45
  const user = userEvent.setup();
24
46
  ```
25
47
 
26
- Creates User Event instances which can be used to trigger events.
48
+ Creates an User Event object instance which can be used to trigger events.
27
49
 
28
50
  ### Options
29
- - `delay` - controls the default delay between subsequent events, e.g. keystrokes, etc.
30
- - `advanceTimers` - time advancement utility function that should be used for fake timers. The default setup handles both real and Jest fake timers.
51
+ - `delay` - controls the default delay between subsequent events, e.g. keystrokes.
52
+ - `advanceTimers` - time advancement utility function that should be used for fake timers. The default setup handles both real timers and Jest fake timers.
31
53
 
32
54
 
33
55
  ## `press()`
@@ -41,11 +63,10 @@ press(
41
63
  Example
42
64
  ```ts
43
65
  const user = userEvent.setup();
44
-
45
66
  await user.press(element);
46
67
  ```
47
68
 
48
- This helper simulates a press on any pressable element, e.g. `Pressable`, `TouchableOpacity`, `Text`, `TextInput`, etc. Unlike `fireEvent.press()` which is a simpler API that will only call the `onPress` prop, this simulates the entire press event in a more realistic way by reproducing what really happens when a user presses an interface view. This will trigger additional events like `pressIn` and `pressOut`.
69
+ This helper simulates a press on any pressable element, e.g. `Pressable`, `TouchableOpacity`, `Text`, `TextInput`, etc. Unlike `fireEvent.press()` which is a simpler API that will only call the `onPress` prop, this function simulates the entire press interaction in a more realistic way by reproducing event sequence emitted by React Native runtime. This helper will trigger additional events like `pressIn` and `pressOut`.
49
70
 
50
71
  ## `longPress()`
51
72
 
@@ -59,8 +80,112 @@ longPress(
59
80
  Example
60
81
  ```ts
61
82
  const user = userEvent.setup();
62
-
63
83
  await user.longPress(element);
64
84
  ```
65
85
 
66
- Simulates a long press user interaction. In React Native the `longPress` event is emitted when the press duration exceeds long press threshold (by default 500 ms). In other aspects this actions behaves similar to regular `press` action, e.g. by emitting `pressIn` and `pressOut` events. The press duration is customisable through the options. This should be useful if you use the `delayLongPress` prop. When using real timers this will take 500 ms so it is highly recommended to use that API with fake timers to prevent test taking a long time to run.
86
+ Simulates a long press user interaction. In React Native the `longPress` event is emitted when the press duration exceeds long press threshold (by default 500 ms). In other aspects this actions behaves similar to regular `press` action, e.g. by emitting `pressIn` and `pressOut` events. The press duration is customisable through the options. This should be useful if you use the `delayLongPress` prop. When using real timers this will take 500 ms so it is highly recommended to use that API with fake timers to prevent test taking a long time to run.
87
+
88
+ ### Options
89
+ - `duration` - duration of the press in miliseconds. Default value is 500 ms.
90
+
91
+ ## `type()`
92
+
93
+ ```ts
94
+ type(
95
+ element: ReactTestInstance,
96
+ text: string,
97
+ options?: {
98
+ skipPress?: boolean
99
+ submitEditing?: boolean
100
+ }
101
+ ```
102
+
103
+ Example
104
+ ```ts
105
+ const user = userEvent.setup();
106
+ await user.type(textInput, "Hello world!");
107
+ ```
108
+
109
+ This helper simulates user focusing on `TextInput` element, typing `text` one character at a time, and leaving the element.
110
+
111
+ This function supports only host `TextInput` elements. Passing other element type will result in throwing error.
112
+
113
+ :::note
114
+ This function will add text to the text already present in the text input (as specified by `value` or `defaultValue` props). In order to replace existing text, use [`clear()`](#clear) helper first.
115
+ :::
116
+
117
+ ### Options
118
+ - `skipPress` - if true, `pressIn` and `pressOut` events will not be triggered.
119
+ - `submitEditing` - if true, `submitEditing` event will be triggered after typing the text.
120
+
121
+ ### Sequence of events
122
+
123
+ The sequence of events depends on `multiline` prop, as well as passed options.
124
+
125
+ Events will not be emitted if `editable` prop is set to `false`.
126
+
127
+ **Entering the element**:
128
+ - `pressIn` (optional)
129
+ - `focus`
130
+ - `pressOut` (optional)
131
+
132
+ The `pressIn` and `pressOut` events are sent by default, but can be skipped by passing `skipPress: true` option.
133
+
134
+ **Typing (for each character)**:
135
+ - `keyPress`
136
+ - `textInput` (optional)
137
+ - `change`
138
+ - `changeText`
139
+ - `selectionChange`
140
+
141
+ The `textInput` event is sent only for mutliline text inputs.
142
+
143
+ **Leaving the element**:
144
+ - `submitEditing` (optional)
145
+ - `endEditing`
146
+ - `blur`
147
+
148
+ The `submitEditing` event is skipped by default. It can sent by setting `submitEditing: true` option.
149
+
150
+ ## `clear()`
151
+
152
+ ```ts
153
+ clear(
154
+ element: ReactTestInstance,
155
+ }
156
+ ```
157
+
158
+ Example
159
+ ```ts
160
+ const user = userEvent.setup();
161
+ await user.clear(textInput);
162
+ ```
163
+
164
+ This helper simulates user clearing content of `TextInput` element.
165
+
166
+ This function supports only host `TextInput` elements. Passing other element type will result in throwing error.
167
+
168
+ ### Sequence of events
169
+
170
+ The sequence of events depends on `multiline` prop, as well as passed options.
171
+
172
+ Events will not be emitted if `editable` prop is set to `false`.
173
+
174
+ **Entering the element**:
175
+ - `focus`
176
+
177
+ **Selecting all content**:
178
+ - `selectionChange`
179
+
180
+ **Pressing backspace**:
181
+ - `keyPress`
182
+ - `textInput` (optional)
183
+ - `change`
184
+ - `changeText`
185
+ - `selectionChange`
186
+
187
+ The `textInput` event is sent only for mutliline text inputs.
188
+
189
+ **Leaving the element**:
190
+ - `endEditing`
191
+ - `blur`
@@ -1,7 +1,7 @@
1
1
  module.exports = {
2
2
  docs: {
3
3
  Introduction: ['getting-started', 'faq'],
4
- 'API Reference': ['api', 'api-queries'],
4
+ 'API Reference': ['api', 'api-queries', 'user-event'],
5
5
  Guides: [
6
6
  'troubleshooting',
7
7
  'how-should-i-query',
@@ -1,3 +0,0 @@
1
- import type { ReactTestInstance } from 'react-test-renderer';
2
- import * as React from 'react';
3
- export declare const filterNodeByType: (node: ReactTestInstance | React.ReactElement, type: React.ElementType | string) => boolean;
@@ -1,9 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.filterNodeByType = void 0;
7
- const filterNodeByType = (node, type) => node.type === type;
8
- exports.filterNodeByType = filterNodeByType;
9
- //# sourceMappingURL=filterNodeByType.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"filterNodeByType.js","names":["filterNodeByType","node","type","exports"],"sources":["../../src/helpers/filterNodeByType.ts"],"sourcesContent":["import type { ReactTestInstance } from 'react-test-renderer';\nimport * as React from 'react';\n\nexport const filterNodeByType = (\n node: ReactTestInstance | React.ReactElement,\n type: React.ElementType | string\n) => node.type === type;\n"],"mappings":";;;;;;AAGO,MAAMA,gBAAgB,GAAGA,CAC9BC,IAA4C,EAC5CC,IAAgC,KAC7BD,IAAI,CAACC,IAAI,KAAKA,IAAI;AAACC,OAAA,CAAAH,gBAAA,GAAAA,gBAAA"}
@@ -1 +0,0 @@
1
- export declare const warnAboutRealTimers: () => void;
@@ -1,14 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.warnAboutRealTimers = void 0;
7
- const warnAboutRealTimers = () => {
8
- // eslint-disable-next-line no-console
9
- console.warn(`It is recommended to use userEvent with fake timers
10
- Some events involve duration so your tests may take a long time to run.
11
- For instance calling userEvent.longPress with real timers will take 500 ms.`);
12
- };
13
- exports.warnAboutRealTimers = warnAboutRealTimers;
14
- //# sourceMappingURL=warnAboutRealTimers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"warnAboutRealTimers.js","names":["warnAboutRealTimers","console","warn","exports"],"sources":["../../../../src/user-event/press/utils/warnAboutRealTimers.ts"],"sourcesContent":["export const warnAboutRealTimers = () => {\n // eslint-disable-next-line no-console\n console.warn(`It is recommended to use userEvent with fake timers\nSome events involve duration so your tests may take a long time to run.\nFor instance calling userEvent.longPress with real timers will take 500 ms.`);\n};\n"],"mappings":";;;;;;AAAO,MAAMA,mBAAmB,GAAGA,CAAA,KAAM;EACvC;EACAC,OAAO,CAACC,IAAI,CAAE;AAChB;AACA,4EAA4E,CAAC;AAC7E,CAAC;AAACC,OAAA,CAAAH,mBAAA,GAAAA,mBAAA"}