Skip to content

Commit

Permalink
Merge pull request #44 from Workiva/regexp-escape-testids
Browse files Browse the repository at this point in the history
Fix test ID queries not properly escaping RegExps
  • Loading branch information
rm-astro-wf authored Dec 10, 2021
2 parents 747df6f + d3c235a commit 5f39b37
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/src/dom/queries/by_testid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ external List< /*Element*/ dynamic> _jsQueryAllByTestId(
dynamic _convertTestIdStringToRegExp(dynamic testId, {bool exact = true}) {
if (testId is! String) return TextMatch.toJs(testId);

final testIdMatcher = exact ? RegExp('(\\s|^)$testId(\\s|\$)') : RegExp('(.?)$testId(.?)', caseSensitive: false);
final testIdMatcher = exact
? RegExp('(\\s|^)${RegExp.escape(testId as String)}(\\s|\$)')
: RegExp('(.?)${RegExp.escape(testId as String)}(.?)', caseSensitive: false);

return TextMatch.toJs(testIdMatcher);
}
Expand Down
20 changes: 20 additions & 0 deletions test/unit/dom/queries/by_testid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ void main() {
});
});

test(
'works when test IDs contain regular expression syntax'
' (by properly escaping the test ID when incorporating it into a RegExp)', () {
final view = rtl.render(react.span({
'data-test-id': 'testId(1)',
}) as ReactElement);

expect(view.getByTestId('testId(1)'), isA<SpanElement>());
});

group('with the "exact: false" argument', () {
group('getByTestId', () {
test('[non-exact string match]', () {
Expand Down Expand Up @@ -255,6 +265,16 @@ void main() {
expect(await view.findAllByTestId('estid-2', exact: false), hasLength(2));
});
});

test(
'works when test IDs contain regular expression syntax'
' (by properly escaping the test ID when incorporating it into a RegExp)', () {
final view = rtl.render(react.span({
'data-test-id': 'testId(1)',
}) as ReactElement);

expect(view.getByTestId('estId(1)', exact: false), isA<SpanElement>());
});
});
});

Expand Down
3 changes: 2 additions & 1 deletion test/unit/dom/wait_for_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -456,5 +456,6 @@ void main() {
}, timeout: asyncQueryTestTimeout);
});
});
});
// Attempt to fix flaky timeout failures in CI
}, tags: 'run-alone');
}

0 comments on commit 5f39b37

Please sign in to comment.