hossein-zare / react-native-dropdown-picker

A single / multiple, categorizable, customizable, localizable and searchable item picker (drop-down) component for react native which supports both Android & iOS.
https://hossein-zare.github.io/react-native-dropdown-picker-website/
MIT License
970 stars 294 forks source link

Add support for Jest and @testing-library/react-native library #618

Open amwebexpert opened 1 year ago

amwebexpert commented 1 year ago
    How can we leverage the testID functionality added in v5.2 for a Dropdown Picker item to simulate a test environment to click on the Dropdown picker and select a different item?

We tried to add a testID for all our Dropdown Picker items and then tried to retrieve them based on the testID using getByTestID method in the @testing-library/react-native library - however the test was not able to find the Dropdown picker item.

Any code example/snippet for this would be much appreciated.

Originally posted by @sagupta1001 in https://github.com/hossein-zare/react-native-dropdown-picker/issues/127#issuecomment-999718015

JSONRice commented 1 year ago
    How can we leverage the testID functionality added in v5.2 for a Dropdown Picker item to simulate a test environment to click on the Dropdown picker and select a different item?

We tried to add a testID for all our Dropdown Picker items and then tried to retrieve them based on the testID using getByTestID method in the @testing-library/react-native library - however the test was not able to find the Dropdown picker item.

Any code example/snippet for this would be much appreciated.

Originally posted by @sagupta1001 in #127 (comment)

@amwebexpert where you able to figure out a solution? I'm having the same problem. I can't seem to retrieve the item based on the testID and really need this for code coverage. @hossein-zare any thoughts on this one?

amwebexpert commented 1 year ago

@jasonwr Unfortunately no... I'm just hoping 🤞 one of the contributors have some spare time to add this feature

JSONRice commented 1 year ago

@jasonwr Unfortunately no... I'm just hoping 🤞 one of the contributors have some spare time to add this feature

Ok I figured out the problem (at least part of it). The testID for each dropdown item shows up once the dropdown is in an open state. Just trying to figure out how to tell the dropdown to be open because I tried this:

    const firstSeen = screen.getByTestId(
      'history-primary-care-add-provider-first-seen',
    );

    fireEvent.press(firstSeen);

I'm not sure what to do at this point as the DropDownPicker has the setOpen prop.

JSONRice commented 1 year ago

@amwebexpert ok after putting this line in it's now working (tests are finally passing):

onPress={setFirstSeenOpen}

@hossein-zare IMHO this is a bit redundant and a potential bug because the following is a required prop and what I had originally (and that's not working):

setOpen={setFirstSeenOpen}

Perhaps I'm not understanding something here. I'd be happy to setup a Zoom call to go over this solution. This is for my work so I can't post much here.

EDIT: This is a temporary solution because memoization doesn't cause another re-render since the value being set isn't changing.

catalinberta commented 1 year ago

I was confused by most solutions here but after trying out I finally understood that:

So you end up with something like

<Dropdown
...
setOpen={setStateToOpen}
onPress={setStateToOpen}
...
/>

e.g.

items = {[
{
  value: "val1",
  label: "label1",
  testID: "dropdownItem"
},
{
  value: "val2",
  label: "label2",
  testID: "dropdownItem"
}
]}

And then you can press them to select a value in tests

harshilparmar commented 10 months ago

@jasonwr Unfortunately no... I'm just hoping 🤞 one of the contributors have some spare time to add this feature

Ok, let me try.