Skip to content
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
62 changes: 62 additions & 0 deletions packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,65 @@ test('StatefulPressable exposes its press and hover handlers to testing-library'
expect(button.props.testOnly_onHoverIn).toBe(onHoverIn);
expect(button.props.testOnly_onHoverOut).toBe(onHoverOut);
});

test.each([
['Pressable', {}],
// A relation prop routes `Pressable` to the `StatefulPressable` engine.
['StatefulPressable', { simultaneousWith: [] }],
])(
'disabled %s fires none of its callbacks through testing-library',
(_engine, engineProps) => {
// On a device, `disabled` turns the native button off, so none of these
// callbacks fire. Testing-library calls the button's `testOnly_*` props in
// place of the native events, and falls back to handlers further up the
// tree when one is missing. So each of them has to be there and do
// nothing, even when called without an event, as a bare
// `fireEvent(button, 'pressIn')` does.
const callbacks = {
onPress: jest.fn(),
onPressIn: jest.fn(),
onPressOut: jest.fn(),
onLongPress: jest.fn(),
onHoverIn: jest.fn(),
onHoverOut: jest.fn(),
};

const { rerender } = render(
<GestureHandlerRootView>
<Pressable testID="pressable" disabled {...engineProps} {...callbacks}>
<Text>Press Me</Text>
</Pressable>
</GestureHandlerRootView>
);

const { props } = screen.getByTestId('pressable');

for (const name of Object.keys(callbacks)) {
const handler: unknown = props[`testOnly_${name}`];

expect(handler).toEqual(expect.any(Function));
expect(() => (handler as () => void)()).not.toThrow();
}

for (const callback of Object.values(callbacks)) {
expect(callback).not.toHaveBeenCalled();
}

// Callbacks that weren't given stay unset, as on an enabled button, so
// testing-library keeps looking further up the tree, e.g. for an enabled
// `Pressable` around this one.
rerender(
<GestureHandlerRootView>
<Pressable testID="pressable" disabled {...engineProps}>
<Text>Press Me</Text>
</Pressable>
</GestureHandlerRootView>
);

const { props: propsWithoutCallbacks } = screen.getByTestId('pressable');

for (const name of Object.keys(callbacks)) {
expect(propsWithoutCallbacks[`testOnly_${name}`]).toBeUndefined();
}
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { pointerStyle } from './pointerStyle';
import { Touchable } from './Touchable/Touchable';

const IS_TEST_ENV = isTestEnv();
const noop = () => undefined;

// RN's Pressable default. Touchable's own default is 600ms
const DEFAULT_LONG_PRESS_DURATION = 500;
Expand Down Expand Up @@ -306,6 +307,15 @@ const PressableWithTouchable = (props: PressableProps) => {
? children({ pressed: displayPressed })
: children;

// Testing-library calls the `testOnly_*` props in place of the native press
// events. A disabled button never fires them, so each callback given is
// swapped for a no-op rather than dropped: an unset prop would let
// testing-library fall back to handlers further up the tree, like the ones
// `Touchable` gets below, and those can still run. Callbacks that weren't
// given stay unset, as on an enabled button.
const testOnlyHandler = <T,>(handler: T) =>
IS_TEST_ENV ? (handler && disabled === true ? noop : handler) : undefined;

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return (
<Touchable
{...remainingProps}
Expand All @@ -326,12 +336,12 @@ const PressableWithTouchable = (props: PressableProps) => {
onLongPress={handleLongPress}
onHoverIn={handleHoverIn}
onHoverOut={handleHoverOut}
testOnly_onPress={IS_TEST_ENV ? onPress : undefined}
testOnly_onPressIn={IS_TEST_ENV ? onPressIn : undefined}
testOnly_onPressOut={IS_TEST_ENV ? onPressOut : undefined}
testOnly_onLongPress={IS_TEST_ENV ? onLongPress : undefined}
testOnly_onHoverIn={IS_TEST_ENV ? onHoverIn : undefined}
testOnly_onHoverOut={IS_TEST_ENV ? onHoverOut : undefined}>
testOnly_onPress={testOnlyHandler(onPress)}
testOnly_onPressIn={testOnlyHandler(onPressIn)}
testOnly_onPressOut={testOnlyHandler(onPressOut)}
testOnly_onLongPress={testOnlyHandler(onLongPress)}
testOnly_onHoverIn={testOnlyHandler(onHoverIn)}
testOnly_onHoverOut={testOnlyHandler(onHoverOut)}>
{resolvedChildren}
{__DEV__ ? (
<PressabilityDebugView color="red" hitSlop={normalizedHitSlop} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { PureNativeButton } from './GestureButtons';

const DEFAULT_LONG_PRESS_DURATION = 500;
const IS_TEST_ENV = isTestEnv();
const noop = () => undefined;

const StatefulPressable = (props: PressableProps) => {
const {
Expand Down Expand Up @@ -461,6 +462,14 @@ const StatefulPressable = (props: PressableProps) => {

const tvProps = getTVProps(remainingProps);

// Testing-library calls the `testOnly_*` props in place of the native press
// events. A disabled button never fires them, so each callback given is
// swapped for a no-op rather than dropped: an unset prop would let
// testing-library fall back to handlers further up the tree. Callbacks that
// weren't given stay unset, as on an enabled button.
const testOnlyHandler = <T,>(handler: T) =>
IS_TEST_ENV ? (handler && disabled === true ? noop : handler) : undefined;

return (
<GestureDetector gesture={gesture}>
<PureNativeButton
Expand All @@ -477,12 +486,12 @@ const StatefulPressable = (props: PressableProps) => {
borderless={android_ripple?.borderless ?? undefined}
foreground={android_ripple?.foreground ?? undefined}
style={[pointerStyle, styleProp]}
testOnly_onPress={IS_TEST_ENV ? onPress : undefined}
testOnly_onPressIn={IS_TEST_ENV ? onPressIn : undefined}
testOnly_onPressOut={IS_TEST_ENV ? onPressOut : undefined}
testOnly_onLongPress={IS_TEST_ENV ? onLongPress : undefined}
testOnly_onHoverIn={IS_TEST_ENV ? onHoverIn : undefined}
testOnly_onHoverOut={IS_TEST_ENV ? onHoverOut : undefined}>
testOnly_onPress={testOnlyHandler(onPress)}
testOnly_onPressIn={testOnlyHandler(onPressIn)}
testOnly_onPressOut={testOnlyHandler(onPressOut)}
testOnly_onLongPress={testOnlyHandler(onLongPress)}
testOnly_onHoverIn={testOnlyHandler(onHoverIn)}
testOnly_onHoverOut={testOnlyHandler(onHoverOut)}>
{childrenProp}
{__DEV__ ? (
<PressabilityDebugView color="red" hitSlop={normalizedHitSlop} />
Expand Down
Loading