Cannot find custom component using selector with property value
See original GitHub issueI have a custom text input component in my React Native app which wraps TextInput
:
export class FnTextInput extends Component {
static propTypes = {
...TextInput.propTypes
};
static defaultProps = {
style: {}
};
props: {
style?: any,
};
textInput: TextInput;
focus() {
this.textInput.focus();
}
render() {
return (
<TextInput
{...this.props}
id="textInput"
ref={e => (this.textInput = e)}
style={[styles.textInput, this.props.style]}
selectionColor={colors.fortnoxGreen}
placeholderTextColor={colors.secondaryText}
autoCapitalize="none"
autoCorrect={false}
clearButtonMode="while-editing"
/>
);
}
}
I have 2 such text inputs in my login view:
<...>
<FnTextInput
id="usernameInput"
placeholder={strings.t('login.usernamePlaceholder')}
/>
<FnTextInput
id="passwordInput"
placeholder={strings.t('login.passwordPlaceholder')}
/>
</...>
I mount()
login view in tests and try to find the text fields by their ID:
const loginView = mount(<LoginView />);
const usernameInput = loginView.find('FnTextInput[id="usernameInput"]');
Unfortunately, this lookup fails, usernameInput
is empty.
However, the following lookups work as expected:
const textInputs = loginView.find('FnTextInput');
const usernameInput = loginView.find('TextInput[id="textInput"]');
I use react-native-mock-render to enable deep rendering, if it matters. My React Native version is 0.42.3.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Angular 2 'component' is not a known element - Stack Overflow
Given that you defined the custom component in the the template of another component part of the SAME module, then you do not...
Read more >LWC push fails with error "Cannot find Lightning Component ...
I am trying to follow this trailhead module and I am trying to create a selector lightning web component with code
Read more >Using Attributes and Properties in Custom Elements
Global attributes are attributes common to all HTML elements; they can be used on all elements, though they may have no effect on...
Read more >Using custom elements - Web Components | MDN
This article introduces the use of the Custom Elements API. ... an extends property, which specifies the built-in element your element ...
Read more >When to use an attribute selector for Angular components
A CSS element selector is by far the most popular way to mark an Angular component. Although, an attribute selector is mainly meant...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’d recommend using
findWhere
, and implementing your find logic in JS, instead of as string selectors - both because enzyme is currently very limited in its string selector capability, but also because it’s more declarative than “magic strings”.Hi @andriichernenko, I’m not sure you can find your component using a string unless the component has a displayName set. So you can find using the actual component or you could use the id as it’s unique.
I’m not sure why the `TextInput[id=“textInput”]’ works (I’m not familiar with RN) but I can only assume it has a displayName set.
See this page for more info. I hope that helps!