Skip to content

Add more details to the fireEvent api documentation #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,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';

Expand All @@ -159,6 +158,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.
Expand Down