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

Ionicons not rendering

Open andrewmwhite opened this issue 4 years ago • 1 comments

Describe the bug

Ionicons component from @expo/vector-icons doesn't appear to render under RNTL but does under react-test-renderer. It appears as an empty <Text /> element under RNTL:

yarn run v1.22.17
$ jest
 PASS  ./App.test.js
 FAIL  ./Icon.test.js (5.831 s)
  ● Console

    console.log
      <Text />

      at debugDeep (node_modules/@testing-library/react-native/build/helpers/debugDeep.js:19:13)

    console.log
      { type: 'Text', props: {}, children: null }

      at Object.<anonymous> (Icon.test.js:13:13)

    console.log
      {
        type: 'Text',
        props: {
          accessibilityLabel: 'myIcon',
          allowFontScaling: false,
          style: [ [Object], undefined, [Object], {} ]
        },
        children: [ '' ]
      }

      at Object.<anonymous> (Icon.test.js:18:13)

  ● <Ionicons /> › renders icon with react-native-testng-library

    No instances found with accessibilityLabel /myIcon/i

      12 |     tree.debug();
      13 |     console.log(tree.toJSON());
    > 14 |     expect(tree.getByLabelText(/myIcon/i)).toBeDefined();
         |                 ^
      15 |   });
      16 |   it('renders icon with react-test-renderer', () => {
      17 |     const tree = renderer.create(icon);

      at Object.<anonymous> (Icon.test.js:14:17)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 2 passed, 3 total
Snapshots:   0 total
Time:        6.957 s
Ran all test suites.
error Command failed with exit code 1.

The first two log messages are using RNTL, the third is with react-test-renderer.

Expected behavior

Ionicons component should render when using RNTL's render function as under react-test-renderer's.

Steps to Reproduce

Minimal repro repo:

https://github.com/andrewmwhite/rntl-ionicons-bug-repro

yarn install && yarn test

Versions

  npmPackages:
    @testing-library/react-native: ^8.0.0 => 8.0.0 
    react: 17.0.1 => 17.0.1 
    react-native: 0.64.3 => 0.64.3 
    @expo/vector-icons: ^12.0.5 => 12.0.5 

andrewmwhite avatar Dec 06 '21 01:12 andrewmwhite

Hi @andrewmwhite ! Thanks for reporting this issue and providing a reproductible demo !

I investigated and it is not a bug on react-native-testing-library side. On your repo, when commenting the first test, the second one will only render a raw Text without props. This happens because the font is not loaded yet (see https://github.com/expo/vector-icons/blob/ef0f0e7654cae597a41843836cca950bf5ffaaeb/src/createIconSet.tsx#L153). When rendering the icon in the second test it seems that the font is already loaded (not quite sure how it works though).

To fix your issue you can either wait for the font to load using waitFor or find by query

it('renders icon with react-native-testng-library', async () => {
    const tree = render(icon);
    expect(await tree.findByLabelText(/myIcon/i)).toBeDefined();
  });

Or you can mock expo-font library so that fonts are always considered loaded

jest.mock('expo-font', () => ({
  loadAsync: jest.fn(),
  isLoaded: jest.fn().mockReturnValue(true)
})) 

Maybe you can ask on the @expo/vector-icons repo what they think is the right way to test icons

I hope this helps you, I'm closing this issue since it's not rntl related

pierrezimmermannbam avatar Oct 16 '22 19:10 pierrezimmermannbam