Skip to content

feat: being able to fire custom events that do not need to start with "on" #147

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
Apr 9, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
*.log
.eslintcache
build
.idea
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Ferran guy XD

20 changes: 20 additions & 0 deletions src/__tests__/fireEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const CustomEventComponent = ({ onCustomEvent }) => (
</TouchableOpacity>
);

const MyCustomButton = ({ handlePress }) => (
<OnPressComponent onPress={handlePress} />
);

const CustomEventComponentWithCustomName = ({ handlePress }) => (
<MyCustomButton testID="my-custom-button" handlePress={handlePress} />
);

describe('fireEvent', () => {
test('should invoke specified event', () => {
const onPressMock = jest.fn();
Expand Down Expand Up @@ -128,3 +136,15 @@ test('fireEvent.changeText', () => {

expect(onChangeTextMock).toHaveBeenCalledWith(CHANGE_TEXT);
});

test('custom component with custom event name', () => {
const handlePress = jest.fn();

const { getByTestId } = render(
<CustomEventComponentWithCustomName handlePress={handlePress} />
);

fireEvent(getByTestId('my-custom-button'), 'handlePress');

expect(handlePress).toHaveBeenCalled();
});
2 changes: 2 additions & 0 deletions src/fireEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const findEventHandler = (element: ReactTestInstance, eventName: string) => {

if (typeof element.props[eventHandler] === 'function') {
return element.props[eventHandler];
} else if (typeof element.props[eventName] === 'function') {
return element.props[eventName];
}

// Do not bubble event to the root element
Expand Down