mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
docs: Add more details to the fireEvent api documentation. (#102)
This commit is contained in:
committed by
Michał Pierzchała
parent
6d4d426d41
commit
af067d0712
+18
-2
@@ -148,14 +148,13 @@ test('Component has a structure', () => {
|
||||
|
||||
## `fireEvent`
|
||||
|
||||
Invokes given event handler on the element bubbling to the root of the rendered tree.
|
||||
Invokes a given event handler (whether native or custom) on the element, bubbling to the root of the rendered tree. The three most common events (`press`, `changeText`, and `scroll`) have been aliased for convenience.
|
||||
|
||||
### `fireEvent: (element: ReactTestInstance, eventName: string, data?: *) => void`
|
||||
|
||||
Invokes named event handler on the element or parent element in the tree. For better readability, `fireEvent` strips the `on` part of the handler prop name, so it will fire `onMyCustomEvent` when `myCustomEvent` is passed as `eventName`.
|
||||
|
||||
```jsx
|
||||
import { View } from 'react-native';
|
||||
import { render, fireEvent } from 'react-native-testing-library';
|
||||
import { MyComponent } from './MyComponent';
|
||||
|
||||
@@ -167,6 +166,23 @@ const { getByTestId } = render(
|
||||
fireEvent(getByTestId('custom'), 'myCustomEvent');
|
||||
```
|
||||
|
||||
An example using `fireEvent` with native events that aren't already aliased by the `fireEvent` api.
|
||||
|
||||
```jsx
|
||||
import { TextInput, View } from 'react-native';
|
||||
import { fireEvent, render } from 'react-native-testing-library';
|
||||
|
||||
const onBlurMock = jest.fn();
|
||||
|
||||
const { getByPlaceholder } = render(
|
||||
<View>
|
||||
<TextInput placeholder="my placeholder" onBlur={onBlurMock} />
|
||||
</View>
|
||||
);
|
||||
|
||||
fireEvent(getByPlaceholder('my placeholder'), 'blur');
|
||||
```
|
||||
|
||||
### `fireEvent.press: (element: ReactTestInstance) => void`
|
||||
|
||||
Invokes `press` event handler on the element or parent element in the tree.
|
||||
|
||||
Reference in New Issue
Block a user