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

Conversation

danahartweg
Copy link
Contributor

Summary

I personally encountered some confusion with the fireEvent api (via #100), and didn't immediately realize events did not require an aliased to be fired.

This PR adds some documentation to help make that api as clear as possible for new users.

@thymikee
Copy link
Member

It may confuse users though that you wrap component in a View. To avoid confusion can we get rid of props inside MyComponent and provide its basic implementation with the testID, placeholder, etc?

@thymikee
Copy link
Member

And thanks for adjusting that, because current example wouldn't work if there's no components reacting to that event (as we omit the root on purpose because the handler could not be called unless that's a host component)

@danahartweg
Copy link
Contributor Author

I'm not entirely sure I understand what you're suggesting. To the best of my knowledge and testing, the below will work just fine (which is basically what the example was before I modified it by wrapping in a View) regardless of the underlying implementation of MyComponent.

import { render, fireEvent } from 'react-native-testing-library;
import { MyComponent } from './MyComponent';

const onEventMock = jest.fn();
const { getByTestId } = render(
  <MyComponent testID="custom" onMyCustomEvent={onEventMock} />
);

fireEvent(getByTestId('custom'), 'myCustomEvent');

Are you suggesting a more in-depth example where a child in the component has an event that (once fired) would call an event (passed as a prop) on its parent?

@thymikee
Copy link
Member

Sorry for the confusion, I was thinking about this case (nor really relevant here though):

test('should not bubble event to root element', () => {
const onPressMock = jest.fn();
const { getByTestId } = render(
<TouchableOpacity onPress={onPressMock}>
<Text testID="test">Content</Text>
</TouchableOpacity>
);
expect(() => fireEvent.press(getByTestId('test'))).toThrowError();
expect(onPressMock).not.toHaveBeenCalled();
});
});

It just seems like extra View wrapper is unnecessary obfuscation of the example you provided, but will run just fine. Mind removing it?

@danahartweg
Copy link
Contributor Author

danahartweg commented Dec 27, 2018

@thymikee aaaahhh, I see what you were going for now. Aaaaaanyway, the extra <View /> and now unnecessary import have been removed.

@thymikee thymikee merged commit af067d0 into callstack:master Dec 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants