question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add testID to component props to easily access component with test libraries

See original GitHub issue

Is your feature request related to a problem? Please describe.
The problem I am experiencing is writing tests using detox. I currently have a few selects on a screen and need to tap each one of them in order to set the values, but I am finding it difficult because I cannot specify a custom test id for each of the selects that I use. Another issue I am experiencing is when using stack navigators the previous selects are located that are not visible and I would like a unique way to ensure that I am selecting the correct select when testing.

Describe the solution you’d like
Add a testID prop that overrides the following testID’s when you provide it:

  • android_picker
  • ios_touchable_wrapper
  • android_touchable_wrapper So I would like to replace the testID in index.js with something like this:
<TouchableOpacity
                testID={testID || 'android_touchable_wrapper'}

So when the testID is provided replace it, otherwise keep the original testID.

Describe alternatives you’ve considered
Currently I am selecting all the selects and then accessing them by index, but when you have a lot of selects this gets confusing especially with nested views and navigation stacks

Additional details
NONE

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
TildeWillcommented, May 15, 2020

We were able to write a test that picked something from the picker. We found that you had to tap twice on the text field to get the picker to open up, like this:

in index.js

import RNPickerSelect from 'react-native-picker-select'
...
<RNPickerSelect
  touchableWrapperProps={{testID: "myUsStatePicker"}} 
  ... 
/>

in happyPath.spec.js:

describe('Example', () => {
  beforeEach(async () => {
    await device.reloadReactNative()
  })

  it('lets the user pick a US state from the picker', async () => {
    ...
    await element(by.id('myUsStatePicker')).tap() //tap once to focus the text input
    await element(by.id('myUsStatePicker')).tap() //tap a second time to open the picker modal
    await element(by.text('Alabama')).tap() //can only pick one of the first three-ish values because they are the ones that are visible when the picker is opened, otherwise you could maybe scroll
    await element(by.text('Done')).tap()
    ...
  })
})

cc @stupakov

0reactions
chenopcommented, Jun 14, 2022

Another way of scrolling would be doing:

const picker = element(by.id("ios_picker"));
await expect(picker).toBeVisible();
await picker.tap();
picker.setColumnToValue(0, "Desired Value");
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing a simple component with React Testing Library
This component provides an input field, an “Add” button, and of course some inner logic for word validation and interaction with the external ......
Read more >
Testing Recipes - React
Testing Recipes. Common testing patterns for React components. Note: This page assumes you're using Jest as a test runner. If you use a...
Read more >
Update Props | Testing Library
This is an example of how to update the props of a rendered component. // the basic idea is to simply call `render`...
Read more >
Test your Components With React Testing Library
React Testing Library gives you access to tools located on react-dom and react-dom/test-utils that help you follow best testing practice and ...
Read more >
How to add data-testid attribute to react-select components
Ordinarily, the react-testing-library way to do this is to add a 'data-testid' attribute to the item in question. I've found that it's possible ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found