Skip to content

queryByText with newline \n doesn't find matching rendered text #983

@pke

Description

@pke

Describe the bug

New lines, that are rendered in RN as separate Lines, are wrongly normalised during testing.

const { queryByText } = render(<Text>Line 1\nLine 2</Text>)
expect(queryByText("Line 1\nLine 2")).toBeDefined()

Expected behavior

I expect the test to pass.
I have checked the queryByText and it seems it uses the same normaliser (Default) as the findTextByNode so this should actually work.

I know that in HTML whitespace is ignored and normalised by browsers to one space. But in RN it actually matters and is rendered (at least \n whitespace).

The test can be made to pass by this change:

-expect(queryByText("Line 1\nLine 2")).toBeDefined()
+expect(queryByText("Line 1\nLine 2".split("\n").join(" ")).toBeDefined()

which indicates to me, the queryByText does in fact not normalises the input query text correctly.

Versions

npmPackages:
@testing-library/react-native: 9.0.0 => 9.0.0
react: 17.0.2 => 17.0.2
react-native: 0.66.3 => 0.66.3
react-test-renderer: 16.14.0 => 16.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions