react-native-testing-library icon indicating copy to clipboard operation
react-native-testing-library copied to clipboard

queryAllByProps returning incorrect number of elements

Open jackharding opened this issue 7 years ago • 8 comments

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?

jackharding avatar Mar 26 '19 13:03 jackharding

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?

thymikee avatar Mar 26 '19 14:03 thymikee

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!

jackharding avatar Mar 26 '19 14:03 jackharding

Would you be so kind and put it in a repository we can download and investigate? :)

thymikee avatar Mar 26 '19 14:03 thymikee

Oh, sorry. I'll do this later.

jackharding avatar Mar 26 '19 14:03 jackharding

@thymikee I've just created this repo.

jackharding avatar Mar 26 '19 15:03 jackharding

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.

thymikee avatar May 28 '19 20:05 thymikee

I have the same issue when only running getByTestId

teodorciuraru avatar Aug 07 '19 13:08 teodorciuraru

@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

thymikee avatar Feb 01 '20 14:02 thymikee