queryAllByProps returning incorrect number of elements
I have a test with the following assertion:
expect(queryAllByProps({ testID: 'searchSuggestion' })).toHaveLength(1);
I know there is only one element with the testID 'searchSuggestion'. When I log the result of queryAllByProps, I get the following:
[ ReactTestInstance {
_fiber:
FiberNode {
tag: 1,
key: 'cardiff',
elementType: [Function],
type: [Function],
stateNode: [ReactClassComponent],
return: [FiberNode],
child: [FiberNode],
sibling: null,
index: 0,
ref: null,
pendingProps: [Object],
memoizedProps: [Object],
updateQueue: null,
memoizedState: [Object],
contextDependencies: null,
mode: 0,
effectTag: 5,
nextEffect: [FiberNode],
firstEffect: null,
lastEffect: null,
expirationTime: 0,
childExpirationTime: 0,
alternate: null,
_debugID: 41,
_debugSource: null,
_debugOwner: [FiberNode],
_debugIsCurrentlyTiming: false,
_debugHookTypes: null } },
ReactTestInstance {
_fiber:
FiberNode {
tag: 1,
key: null,
elementType: [Function],
type: [Function],
stateNode: [Component],
return: [FiberNode],
child: [FiberNode],
sibling: null,
index: 0,
ref: null,
pendingProps: [Object],
memoizedProps: [Object],
updateQueue: null,
memoizedState: null,
contextDependencies: null,
mode: 0,
effectTag: 1,
nextEffect: null,
firstEffect: null,
lastEffect: null,
expirationTime: 0,
childExpirationTime: 0,
alternate: null,
_debugID: 42,
_debugSource: null,
_debugOwner: [FiberNode],
_debugIsCurrentlyTiming: false,
_debugHookTypes: null } },
ReactTestInstance {
_fiber:
FiberNode {
tag: 5,
key: null,
elementType: 'View',
type: 'View',
stateNode: [Object],
return: [FiberNode],
child: [FiberNode],
sibling: null,
index: 0,
ref: null,
pendingProps: [Object],
memoizedProps: [Object],
updateQueue: null,
memoizedState: null,
contextDependencies: null,
mode: 0,
effectTag: 0,
nextEffect: null,
firstEffect: null,
lastEffect: null,
expirationTime: 0,
childExpirationTime: 0,
alternate: null,
_debugID: 43,
_debugSource: null,
_debugOwner: [FiberNode],
_debugIsCurrentlyTiming: false,
_debugHookTypes: null } } ]
Am I misunderstanding how this should work? Or is this a bug?
Repro please, I don't see your code, so what can I say? There should be 1 element, but maybe some components inherit the testID prop and pass it further?
SearchScreen.js
export class SearchScreen extends Component {
state = {
search: '',
typingTimeout: 0,
disableGeo: false,
searchingLocation: false,
}
render() {
let {
eventCount,
suggestions,
history,
fetching,
} = this.props;
return(
<View style={styles.outer}>
<ScrollView>
<View>
<SearchSuggestions
fetching={this.state.search.length >= 3 ? fetching : history.fetching}
suggestions={this.state.search.length >= 3 ? suggestions : history.data}
resultsCount={eventCount}
onSearch={this.goToResults}
displayingHistory={this.state.search.length < 3}
/>
</View>
</ScrollView>
</View>
);
}
}
SearchSuggestions.js
const SearchSuggestions = ({ suggestions, resultsCount, fetching, onSearch }) => (
<View style={styles.container}>
{ fetching ? <Loading /> :
<Fragment>
{ suggestions && !!suggestions.length && suggestions.map(item => (
<TouchableHighlight
onPress={() => onSearch(item.search_term || item.title, null, item.tax_name === 'location' && item.level === 1)}
underlayColor={'transparent'}
key={item.term_id || item.title}
testID={'searchSuggestion'}
>
<View style={styles.result}>
<Text style={styles.resultsText}>{ item.search_term || item.title }</Text>
{ item.text && <Text style={StyleSheet.flatten([styles.resultsText, { marginTop: 5 }])}>{ item.text }</Text> }
</View>
</TouchableHighlight>
)) }
</Fragment>
}
</View>
);
SearchScreen-test.js
test('should verify two questions', () => {
const { debug, queryAllByProps } = render(
<SearchScreen
fetchPreferences={() => null}
history={{
fetching: false,
data: [
{
search_term: 'Cardiff',
term_id: 'cardiff'
}
]
}}
/>
);
expect(queryAllByProps({ testID: 'searchSuggestion' })).toHaveLength(1);
});
Bear in mind I've stripped out irrelevant stuff for clarity. Excuse the mess!
Would you be so kind and put it in a repository we can download and investigate? :)
Oh, sorry. I'll do this later.
@thymikee I've just created this repo.
Thanks! FYI, ByProps matchers are going to be deprecated in v2. Please migrate off this API, e.g. to ByText, ById or ByA11y*. We'll try to fix it for 1.x, but it's not going to be a priority.
I have the same issue when only running getByTestId
@jackharding in this case you can use getAllByTestId.
@teo029 thanks for pointing this out. Turns out that the behavior of getByTestId (searching across all components, incorrect) is different than getAllByTestId (searching across host components, correct). However changing this behavior would be pretty breaking, because some of our users use testID to find a specific custom component, which may not pass this prop to a host component like View or Text. We even have tests for that behavior. Will add changing that behavior to v2 release.
Closing, as this is not actionable, other than forbid testID as a prop to search by in get*ByProps (happy to accept PR adding this check and providing user with a hint to use get*ByTestId