jest-native icon indicating copy to clipboard operation
jest-native copied to clipboard

queryByLabelText not working for custom components

Open piotrponikowski opened this issue 4 years ago • 0 comments

I am trying to figure out why this test works:

test('test 1', () => {
  const { queryByLabelText, debug } = render(
    <View accessibilityLabel={'parent'}>
      <Text>One</Text>
      <View>
        <Text>Two</Text>
        <Text>Three</Text>
      </View>
    </View>
  );

  debug();
  expect(queryByLabelText('parent')).toHaveTextContent('OneTwoThree');
});

but after moving part of hierarchy to separate component it stopped to pass:

test('test 2', () => {
  const { queryByLabelText, debug } = render(
    <View accessibilityLabel={'parent'}>
      <Text>One</Text>
      <TestComponent />
    </View>
  );

  debug();
  expect(queryByLabelText('parent')).toHaveTextContent('OneTwoThree');
});

const TestComponent = () => {
  return (
    <View>
      <Text>Two</Text>
      <Text>Three</Text>
    </View>
  );
};

Error for 2nd test:

  Expected element to have text content:
    OneTwoThree
  Received:
    One

Both tests have the same snapshot printed by debug method:

    <View
      accessibilityLabel="parent"
    >
      <Text>
        One
      </Text>
      <View>
        <Text>
          Two
        </Text>
        <Text>
          Three
        </Text>
      </View>
    </View>

Is it intentional behavior?

Tested with:

  • @testing-library/jest-native: 4.0.1
  • @testing-library/react-native: 8.0.0-rc.1

piotrponikowski avatar Apr 08 '21 18:04 piotrponikowski