question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot find custom component using selector with property value

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
ljharbcommented, Jun 3, 2017

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”.

2reactions
samit4mecommented, May 19, 2017

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.

// using id only
loginView.find('[id="usernameInput"]');

// OR 
// using the component (it will require FnTextInput to be imported into your test)
loginView.find(FnTextInput).find('[id="usernameInput"]');

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found