callstack / react-native-testing-library

🦉 Simple and complete React Native testing utilities that encourage good testing practices.
https://callstack.github.io/react-native-testing-library/
MIT License
3.07k stars 271 forks source link

GetByLabel and aria-labelledby #1563

Closed Clement-Hue closed 6 months ago

Clement-Hue commented 7 months ago

Describe the bug

When I use aria-labelledby and getByLabel (or name property of getByRole), the test doesn't work. I also tried to use regex for the label. However I have no problem if I use aria-label

Expected behavior

The test should pass

Steps to Reproduce

Here is the component:

const Switch: React.FC<SwitchProps> = ({checked, onPress, title, ...props}) => {
  const theme = useTheme();
  const id = useId();
  return (
    <View style={switchStyle.container}>
      <BaseSwitch
        thumbColor={
          checked ? theme.color.primary['50'] : theme.color.grey['50']
        }
        trackColor={{
          true: theme.color.primary['30'],
          false: theme.color.grey['30'],
        }}
        value={checked}
        onValueChange={onPress}
        aria-labelledby={id}
        {...props}
      />
      {!!title && (
        <Text nativeID={id} style={switchStyle.title} variant="label">
          {title}
        </Text>
      )}
    </View>
  );
};

Here is the test

  it('should render Switch with label', () => {
    render(<Switch title="activate" />);
    expect(screen.getByRole('switch', {name: 'activate'})).toBeOnTheScreen();
  });

Screenshots

Here the result of the test image

Versions

react: 18.2.0 react-native: 0.73.3 react-test-renderer: 18.2.0 @testing-library/react-native: 12.4.3

mdjastrzebski commented 7 months ago

@Clement-Hue Thanks for reporting this. I've done some checks with our codebase and theoretically this should work fine as we support aria-labelledby and have tests for that.

With what I know so far this problem could be located either in RNTL itself on some edge-case, your component or tests itself. Could you post a minimal reproduction repository so that I can run this locally?

mdjastrzebski commented 6 months ago

Closing as stale.