Skip to content

Conversation

@ACR1209
Copy link

@ACR1209 ACR1209 commented Jun 11, 2025

Description

Adds a new toHaveTextContent matcher to the React Native module, allowing you to match an element’s text content using a string, a regular expression, or a custom function.

@ACR1209 ACR1209 requested a review from suany0805 June 11, 2025 19:36
return element.flatMap(child => this.collectText(child));
}

if (element && typeof element === "object" && "props" in element) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add parenthesis to group logical validations for a better readability please

}

if (Array.isArray(element)) {
return element.flatMap(child => this.collectText(child));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure flatMap will always work here? is the collectText always going to return a 1-level nested array

* @returns the assertion instance
*/
public toHaveTextContent(text: TestableTextMatcher): this {
const actualTextContent = this.getTextContent(this.actual);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could validate that text is a string, RegExp, or function before this line so we can throw the error from helpers/testableTextMatcherToString here:

  throw new Error("Matcher must be a string, RegExp, or function.");

const error = new AssertionError({
actual: this.actual,
message: `Expected element ${this.toString()} to have text content matching '` +
`${testableTextMatcherToString(text)}'.`,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use the text variable instead of the testableTextMatcherToString function for the messaging?

* Type representing a value that can be used to match text content in tests.
* It can be a string, a ReactTestInstance, or an array of ReactTestInstances.
*/
export type WithTextContent = string | ReactTestInstance | ReactTestInstance[];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type naming for WithTextContext could improve for the readability to TextContent, wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants