Skip to content

feat: support pointerEvents #655

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 6 commits into from
Feb 5, 2021
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
77 changes: 77 additions & 0 deletions src/__tests__/fireEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,83 @@ test('should not fire on non-editable TextInput with nested Text', () => {
expect(onChangeTextMock).not.toHaveBeenCalled();
});

test('should not fire on none pointerEvents View', () => {
const handlePress = jest.fn();

const screen = render(
<View pointerEvents="none">
<Pressable onPress={handlePress}>
<Text>Trigger</Text>
</Pressable>
</View>
);

fireEvent.press(screen.getByText('Trigger'));
expect(handlePress).not.toHaveBeenCalled();
});

test('should not fire on box-only pointerEvents View', () => {
const handlePress = jest.fn();

const screen = render(
<View pointerEvents="box-only">
<Pressable onPress={handlePress}>
<Text>Trigger</Text>
</Pressable>
</View>
);

fireEvent.press(screen.getByText('Trigger'));
expect(handlePress).not.toHaveBeenCalled();
});

test('should fire on box-none pointerEvents View', () => {
const handlePress = jest.fn();

const screen = render(
<View pointerEvents="box-none">
<Pressable onPress={handlePress}>
<Text>Trigger</Text>
</Pressable>
</View>
);

fireEvent.press(screen.getByText('Trigger'));
expect(handlePress).toHaveBeenCalled();
});

test('should fire on auto pointerEvents View', () => {
const handlePress = jest.fn();

const screen = render(
<View pointerEvents="auto">
<Pressable onPress={handlePress}>
<Text>Trigger</Text>
</Pressable>
</View>
);

fireEvent.press(screen.getByText('Trigger'));
expect(handlePress).toHaveBeenCalled();
});

test('should not fire on box-only pointerEvents View with nested elements', () => {
const handlePress = jest.fn();

const screen = render(
<View pointerEvents="box-only">
<View>
<Pressable onPress={handlePress}>
<Text>Trigger</Text>
</Pressable>
</View>
</View>
);

fireEvent.press(screen.getByText('Trigger'));
expect(handlePress).not.toHaveBeenCalled();
});

test('should pass event up on disabled TouchableOpacity', () => {
const handleInnerPress = jest.fn();
const handleOuterPress = jest.fn();
Expand Down
18 changes: 18 additions & 0 deletions src/fireEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,29 @@ const isTouchResponder = (element?: ReactTestInstance) => {
return !!element?.props.onStartShouldSetResponder || isTextInput(element);
};

const isPointerEventEnabled = (
element?: ReactTestInstance,
isParent?: boolean
) => {
const parentCondition = isParent
? element?.props.pointerEvents === 'box-only'
: element?.props.pointerEvents === 'box-none';

if (element?.props.pointerEvents === 'none' || parentCondition) {
return false;
}

if (!element?.parent) return true;

return isPointerEventEnabled(element.parent, true);
};

const isEventEnabled = (
element?: ReactTestInstance,
touchResponder?: ReactTestInstance
) => {
if (isTextInput(element)) return element?.props.editable !== false;
if (!isPointerEventEnabled(element)) return false;

const touchStart = touchResponder?.props.onStartShouldSetResponder?.();
const touchMove = touchResponder?.props.onMoveShouldSetResponder?.();
Expand Down