Skip to content
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

fix: do not bubble event to root element #30

Merged
merged 1 commit into from
Oct 15, 2018
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
23 changes: 19 additions & 4 deletions src/__tests__/fireEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const CustomEventComponent = ({ onCustomEvent }) => (
</TouchableOpacity>
);

const DoublePressComponent = ({ onDoublePress }) => (
<TouchableOpacity onDoublePress={onDoublePress}>
<Text testID="button-text">Click me</Text>
</TouchableOpacity>
);

describe('fireEvent', () => {
test('should invoke specified event', () => {
const onPressMock = jest.fn();
Expand Down Expand Up @@ -70,6 +76,18 @@ describe('fireEvent', () => {

expect(handlerMock).toHaveBeenCalledWith(EVENT_DATA);
});

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();
});
});

test('fireEvent.press', () => {
Expand Down Expand Up @@ -104,11 +122,8 @@ test('fireEvent.scroll', () => {

test('fireEvent.doublePress', () => {
const onDoublePressMock = jest.fn();

const { getByTestId } = render(
<TouchableOpacity onDoublePress={onDoublePressMock}>
<Text testID="button-text">Click me</Text>
</TouchableOpacity>
<DoublePressComponent onDoublePress={onDoublePressMock} />
);

fireEvent.doublePress(getByTestId('button-text'));
Expand Down
3 changes: 2 additions & 1 deletion src/fireEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const findEventHandler = (element: ReactTestInstance, eventName: string) => {
return element.props[eventHandler];
}

if (element.parent === null) {
// Do not bubble event to the root element
if (element.parent === null || element.parent.parent === null) {
throw new ErrorWithStack(
`No handler function found for event: ${eventName}`,
invokeEvent
Expand Down