testing-library / native-testing-library

🐳 Simple and complete React Native testing utilities that encourage good testing practices.
https://native-testing-library.com
MIT License
516 stars 44 forks source link

Make `getNodeText` recursively get children #104

Closed ghost closed 4 years ago

ghost commented 4 years ago

Describe the feature you'd like:

Right now, getNodeText only checks immediate children for their text content. So code like this is not easily testable:

<View accessibilityLabel={"a11yLabel"} accessible={true}>
  <Image src={someImage} />
  <View>
    <Text>Hello</Text>
    <Text>World</Text>
  </View>
</View>

// this fails
expect(getNodeText(getByLabelText("a11yLabel")).toBe("Hello World")

Suggested implementation:

I'd recursively go through the children and merge any arrays in there. Couldn't get anywhere in half an hour but am happy to invest more time if we agree this is a good idea.

Describe alternatives you've considered:

None really

Teachability, Documentation, Adoption, Migration Strategy:

A user will use it the same way as they can already except that it's now recursive so even nested children will be merged.

bcarroll22 commented 4 years ago

Hi, thanks for using Testing Library!

Unfortunately, I don’t think we’ll be able to implement this feature. We do have the tools to do it, but it’s not the way text matchers work in the rest of the Testing Library family, and keeping in parity with the rest of the family is a top priority for us. If text is broken up into multiple nodes, it won’t match in React/DOM Testing Library either.

With that in mind, my recommendation would be to take it upstream to DOM Testing Library to get buy in to change it there, or follow the warning given when text can’t be found. If instead of a string or regex match, you pass a callback function as the “text”, you can do custom matching to find text broken up by elements. Two arguments are passed to that callback, here it is in the source if you want to check it out.

Hopefully that helps, and good luck! ☺️

thymikee commented 4 years ago

Implemented in v7.0. FYI, this repository is no longer responsible for this package. See the migration guide to v7.0.