keil
keil

Reputation: 433

How to use react-native-testing-library getByRole

I'm trying to use getByRole in react native testing library but can't for the life of me figure out the syntax

Component in question-

<Text 
  accessibilityRole={'button'}
  accessibilityLabel={'upvote count'}
  style={[styles.upvoteText, styles.upvoteCount]}>
  {upvoteCount}
</Text>

and my code

expect(getByRole('button', {name: 'upvote count'} as MatcherOptions)).toBe(2)

Upvotes: 6

Views: 4581

Answers (1)

Joshua Richardson
Joshua Richardson

Reputation: 115

So, I'm working on something similar. I saw this in the docs to help with this case:

In Component

<Text 
  accessibilityRole={'button'}
  accessibilityLabel={'upvote count'}
  style={[styles.upvoteText, styles.upvoteCount]}>
  {upvoteCount}
</Text>

In Test

it('gets the button', () => {
  const { getByA11yRole, getByA11yLabel } = render(<Text>foo</Text>);
  
  expect(getByA11yRole('button')).toBeDefined();
  expect(getbyA11yRole('upvote count')).toBeDefined();
});

Upvotes: 1

Related Questions