React-native-testing-library: getByText No instances found error

Created on 30 May 2020  路  2Comments  路  Source: callstack/react-native-testing-library

Ask your Question

I'm writing a simple Button component , which has a prop loading, if loading is send true, Text component is not rendered, in place of it a loader is rendered. I want to test, if I pass loading prop true, the Text component is not rendered but I'm getting No instance found when using getByText

MyButton.js

const MyButton = ({ title, loading, onPress }) => (
    <TouchableOpacity onPress={onPress} accessible accessibilityRole="button">
         {!loading && (<Text>{title}</Text>)}
         {loading && (<ActivityIndicator />)}
    </TouchableOpacity>
);

MyButton.test.js

test('loading state is handled', () => {
    // Setup
    const buttonTitle = 'Press me';
    const { getByText } = render(<MyButton loading={true} title={buttonTitle} onPress={() => {}} />);

    // Exercise
    const textElement = getByText(buttonTitle);

    // Verify
    expect(textElement).toBeUndefined();
  })

But I'm getting No instances found error

Screenshot from 2020-05-30 10-08-14

question

Most helpful comment

loading is true, so text doesn't render and hence getByText throws an error.

From the docs:

getBy* queries return the first matching node for a query, and throw an error if no elements match or if more than one match is found (use getAllBy instead).

If you want to verify the text is not there, use queryByText

All 2 comments

loading is true, so text doesn't render and hence getByText throws an error.

From the docs:

getBy* queries return the first matching node for a query, and throw an error if no elements match or if more than one match is found (use getAllBy instead).

If you want to verify the text is not there, use queryByText

Thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ccfz picture ccfz  路  10Comments

maitriyogin picture maitriyogin  路  7Comments

entiendoNull picture entiendoNull  路  6Comments

jonmchan picture jonmchan  路  11Comments

deepakaggarwal7 picture deepakaggarwal7  路  3Comments