Movatterモバイル変換


[0]ホーム

URL:


logo
React Native Testing Library
13.x
logo
React Native Testing Library

User Event interactions

Comparison with Fire Event API

Fire Event is our original event simulation API. It can invokeany event handler declared oneither host or composite elements. Suppose the element does not haveonEventName event handler for the passedeventName event, or the element is disabled. In that case, Fire Event will traverse up the component tree, looking for an event handler on both host and composite elements along the way. By default, it willnot pass any event data, but the user might provide it in the last argument.

In contrast, User Event provides realistic event simulation for user interactions likepress ortype. Each interaction will trigger asequence of events corresponding to React Native runtime behavior. These events will be invokedonly on host elements, andwill automatically receive event data corresponding to each event.

If User Event supports a given interaction, you should always prefer it over the Fire Event counterpart, as it will make your tests much more realistic and, hence, reliable. In other cases, e.g., when User Event does not support the given event or when invoking event handlers on composite elements, you have to use Fire Event as the only available option.

setup()

userEvent.setup(options?:{  delay:number;advanceTimers:(delay:number)=>Promise<void>|void;})

Example

const user= userEvent.setup();

Creates a User Event object instance, which can be used to trigger events.

Options

  • delay controls the default delay between subsequent events, e.g., keystrokes.
  • advanceTimers is a time advancement utility function that should be used for fake timers. The default setup handles both real timers and Jest fake timers.

press()

press(  element: ReactTestInstance,):Promise<void>

Example

const user= userEvent.setup();await user.press(element);

This helper simulates a press on any pressable element, e.g.Pressable,TouchableOpacity,Text,TextInput, etc. UnlikefireEvent.press(), a more straightforward API that will only call theonPress prop, this function simulates the entire press interaction in a more realistic way by reproducing the event sequence emitted by React Native runtime. This helper will trigger additional events likepressIn andpressOut.

This event will take a minimum of 130 ms to run due to the internal React Native logic. Consider using fake timers to speed up test execution for tests involvingpress andlongPress interactions.

longPress()

longPress(  element: ReactTestInstance,  options:{ duration:number}={ duration:500}):Promise<void>

Example

const user= userEvent.setup();await user.longPress(element);

Simulates a long press user interaction. In React Native, thelongPress event is emitted when the press duration exceeds the long press threshold (by default, 500 ms). In other aspects, this action behaves similarly to regularpress action, e.g., by emittingpressIn andpressOut events. The press duration is customizable through the options. This should be useful if you use thedelayLongPress prop.

This event will, by default, take 500 ms to run. Due to internal React Native logic, it will take at least 130 ms regardless of the duration option passed. Consider using fake timers to speed up test execution for tests involvingpress andlongPress interactions.

Options

  • duration - duration of the press in milliseconds. The default value is 500 ms.

type()

type(  element: ReactTestInstance,  text:string,  options?:{    skipPress?:boolean;    skipBlur?:boolean;    submitEditing?:boolean;}

Example

const user= userEvent.setup();await user.type(textInput,'Hello world!');

This helper simulates the user focusing on aTextInput element, typingtext one character at a time, and leaving the element.

This function supports only hostTextInput elements. Passing other element types will result in throwing an error.

NOTE

This function will add text to the text already present in the text input (as specified byvalue ordefaultValue props). To replace existing text, useclear() helper first.

Options

  • skipPress - if true,pressIn andpressOut events will not be triggered.
  • skipBlur - if true,endEditing andblur events will not be triggered when typing is complete.
  • submitEditing - if true,submitEditing event will be triggered after typing the text.

Sequence of events

The sequence of events depends on themultiline prop and the passed options.

Events will not be emitted if theeditable prop is set tofalse.

Entering the element:

  • pressIn (optional)
  • focus
  • pressOut (optional)

ThepressIn andpressOut events are sent by default but can be skipped by passing theskipPress: true option.

Typing (for each character):

  • keyPress
  • change
  • changeText
  • selectionChange
  • contentSizeChange (only multiline)

Leaving the element:

  • submitEditing (optional)
  • endEditing
  • blur

ThesubmitEditing event is skipped by default. It can sent by setting thesubmitEditing: true option.TheendEditing andblur events can be skipped by passing theskipBlur: true option.

clear()

clear(  element: ReactTestInstance,)

Example

const user= userEvent.setup();await user.clear(textInput);

This helper simulates the user clearing the content of aTextInput element.

This function supports only hostTextInput elements. Passing other element types will result in throwing an error.

Sequence of events

Events will not be emitted if theeditable prop is set tofalse.

Entering the element:

  • focus

Selecting all content:

  • selectionChange

Pressing backspace:

  • keyPress
  • change
  • changeText
  • selectionChange

Leaving the element:

  • endEditing
  • blur

paste()

paste(  element: ReactTestInstance,  text:string,)

Example

const user= userEvent.setup();await user.paste(textInput,'Text to paste');

This helper simulates the user pasting given text to aTextInput element.

This function supports only hostTextInput elements. Passing other element types will result in throwing an error.

Sequence of events

Events will not be emitted if theeditable prop is set tofalse.

Entering the element:

  • focus

Selecting all content:

  • selectionChange

Pasting the text:

  • change
  • changeText
  • selectionChange

Leaving the element:

  • endEditing
  • blur

scrollTo()

scrollTo(  element: ReactTestInstance,  options:{    y:number,    momentumY?:number,    contentSize?:{ width:number, height:number},    layoutMeasurement?:{ width:number, height:number},}|{    x:number,    momentumX?:number,    contentSize?:{ width:number, height:number},    layoutMeasurement?:{ width:number, height:number},}

Example

const user= userEvent.setup();await user.scrollTo(scrollView,{ y:100, momentumY:200});

This helper simulates the user scrolling a hostScrollView element.

This function supports only hostScrollView elements, passing other element types will result in an error. Note thatFlatList is accepted as it renders to a hostScrollView element.

Scroll interaction should match theScrollView element direction:

  • for a vertical scroll view (default orhorizontal={false}), you should pass only they option (and optionally alsomomentumY).
  • for a horizontal scroll view (horizontal={true}), you should pass only thex option (and optionallymomentumX).

Each scroll interaction consists of a mandatory drag scroll part, which simulates the user dragging the scroll view with his finger (they orx option). This may optionally be followed by a momentum scroll movement, which simulates the inertial movement of scroll view content after the user lifts his finger (momentumY ormomentumX options).

Options

  • y - target vertical drag scroll offset
  • x - target horizontal drag scroll offset
  • momentumY - target vertical momentum scroll offset
  • momentumX - target horizontal momentum scroll offset
  • contentSize - passed toScrollView events and enablingFlatList updates
  • layoutMeasurement - passed toScrollView events and enablingFlatList updates

User Event will generate several intermediate scroll steps to simulate user scroll interaction. You should not rely on exact number or values of these scrolls steps as they might be change in the future version.

This function will remember where the last scroll ended, so subsequent scroll interaction will starts from that position. The initial scroll position will be assumed to be{ y: 0, x: 0 }.

To simulate aFlatList (and other controls based onVirtualizedList) scrolling, you should pass thecontentSize andlayoutMeasurement options, which enable the underlying logic to update the currently visible window.

Sequence of events

The sequence of events depends on whether the scroll includes an optional momentum scroll component.

Drag scroll:

  • contentSizeChange
  • scrollBeginDrag
  • scroll (multiple events)
  • scrollEndDrag

Momentum scroll (optional):

  • momentumScrollBegin
  • scroll (multiple events)
  • momentumScrollEnd

[8]ページ先頭

©2009-2025 Movatter.jp