Skip to content

Commit a05bf4b

Browse files
authored
fix: queryByText and getByText match newlines and tabs properly (#984)
Fixes #983
1 parent 51cd8d0 commit a05bf4b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/matches.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ export function matches(
1313

1414
const normalizedText = normalizer(text);
1515
if (typeof matcher === 'string') {
16+
const normalizedMatcher = normalizer(matcher);
1617
return exact
17-
? normalizedText === matcher
18-
: normalizedText.toLowerCase().includes(matcher.toLowerCase());
18+
? normalizedText === normalizedMatcher
19+
: normalizedText.toLowerCase().includes(normalizedMatcher.toLowerCase());
1920
} else {
2021
return matcher.test(normalizedText);
2122
}

src/queries/__tests__/text.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,17 @@ test('getByText and queryByText work properly with multiple nested fragments', (
473473
expect(getByText('Hello')).toBeTruthy();
474474
expect(queryByText('Hello')).not.toBeNull();
475475
});
476+
477+
test('getByText and queryByText work with newlines', () => {
478+
const textWithNewLines = 'Line 1\nLine 2';
479+
const { getByText, queryByText } = render(<Text>{textWithNewLines}</Text>);
480+
expect(getByText(textWithNewLines)).toBeTruthy();
481+
expect(queryByText(textWithNewLines)).toBeTruthy();
482+
});
483+
484+
test('getByText and queryByText work with tabs', () => {
485+
const textWithTabs = 'Line 1\tLine 2';
486+
const { getByText, queryByText } = render(<Text>{textWithTabs}</Text>);
487+
expect(getByText(textWithTabs)).toBeTruthy();
488+
expect(queryByText(textWithTabs)).toBeTruthy();
489+
});

0 commit comments

Comments
 (0)