Skip to content

fix: non-responder wrapping host elements ignore disabled prop #572

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 5 commits into from
Oct 21, 2020
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: 23 additions & 0 deletions src/__tests__/fireEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,26 @@ test('is not fooled by non-native disabled prop', () => {
fireEvent.press(screen.getByText('Trigger Test'));
expect(handlePress).toHaveBeenCalledTimes(1);
});

function TestChildTouchableComponent({ onPress, someProp }) {
return (
<View>
<TouchableOpacity onPress={onPress} disabled={someProp}>
<Text>Trigger</Text>
</TouchableOpacity>
</View>
);
}

test('is not fooled by non-responder wrapping host elements', () => {
const handlePress = jest.fn();

const screen = render(
<View>
<TestChildTouchableComponent onPress={handlePress} someProp={true} />
</View>
);

fireEvent.press(screen.getByText('Trigger'));
expect(handlePress).not.toHaveBeenCalled();
});
43 changes: 29 additions & 14 deletions src/fireEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,47 @@
import act from './act';
import { ErrorWithStack } from './helpers/errors';

const isTextInputComponent = (element: ReactTestInstance) => {
const isHostElement = (element?: ReactTestInstance) => {
return typeof element?.type === 'string';
};

const isTextInput = (element?: ReactTestInstance) => {
// eslint-disable-next-line import/no-extraneous-dependencies
const { TextInput } = require('react-native');
return element.type === TextInput;
return element?.type === TextInput;
};

const isTouchResponder = (element?: ReactTestInstance) => {
if (!isHostElement(element)) return false;

return !!element?.props.onStartShouldSetResponder || isTextInput(element);
};

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

return touchResponder?.props.onStartShouldSetResponder?.() !== false;
};

const findEventHandler = (
element: ReactTestInstance,
eventName: string,
callsite?: any,
nearestHostDescendent?: ReactTestInstance,
nearestTouchResponder?: ReactTestInstance,
hasDescendandHandler?: boolean
) => {
const handler = getEventHandler(element, eventName);
const hasHandler = handler != null || hasDescendandHandler;

const isHostComponent = typeof element.type === 'string';
const hostElement = isHostComponent ? element : nearestHostDescendent;
const touchResponder = isTouchResponder(element)
? element
: nearestTouchResponder;

const isEventEnabled = isTextInputComponent(element)
? element.props.editable !== false
: hostElement?.props.onStartShouldSetResponder?.() !== false;

if (handler && isEventEnabled) return handler;
const handler = getEventHandler(element, eventName);
if (handler && isEventEnabled(element, touchResponder)) return handler;

// Do not bubble event to the root element
const hasHandler = handler != null || hasDescendandHandler;
if (element.parent === null || element.parent.parent === null) {
if (hasHandler) {
return null;
Expand All @@ -43,7 +58,7 @@ const findEventHandler = (
element.parent,
eventName,
callsite,
hostElement,
touchResponder,
hasHandler
);
};
Expand Down