Skip to content

Commit 2aa6609

Browse files
suezzothymikee
andauthored
feat: support pointerEvents (#655)
* test: add pointerEvents test * feat: add pointerEvents handling * test: add test for nested views * feat: add pointerEvents box-none handling * test: handle auto pointerEvents * Update src/__tests__/fireEvent.test.js Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
1 parent 34fc03a commit 2aa6609

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/__tests__/fireEvent.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,83 @@ test('should not fire on non-editable TextInput with nested Text', () => {
234234
expect(onChangeTextMock).not.toHaveBeenCalled();
235235
});
236236

237+
test('should not fire on none pointerEvents View', () => {
238+
const handlePress = jest.fn();
239+
240+
const screen = render(
241+
<View pointerEvents="none">
242+
<Pressable onPress={handlePress}>
243+
<Text>Trigger</Text>
244+
</Pressable>
245+
</View>
246+
);
247+
248+
fireEvent.press(screen.getByText('Trigger'));
249+
expect(handlePress).not.toHaveBeenCalled();
250+
});
251+
252+
test('should not fire on box-only pointerEvents View', () => {
253+
const handlePress = jest.fn();
254+
255+
const screen = render(
256+
<View pointerEvents="box-only">
257+
<Pressable onPress={handlePress}>
258+
<Text>Trigger</Text>
259+
</Pressable>
260+
</View>
261+
);
262+
263+
fireEvent.press(screen.getByText('Trigger'));
264+
expect(handlePress).not.toHaveBeenCalled();
265+
});
266+
267+
test('should fire on box-none pointerEvents View', () => {
268+
const handlePress = jest.fn();
269+
270+
const screen = render(
271+
<View pointerEvents="box-none">
272+
<Pressable onPress={handlePress}>
273+
<Text>Trigger</Text>
274+
</Pressable>
275+
</View>
276+
);
277+
278+
fireEvent.press(screen.getByText('Trigger'));
279+
expect(handlePress).toHaveBeenCalled();
280+
});
281+
282+
test('should fire on auto pointerEvents View', () => {
283+
const handlePress = jest.fn();
284+
285+
const screen = render(
286+
<View pointerEvents="auto">
287+
<Pressable onPress={handlePress}>
288+
<Text>Trigger</Text>
289+
</Pressable>
290+
</View>
291+
);
292+
293+
fireEvent.press(screen.getByText('Trigger'));
294+
expect(handlePress).toHaveBeenCalled();
295+
});
296+
297+
test('should not fire on box-only pointerEvents View with nested elements', () => {
298+
const handlePress = jest.fn();
299+
300+
const screen = render(
301+
<View pointerEvents="box-only">
302+
<View>
303+
<Pressable onPress={handlePress}>
304+
<Text>Trigger</Text>
305+
</Pressable>
306+
</View>
307+
</View>
308+
);
309+
310+
fireEvent.press(screen.getByText('Trigger'));
311+
expect(handlePress).not.toHaveBeenCalled();
312+
});
313+
237314
test('should pass event up on disabled TouchableOpacity', () => {
238315
const handleInnerPress = jest.fn();
239316
const handleOuterPress = jest.fn();

src/fireEvent.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,29 @@ const isTouchResponder = (element?: ReactTestInstance) => {
1717
return !!element?.props.onStartShouldSetResponder || isTextInput(element);
1818
};
1919

20+
const isPointerEventEnabled = (
21+
element?: ReactTestInstance,
22+
isParent?: boolean
23+
) => {
24+
const parentCondition = isParent
25+
? element?.props.pointerEvents === 'box-only'
26+
: element?.props.pointerEvents === 'box-none';
27+
28+
if (element?.props.pointerEvents === 'none' || parentCondition) {
29+
return false;
30+
}
31+
32+
if (!element?.parent) return true;
33+
34+
return isPointerEventEnabled(element.parent, true);
35+
};
36+
2037
const isEventEnabled = (
2138
element?: ReactTestInstance,
2239
touchResponder?: ReactTestInstance
2340
) => {
2441
if (isTextInput(element)) return element?.props.editable !== false;
42+
if (!isPointerEventEnabled(element)) return false;
2543

2644
const touchStart = touchResponder?.props.onStartShouldSetResponder?.();
2745
const touchMove = touchResponder?.props.onMoveShouldSetResponder?.();

0 commit comments

Comments
 (0)