Skip to content

How to get text as string from node children  #230

@automatensalat

Description

@automatensalat

Hi there,

I switched over from Enzyme for my current RN project and I'm wondering how to handle a specific assertion.

Say I have a component like this:

const MyComponent = ({ value }) => {
  return <Text testID="text">Value is {value}</Text>;
}

I now want to test that the component renders the value in the Text node:

const renderer = render(<MyComponent value="xyz" />);
const text = renderer.getByTestId("text");
expect(text.props.children).toBe("Value is xyz");

However, the test fails: Expected: "Value is xyz", Received: ["Value is ", "xyz"]

In Enzyme there is a .text() method on the node which flatly returns the text contained in the child. Is there a way to do this smoothly in react-native-testing-library?
Of course I could change the component to use string templates or reduce the props.children in the test method to a string myself, just wondering if there is a better way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions