Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: queryByText and getByText match newlines and tabs properly #984

Merged
merged 1 commit into from
Jun 24, 2022

Conversation

pke
Copy link
Contributor

@pke pke commented May 24, 2022

Summary

This fixes #983.

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()

Test plan

I have added 2 new tests to verify the fix is working.

Copy link
Collaborator

@AugustinLF AugustinLF left a comment

Choose a reason for hiding this comment

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

While I think this change makes sense, I believe it would get us away from @testing-library/react, which doesn't do such normalisation afaik. Do we think it's an ok tradeoff?

@pke
Copy link
Contributor Author

pke commented May 30, 2022

@AugustinLF That is because React Web an RN handle whitespace in JSX differently. In RW it uses HTML whitespace policy, that is, to ignore whitespace. However, RN respects whitespace inside <Text> tags.

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

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

@AugustinLF Looking at the code it seems that we should match Line1\nLine2' when Textcomponent contains the same very same text content. Before this PR we normalised text but did not normalize the the text matcher string which caused thenormalizedText === matcher` check to fail.

src/queries/__tests__/text.test.tsx Outdated Show resolved Hide resolved
src/queries/__tests__/text.test.tsx Outdated Show resolved Hide resolved
@mdjastrzebski
Copy link
Member

As a general comment, regex text queries are more powerful and don't require perfect matching when using e.g. \s

@pke pke force-pushed the fix/text-query-new-lines branch from 86a036d to 6a6111d Compare June 2, 2022 10:05
@pke pke force-pushed the fix/text-query-new-lines branch from 6a6111d to 11ab803 Compare June 2, 2022 10:10
@pke
Copy link
Contributor Author

pke commented Jun 2, 2022

Rebased and applied suggestions from comments.

As a general comment, regex text queries are more powerful and don't require perfect matching when using e.g. \s

I know but here its important, that the formatting is matched exactly, as it has direct visual consequences in RN Text elements unlike React Web.

@mdjastrzebski
Copy link
Member

@thymikee @AugustinLF We're looking for a second approval for this PR. Can you help?

@thymikee thymikee merged commit a05bf4b into callstack:main Jun 24, 2022
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.

queryByText with newline \n doesn't find matching rendered text
5 participants