0
votes

I'm fairly new to using the react-testing-library, specifically the native-testing-library. I seem to be running into trouble querying elements.

In the example below I would expect the test to pass but it can't seem to find the element.

I have a feeling I am doing something very wrong somewhere but I can't find very much information on it. Any help at all would be greatly appreciated [:

Edit** It seems to work fine if I use a TestID but shouldn't I still be able to query it by the text comment? I don't like using test IDs unless absolutely necessary since it isn't user facing.

Here is the component that I want to test

import React from 'react';
import {Text, View} from 'react-native';
import {GITHUB_COLORS} from '../constants/colors';
import styles from './RepoCard.style';
import truncate from '../utilities/truncate';

function RepoCard({repo}) {
  return (
    <View style={styles.card}>
      {repo && (
        <>
          <Text style={styles.title}>{truncate(25, repo.name)}</Text>
          {repo.description && (
            <Text style={styles.description}>
              {truncate(60, repo.description)}
            </Text>
          )}
          {repo.language && (
            <View style={styles.langauge}>
              <View
                style={[
                  styles.languageColor,
                  {
                    backgroundColor: GITHUB_COLORS[repo.language.toLowerCase()],
                  },
                ]}
              />
              <Text style={styles.text}>{repo.language}</Text>
            </View>
          )}
        </>
      )}
    </View>
  );
}

export default RepoCard;

Here is the test file

import React from 'react';
import {render, wait} from '@testing-library/react-native';
import RepoCard from './RepoCard';
import {REPOS} from '../testing/mocks/repos';

test('displays the repo name', async () => {
  const testRepo = REPOS[0];
  const {queryByText} = render(<RepoCard repo={testRepo} />);
  const name = testRepo.name;

  await wait(() => expect(queryByText(name)).toBeTruthy());
});

And finally here is the repo object, I double checked it and everything that it needs should be in it.

export const REPOS = [
  {
    name: 'AndroidPython3',
    language: 'Python',
    description: 'This is a mock description',
  },
  {
    name: 'arduino-i2c-scanner',
    language: 'C++',
    description: 'Simple Arduino I2C scanner as des',
  },
];

I also created a code sandbox for this, although I don't think it can actually run the tests since it uses react-native-web.

https://codesandbox.io/s/gallant-tereshkova-3biqj

1

1 Answers

0
votes

I figured it out, I missed the following in my jest config

+ preset: '@testing-library/react-native'
- preset: 'react-native'