feat: support 'editable' prop on TextInput (#517)

* test: Add tests for non-editable TextInput

* feat: use editable prop to check for event handling
This commit is contained in:
Panagiotis Plytas
2020-09-07 23:42:22 +03:00
committed by GitHub
parent d55ea0b0b4
commit f68f3120ec
2 changed files with 51 additions and 2 deletions
+40
View File
@@ -193,6 +193,46 @@ test('should not fire on disabled Pressable', () => {
expect(handlePress).not.toHaveBeenCalled();
});
test('should not fire on non-editable TextInput', () => {
const placeholder = 'Test placeholder';
const onChangeTextMock = jest.fn();
const NEW_TEXT = 'New text';
const { getByPlaceholderText } = render(
<View>
<TextInput
editable={false}
placeholder={placeholder}
onChangeText={onChangeTextMock}
/>
</View>
);
fireEvent.changeText(getByPlaceholderText(placeholder), NEW_TEXT);
expect(onChangeTextMock).not.toHaveBeenCalled();
});
test('should not fire on non-editable TextInput with nested Text', () => {
const placeholder = 'Test placeholder';
const onChangeTextMock = jest.fn();
const NEW_TEXT = 'New text';
const { getByPlaceholderText } = render(
<View>
<TextInput
editable={false}
placeholder={placeholder}
onChangeText={onChangeTextMock}
>
<Text>Test text</Text>
</TextInput>
</View>
);
fireEvent.changeText(getByPlaceholderText(placeholder), NEW_TEXT);
expect(onChangeTextMock).not.toHaveBeenCalled();
});
test('should pass event up on disabled TouchableOpacity', () => {
const handleInnerPress = jest.fn();
const handleOuterPress = jest.fn();
+11 -2
View File
@@ -2,6 +2,12 @@
import act from './act';
import { ErrorWithStack } from './helpers/errors';
const isTextInputComponent = (element: ReactTestInstance) => {
// eslint-disable-next-line import/no-extraneous-dependencies
const { TextInput } = require('react-native');
return element.type === TextInput;
};
const findEventHandler = (
element: ReactTestInstance,
eventName: string,
@@ -14,8 +20,11 @@ const findEventHandler = (
const isHostComponent = typeof element.type === 'string';
const hostElement = isHostComponent ? element : nearestHostDescendent;
const isEventEnabled =
hostElement?.props.onStartShouldSetResponder?.() !== false;
const isEventEnabled = isTextInputComponent(element)
? element.props.editable !== false
: hostElement?.props.onStartShouldSetResponder?.() !== false;
if (handler && isEventEnabled) return handler;
// Do not bubble event to the root element