-
Notifications
You must be signed in to change notification settings - Fork 273
fix: prevent non editable textinputs from being updated through getByTestID #1092
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
Changes from 1 commit
e582ddd
a23787e
9d4aaf1
26d484b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
import act from './act'; | ||
import { filterNodeByType } from './helpers/filterNodeByType'; | ||
|
||
type EventHandler = (...args: any) => unknown; | ||
|
||
|
@@ -8,8 +9,15 @@ const isHostElement = (element?: ReactTestInstance) => { | |
}; | ||
|
||
const isTextInput = (element?: ReactTestInstance) => { | ||
if (!element) { | ||
return false; | ||
} | ||
|
||
const { TextInput } = require('react-native'); | ||
return element?.type === TextInput; | ||
return ( | ||
filterNodeByType(element, TextInput) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if this should be handled directly in So fine with me, but perhaps some other maintainer might feel different about that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean if filterNodeByType made the comparison for both the component and the component name ? It would involve that every query would need to filter composite components afterwards (except testId queries that already filter host components), I think it would introduce additional complexity and I don't see clearly how it could be beneficial so I'm not sure about it either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah exactly. I was wondering if that would be the sort of abstraction we would like to move out of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't Wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW I am against having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes TextInput (composite) is always rendering a host TextInput, I was suggesting events could only be triggered on host component in the discussion on the other pr #1080 (comment) but as @AugustinLF noticed, it would be an issue for props with different naming. This could maybe be fixed by mapping native props name with composite components props name but it seems rather complicated and I'm not sure such a mapping is entirely possible. Also props from custom components that do not match any native prop couldn't be triggered through fake events anymore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re query returning host components: iirc we had this assumption with @thymikee that all of the queries should always return host components (maybe except the re
Both of the methods rely in some way on "internal" component structure from React Native, the question is which is less likely to change. I suspect that 1st option might be more stable as it probably refers to touch event handling, but we would need to research which prop is actually being set when @pierrezimmermannbam, @AugustinLF, @thymikee which one would you pick, do you see other alternatives? Additionally we should create a test case where we test whether our simulation works fine, so that we are able to detect when things break. That probably should be something like: test(`fireEvent handles implicitly editable TextInput', () => {
const onChange = jest.fn();
render(<TextInput testID="subject" onChange={jest.fn} />);
const subject = screen.getByTest("subject");
expect(subject.type).toBe("TextInput");
fireEvent.change(subject, "new value");
expect(onChange).toHaveBeenCalledWith("newValue");
}
test(`fireEvent handles explicitly editable TextInput', () => {
const onChange = jest.fn();
render(<TextInput testID="subject" onChange={jest.fn} editable />);
const subject = screen.getByTest("subject");
expect(subject.type).toBe("TextInput");
fireEvent.change(subject, "new value");
expect(onChange).toHaveBeenCalledWith("newValue");
}
test(`fireEvent handles non-editable TextInput', () => {
const onChange = jest.fn();
render(<TextInput testID="subject" onChange={jest.fn} editable={false} />);
const subject = screen.getByTest("subject");
expect(subject.type).toBe("TextInput");
fireEvent.change(subject, "new value");
expect(onChange).not.toHaveBeenCalled();
} |
||
filterNodeByType(element, 'TextInput') | ||
); | ||
}; | ||
|
||
const isTouchResponder = (element?: ReactTestInstance) => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.