Skip to content

add tests to should not fire on non-editable TextInput getting by testID #1080

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

Closed
wants to merge 3 commits into from
Closed
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
33 changes: 19 additions & 14 deletions src/__tests__/fireEvent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,28 @@ 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';
test('should not fire on non-editable composite TextInput', () => {
const onChangeText = jest.fn();
const view = render(
<TextInput
editable={false}
placeholder="subject"
onChangeText={onChangeText}
/>
);

const { getByPlaceholderText } = render(
<View>
<TextInput
editable={false}
placeholder={placeholder}
onChangeText={onChangeTextMock}
/>
</View>
fireEvent.changeText(view.getByPlaceholderText('subject'), 'new text');
expect(onChangeText).not.toHaveBeenCalled();
});

test('should not fire on non-editable host TextInput', () => {
const onChangeText = jest.fn();
const view = render(
<TextInput editable={false} onChangeText={onChangeText} testID="subject" />
);

fireEvent.changeText(getByPlaceholderText(placeholder), NEW_TEXT);
expect(onChangeTextMock).not.toHaveBeenCalled();
fireEvent.changeText(view.getByTestId('subject'), 'new text');
expect(onChangeText).not.toHaveBeenCalled();
});

test('should not fire on non-editable TextInput with nested Text', () => {
Expand Down
9 changes: 8 additions & 1 deletion src/fireEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ const isHostElement = (element?: ReactTestInstance) => {
return typeof element?.type === 'string';
};

// Rendering TextInput results in having composite `TextInput` component with
// child text input host component. Both will have the same `editable` and
// `onChangeText` props.
const isTextInput = (element?: ReactTestInstance) => {
const { TextInput } = require('react-native');
return element?.type === TextInput;

const isExportedCompositeTextInput = element?.type === TextInput;
const isChildHostTextInput = element?.parent?.type === TextInput;

return isExportedCompositeTextInput || isChildHostTextInput;
};

const isTouchResponder = (element?: ReactTestInstance) => {
Expand Down